EmailService.porperties
#----------------这两个是构建session必须的字段----------
#smtp服务器
#mail.smtp.host=smtp.qq.com
mail.smtp.host=smtp.163.com
#mail.smtp.protocol=smtp
#mail.smtp.prot=25
#身份验证
mail.smtp.auth=true
#--------------------------------------------------------------
#发送者的邮箱用户名
mail.sender.username=XXXXXX@163.com
#发送者的邮箱密码
mail.sender.password=password
package com.sdty.util;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sdty.viewModel.MailInfo;
/******************************************************************************
* @Package: [com.sdty.util.MailSendUtils.java]
* @ClassName: [MailSendUtils]
* @Description: []
* @Author: [hhy.huo]
* @CreateDate: [2015-03-08]
* @UpdateDate: [[2015-03-08] ]
* @Version: [v1.0]
*/
public class MailSendUtils {
/***
* 邮件是既可以被发送也可以被受到。JavaMail使用了两个不同的类来完成这两个功能:Transport 和 Store。
* Transport 是用来发送信息的,而Store用来收信。对于这的教程我们只需要用到Transport对象。
*/
private static Properties properties=new Properties();
public static void sendHtmlMail(MailInfo info)throws Exception{
Message message = getMessage(info);
message.setContent(info.getContent(), "text/html;charset=utf-8");
Transport.send(message);
}
public static void sendTextMail(MailInfo info)throws Exception{
Message message = getMessage(info);
message.setText(info.getContent());
Transport.send(message);
}
private static Message getMessage(MailInfo info) throws Exception{
/*final Properties p = System.getProperties() ;
p.setProperty("mail.smtp.host", info.getHost());
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.smtp.user", info.getSenderName());
p.setProperty("mail.smtp.pass", info.getSenderPassword());*/
InputStream in=null;
in=MailSendUtil.class.getResourceAsStream("mailServer.properties");
properties.load(in);
Session session = Session.getInstance(properties, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(properties.getProperty("mail.sender.username"),properties.getProperty("mail.sender.password"));
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
message.setSubject(info.getSubject());
//message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
message.setReplyTo(InternetAddress.parse(properties.getProperty("mail.sender.username")));
message.setFrom(new InternetAddress(properties.getProperty("mail.sender.username"),"网站管理员"));
message.setRecipient(RecipientType.TO,new InternetAddress(info.getToAddress()));
return message ;
}
public static void main(String[] args) throws Exception{
MailInfo info = new MailInfo();
/*info.setHost("smtp.sina.com");
info.setFormName("xxxxx@sina.com");
info.setFormPassword("xxxxxxx");
info.setReplayAddress("xxxx@sina.com");
info.setToAddress("xxxx@qq.com");
info.setSubject("bbs测试邮件");
info.setContent("这是一封测试邮件");
sendTextMail(info);*/
info.setHost("smtp.163.com");
info.setSenderName("xxxx@163.com");
info.setSenderPassword("songdong");
info.setReplayAddress("xxxx@163.com");
info.setToAddress("xxx@qq.com");
info.setSubject("bbs测试邮件");
info.setContent("这是一封测试邮件");
sendTextMail(info);
}
}
浙公网安备 33010602011771号