javamail
package com.baizhi.springboot;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
class SendMailText {
public static String senderAddress = "17613726582@163.com";
public static String recipientAddress = "" +
"@163.com";
public static String senderAccount = "17613726582@163.com";
public static String senderPassword = "liuhuifang";
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.163.com");
Session session = Session.getInstance(props);
session.setDebug(true);
Message msg = getMimeMessage(session);
Transport transport = session.getTransport();
transport.connect(senderAccount, senderPassword);
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
}
public static MimeMessage getMimeMessage(Session session) throws Exception{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress));
msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(recipientAddress));
msg.setSubject("邮件主题","UTF-8");
msg.setContent("可以荐嘉客,奈何阻重深。\n" +
"运命惟所遇,循环不可寻。\n" +
"徒言树桃李,此木岂无阴?", "text/html;charset=UTF-8");
msg.setSentDate(new Date());
return msg;
}
}