package com.windsoft;
/**
* 邮件发送类
*/
import javax.mail.Session;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
public class JavaMail2 {
public static void sendMessage(String smtpHost,
String from,
String to,
String subject,
String messageText,
String fileName) throws MessagingException {
// Step 1: Configure the mail session
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.smtp.auth", "true"); //指定是否需要SMTP验证
props.setProperty("mail.smtp.host", smtpHost); //指定SMTP服务器
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(false); //是否在控制台显示debug信息
// Step 2: Construct the message
System.out.println("Constructing message - from=" + from + " to=" +to);
InternetAddress fromAddress = new InternetAddress(from);
InternetAddress toAddress = new InternetAddress(to);
MimeMessage testMessage = new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(subject);
//Step 3: Create a body part to hold the "text" portion of the message
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(messageText, "text/html;charset=gb2312");
// Step 4: Create a body part to hold the "file" portion of the message
//System.out.println("Attaching 'file' body part: " + fileName);
MimeBodyPart fileBodyPart = new MimeBodyPart();
if(fileName != null && !"".equals(fileName)){
FileDataSource fds = new FileDataSource("E:\\"+fileName);
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
}
///System.out.println("Finished attaching file");
// Step 5: Create a Multipart/container and add the parts
Multipart container = new MimeMultipart();
container.addBodyPart(textBodyPart);
if(fileName != null && !"".equals(fileName)){
container.addBodyPart(fileBodyPart);
}
// Step 6: Add the Multipart to the actual message
testMessage.setContent(container);
System.out.println("Message constructed");
System.out.println("邮件正在发送。。。");
// Step 7: Now send the message
Transport transport = mailSession.getTransport("smtp");
transport.connect(smtpHost, "youremail", "yourpassword");
transport.sendMessage(testMessage, testMessage.getAllRecipients());
transport.close();
System.out.println("邮件发送成功!已经将邮件发送到:"+to);
}
public static void main(String[] args) {
String fileName = "";//"a.rar";
String smtpHost = "smtp.163.com";
String from = "youremail@163.com"; //必须与transport.connect(smtpHost, "username1", "pwd1");的username1一样
//要发送到的邮件地址
String sendto = "10962104@qq.com";
//设置邮件的标题
String subject = "圣诞快乐。happy christmas!"; //subject javamail自动转码
//设置邮件内容
StringBuffer theMessage = new StringBuffer();
theMessage.append("圣诞快乐。。");
theMessage.append("圣诞快乐。");
//theMessage.append("---wwwhaolan123@163.com");
try {
for(int i=0;i<3;i++){
JavaMail2.sendMessage(smtpHost, from, sendto, subject,theMessage.toString(), fileName);
}
System.out.println("邮件发送完毕!");
} catch (javax.mail.MessagingException exc) {
System.out.println(exc);
exc.printStackTrace();
}
}
}
java mail开发所需jar包
5星 · 超过95%的资源 需积分: 10 176 浏览量
2010-12-15
17:43:14
上传
评论
收藏 379KB RAR 举报
新疆嘉博智选科技有限公司
- 粉丝: 88
- 资源: 18
最新资源
- 基于matlab实现多车辆车辆路径问题,用遗传算法编程,保证可用.rar
- 基于matlab实现多层极限学习机实现手写体识别,准确率超过99%.rar
- 基于matlab实现电力系统最优潮流程序,可以应用于电力市场下的最优潮流计算 适合电力系统专业的同仁使用.rar
- 串行通信的通信协议串口协议串行通信的通信协议串口协议
- Vue构建交互式的单页面应用程序Vue构建交互式的单页面应用程序
- Spring Boot快速搭建和部署应用程序
- Redis开源的高性键值存储系统广泛应用于缓存、实时消息传递
- Qt跨平台的应用程序开发框架Qt跨平台的应用程序开发框架
- 单片机是集成处理器、存储器和输入输出设备
- 深度学习机器习方法模仿人脑神经网络深度学习机器习方法模仿人脑神经网络
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈