? //163郵箱發(fā)送郵件
public void sendEmail(OdsOaSendFormRecord sendFormRecord){
try {
//1.創(chuàng)建session會(huì)話
Session session = EmailUtils.createSession();
//2.創(chuàng)建郵件對(duì)象
MimeMessage msg = new MimeMessage(session);
? ? ? ? ? msg.setFrom(new InternetAddress("@163.com"));//自己的郵箱賬號(hào)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //要發(fā)送給其它人的郵箱地址
? ? ? ? ? ? ? ?msg.setRecipient(Message.RecipientType.TO,new InternetAddress("@163.com");
msg.setSubject("測試發(fā)送郵件");//郵件標(biāo)題
//正文
MimeBodyPart textPart = new MimeBodyPart();
StringBuilder buf = new StringBuilder();
buf.append("郵件正文內(nèi)容");
textPart.setContent(buf.toString(),"text/html;charset=utf-8");
//正文加附件(如有附件的話可單獨(dú)設(shè)置)
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
//設(shè)置郵件內(nèi)容
msg.setContent(multipart);
//發(fā)送郵件
Transport.send(msg);
}
} catch (
MessagingException e) {
e.printStackTrace();
}
}
//創(chuàng)建session會(huì)話工具類
public class EmailUtils {
public static String username="1111111111@163.com";//自己的郵箱賬號(hào)
public static String password="";//需要自己去開啟郵箱的授權(quán)密碼,具體看后面圖片
public static String stmp="smtp.163.com";//默認(rèn)163郵箱地址
public static Session createSession(){
Properties props = new Properties();
props.put("mail.smtp.port", "25");//主機(jī)端口號(hào)
props.put("mail.smtp.host", stmp);//STMP主機(jī)名
props.put("mail.smtp.auth", "true");//是否開啟用戶認(rèn)證
props.put("mail.smtp.ssl", "true");//啟用加密
props.put("mail.smtp.ssl.trust", stmp);
props.put("mail.transport.protocol", "smtp");
? ? ? ? //創(chuàng)建session
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//設(shè)置發(fā)件人的賬號(hào)、密碼
return new PasswordAuthentication(username, password);
}
});
//開啟debug終端
session.setDebug(true);
return session;
}
}
注意:記得保存開啟后的客戶端授權(quán)碼,否則還需關(guān)閉重新開啟一次
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧