Java实现多线程邮件发送
- 格式:pdf
- 大小:93.62 KB
- 文档页数:7
Java实现多线程邮件发送
利⽤java多线程技术配合线程池实现多任务邮件发送.
1.基本邮件发送MailSender
package hk.buttonwood.ops.email;
import java.io.File;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 简单邮件(带附件的邮件)发送器
*/
public class MailSender {
private Logger logger = LoggerFactory.getLogger(MailSender.class);
public static void main(String[] args) {
// 这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("15138666597");
mailInfo.setPassword("myb1991517");// 您的邮箱密码
mailInfo.setFromAddress("15138666597@163.com");
mailInfo.setToAddress("930147677@qq.com");
mailInfo.setSubject("windowfg");
mailInfo.setContent("asgjhkhl");
// 这个类主要来发送邮件
MailSender sms = new MailSender();
Vector files = new Vector();
files.addElement(new File("/home/maybo/myProject/2015-11-04/mt_2015-11-04_2.pdf"));
mailInfo.setFile(files);
sms.sendWithAttachment(mailInfo);// 发送⽂体格式
// sms.sendHtmlMail(mailInfo);//发送html格式
}
/**
* 发送邮件
*/
public MailState sendWithAttachment(MailSenderInfo info) {
MailState mailState=new MailState();
mailState.setDate(info.getDate());
mailState.setState(MailState.SUCCESS);
mailState.setDesc("邮箱发送成功!");
mailState.setAddress(info.getToAddress());
Session session = null;
Properties props = System.getProperties();
props.put("mail.smtp.host", info.getMailServerHost());
if (info.isValidate()) { // 服务器需要⾝份认证
props.put("mail.smtp.auth", "true");
MyAuthenticator smtpAuth = new MyAuthenticator(info.getUserName(), info.getPassword());
session = Session.getDefaultInstance(props, smtpAuth);
} else {
props.put("mail.smtp.auth", "false");
session = Session.getDefaultInstance(props, null);
}
//session.setDebug(true);
Transport trans = null;
try {
Message msg = new MimeMessage(session);
try { Address from_address = new InternetAddress(info.getFromAddress(), info.getUserName());
msg.setFrom(from_address);
} catch (java.io.UnsupportedEncodingException e) {
//e.printStackTrace();
mailState.setState(MailState.ERROR);
String message = "邮件发送失败!";
mailState.setDesc(message);
}
InternetAddress[] address = { new InternetAddress(info.getToAddress()) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(info.getSubject());
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(info.getContent().toString(), "text/html;charset=gb2312");
mp.addBodyPart(mbp);
if (!info.getFile().isEmpty()) {// 有附件
Enumeration efile = info.getFile().elements();
while (efile.hasMoreElements()) {
mbp = new MimeBodyPart();
String filename = efile.nextElement().toString(); // 选择出每⼀个附件名
FileDataSource fds = new FileDataSource(filename); // 得到数据源
mbp.setDataHandler(new DataHandler(fds)); // 得到附件本⾝并⾄⼊BodyPart
mbp.setFileName(fds.getName()); // 得到⽂件名同样⾄⼊BodyPart
mp.addBodyPart(mbp);
}
info.getFile().removeAllElements();
}
msg.setContent(mp); // Multipart加⼊到信件
msg.setSentDate(new Date()); // 设置信件头的发送⽇期
// 发送信件
msg.saveChanges();
trans = session.getTransport("smtp");
trans.connect(info.getMailServerHost(), info.getUserName(), info.getPassword());
trans.sendMessage(msg, msg.getAllRecipients());
trans.close();
} catch (AuthenticationFailedException e) {
mailState.setState(MailState.ERROR);
String message = "邮件发送失败!错误原因:\n" + "⾝份验证错误!";
mailState.setDesc(message);
// e.printStackTrace();
} catch (MessagingException e) {
//e.printStackTrace();
String message = "邮件发送失败!错误原因:" + e.getMessage();
mailState.setDesc(message);
mailState.setState(MailState.ERROR);
}
return mailState;
}
/**
* 以⽂本格式发送邮件
*
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要⾝份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要⾝份认证,则创建⼀个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造⼀个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// 根据session创建⼀个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);