throws AddressException, MessagingException {
// 1.創建一個程序與郵件服務器會話對象 Session
Properties props = new Properties();
props.setProperty(“mail.transport.protocol”, “SMTP”);
props.setProperty(“mail.host”, “smtp.163.com”);
props.setProperty(“mail.smtp.auth”, “true”);// 指定驗證為true
// 創建驗證器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“cui******@163.com”, “12313456”);//注冊郵箱的帳號和授權碼
}
};
Session session = Session.getInstance(props, auth);
// 2.創建一個Message,它相當于是郵件內容
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(“**********@163.com”,”8896″));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 設置發送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設置發送方式與接收者
message.setSubject(“用戶激活”);
// message.setText(“這是一封激活郵件,請<a href=\\\’#\\\’>點擊</a>”);
message.setContent(emailMsg, “text/html;charset=utf-8″);
// 3.創建 Transport用于將郵件發送
Transport.send(message);
}
}
這里我用的是網易郵箱,郵箱需要設置開通smtp協議,得到授權碼
測試類
public class Test { public static void main(String[] args) { try {
String emailMsg=”這是一封激活郵件”; MailUtils.sendMail(驗證的郵箱, emailMsg); System.out.println(“郵件發送成功!”); } catch (MessagingException e) { e.printStackTrace(); } } }
如果,你對上面的內容還有疑問,推薦選擇三五互聯企業云郵箱!有專人協助您解答郵箱疑問。
三五互聯是專業企業郵箱的官方正規提供商,21年行業經驗,提供安全穩定,簡單易用,高性價比的企業郵箱,按需自由定制,不限空間,極速收發,能夠滿足用戶對企業郵箱的不同需求??梢酝ㄟ^以下幾種方式注冊、申請、購買、試用、開通企業郵箱:
1、登錄http://m.shinetop.cn/services/mail/在線咨詢申請試用或購買;
2、直接致電028-62778877申請試用或正式購買開通;