public class SendEmail {
/**
*
* @param sendAddress 发送地址
* @param title 标题
* @param content 内容
* @throws Exception
*/
public static void sendEmailToUser(String sendAddress,String title,String content) throws Exception{
String username = "";
String password = "";
Properties properties = new Properties();
//设置服务器地址
properties.setProperty("mail.host", "smtp.163.com");
//设置邮件传输协议
properties.setProperty("mail.transport.protocol", "smtp");
//设置是否需要认证服务器的用户名和密码
properties.setProperty("mail.smtp.auth", "true");
//创建一个Session对象
Session session = Session.getInstance(properties);
//得到发送邮件的对象 transport
Transport transport = session.getTransport();
//连接服务器
transport.connect("smtp.163.com", username, password);
//创建message对象
Message message = createSimpleEmail(session,sendAddress,title,content);
//发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
private static Message createSimpleEmail(Session session, String sendAddress, String title, String content) throws Exception {
MimeMessage message = new MimeMessage(session);
//设置发件人
message.setFrom(new InternetAddress("budongil@163.com"));
//设置邮件的接收人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(sendAddress));
//设置邮件标题
message.setSubject(title);
//设置邮件内容
message.setContent(content, "text/html;charset=utf-8");
return message;
}
}