javamail邮件发送例子
public class EmailTask
{
// Session used by the javamail classes
private Session session;
// List of messages邮件发送信息对象列表
private List<Message> messages = null;
/**构造方法
* Creates a new EmailTask.
*/
public EmailTask()
{
//邮件发送入参Properties,用来设置参数
Properties mailProps = new Properties();
//邮件服务器地址:域名或ip
String host = "smtpscn.test.com";
//邮件服务器端口,默认25
String port = "25";
//是否开启TLS方式发送,1 是,0 否.默认否
String startTLS = "0";
//是否验证证书,1 是,0 否.默认是
String cert = "0";
//是否鉴权true false
String isAuth = "true";
//鉴权账号
final String account = "zWX161496";
//鉴权密码
final String pwd = "Asd123";
if (host != null && !"".equals(host))
{
mailProps.setProperty("mail.smtp.host", host);
}
if (port != null && !"".equals(port))
{
mailProps.setProperty("mail.smtp.port", port);
}
//如果开始TLS方式
if ("1".equals(startTLS))
{
mailProps.setProperty("mail.smtp.starttls.enable", "true");
//是否验证证书
if ("0".equals(cert))
{
mailProps.setProperty("mail.smtp.ssl.trust", "*");
}
}
//是否鉴权:true false
mailProps.setProperty("mail.smtp.auth", isAuth);
// Create the mail session, check authenticator
session = Session.getInstance(mailProps, new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(account, pwd);
}
});
messages = new LinkedList();
}
/**发送一批邮件
* 如果调用该方法,将不会通过单独起一个线程的方式来发送邮件
* 好处是如果发送失败可以立即返回结果
* 缺点是不适合频率较高的操作
*/
public void sendMessage()
{
try
{
Iterator<Message> messageIterator = messages.iterator();
while (messageIterator.hasNext())
{
Message message = messageIterator.next();
Transport.send(message);
}
}
catch (MessagingException me)
{
//
}
catch (Exception e)
{
//
}
}
/**
* 构造Message
* @param toName the name of the recipient of this email.
* @param toEmail the email address of the recipient of this email.
* @param fromName the name of the sender of this email.
* @param fromEmail the email address of the sender of this email.
* @param subject the subject of the email.
* @param body the body of the email.
*/
public void addMessage(String toName, String toEmail, String fromName,
String fromEmail, String subject, String body)
{
if (toEmail == null || fromEmail == null || subject == null
|| body == null)
{
DebugLogFactory.debug(MailTmplTool.class,
"Error sending email in EmailTask.java: Invalid fields.");
}
else
{
try
{
Message message = createMessage();
Address to = null;
Address from = null;
if (toName != null)
{
to = new InternetAddress(toEmail, toName);
}
else
{
to = new InternetAddress(toEmail);
}
if (fromName != null)
{
from = new InternetAddress(fromEmail, fromName);
}
else
{
from = new InternetAddress(fromEmail);
}
message.setRecipient(Message.RecipientType.TO, to);
message.setFrom(from);
message.setSubject(subject);
message.setSentDate(new Date());
message.setHeader("Content-Transfer-Encoding", "BASE64");
message.setContent(body, "text/html;charset=UTF-8");
messages.add(message);
}
catch (Exception e)
{
//
}
}
}
/**
*
* @return A new JavaMail message.
*/
public Message createMessage()
{
return new MimeMessage(session);
}
}
补充:可以使用多线程发送发送,好处是不影响主线程,可以立即返回。
创建一个链表LinkedList(可包装为邮件发送工厂),用来存放每个邮件任务对象,启用几个线程如6个,每次发邮件时,将发邮件任务对象添加到LinkedList,注意枷锁,并通知所有线程激活notifyall;
当LinkedList有元素时,这些线程被激活,同时从LinkedList取出任务,注意加锁,本别发送;
当列表为空时,就挂起wait。