常用郵件服務商的SMTP信息:
? ? QQ郵A箱:SMTP服務器是smtp.qq.com,端口是465/587
? ? 163郵箱:SMTP服務器是smtp.163.com,端口是465
? ? Gmail郵箱:SMTP服務器是smtp.gmail.com,端口是465/587
????????通過JavaMail API連接到SMTP服務器上,連接SMTP服務器時,需要準備一個Properties對象,填入相關信息。
//SMTP服務器連接信息
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");//SMTP主機名
props.put("mail.smtp.port", "25");//主機端口號
//是否需要認證
props.put("mail.smtp.auth", "true");
//啟用TLS加密
props.put("mail.smtp.starttls.enable", "true");
????????最后獲取Session實例時,如果服務器需要認證,還需要傳入一個Authenticator對象,并返回指定的用戶名和口令。當我們獲取到Session實例后,打開調試模式可以看到SMTP通信的詳細內容,便于調試。
String userName = "m18091491321@163.com";
String password = "********";
//SMTP服務器連接信息
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");//SMTP主機名
props.put("mail.smtp.port", "25");//主機端口號
props.put("mail.smtp.auth", "true");
//是否需要認證
props.put("mail.smtp.starttls.enable", "true");
//啟用TLS加密
//創(chuàng)建Session會話
//參數1:smtp服務器連接參數
//參數2:賬號和密碼的授權認證對象
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO 自動生成的方法存根
return new PasswordAuthentication(userName, password);
}
});
session.setDebug(true);
System.out.println(session);
? ? ? ?發(fā)送郵件時,我們需要構造一個Message對象,然后調用Transport.send(Message)即可完成發(fā)送:絕大多數郵件服務器要求發(fā)送方地址和登錄用戶名必須一致,否則發(fā)送將失敗.
? ? ? ? 1.創(chuàng)建Session對象
? ? ? ? 2.創(chuàng)建郵件對象
? ? ? ? 3.發(fā)送郵件
????????文章來源:http://www.zghlxwxcb.cn/news/detail-554433.html
//2.創(chuàng)建郵件對象
MimeMessage message = new MimeMessage(session);
//設置文件主題
message.setSubject("測試郵件");
//設置郵件發(fā)送方賬號
message.setFrom(new InternetAddress("m18091491321@163.com"));
//設置郵件接收方
//參數1:接受
//參數2:接收方賬號
message.setRecipient(RecipientType.TO, new InternetAddress("1417547535@qq.com"));
//抄送給多個賬號
//參數1:抄送
//參數2:賬戶
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("1417547535@qq.com"),new InternetAddress("1252303367@qq.com")});
//郵件僅包含正文
message.setText("好好學習天天向上");
//3.發(fā)送
Transport.send(message);
?如果郵件內容既包含正文,又包含附件。可以構造一個Multipart對象。
//郵件既包含正文,又包含附件
//正文
BodyPart textPart = new MimeBodyPart();
textPart.setContent("<h3>我希望你</h3>","text/html;charset=utf-8");
//附件
BodyPart filePart = new MimeBodyPart();
filePart.setDataHandler(
new DataHandler(
new ByteArrayDataSource(Files.readAllBytes(Paths.get("D:\\niuma\\bao.jpg")) , "application/octet-stream")));
//將正文+附件組裝成Multipart對象
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(filePart);
//將Multipart對象放入郵件
message.setContent(multipart);
一個Multipart對象可以添加若干個BodyPart,其中第一個BodyPart是文本,即郵件正文,后面的BodyPart是附件。BodyPart依靠setContent()決定添加的內容,如果添加文本,用setContent("...", "text/plain;charset=utf-8")添加純文本,或者用setContent("...", "text/html;charset=utf-8")添加HTML文本。如果添加附件,需要設置文件名(不一定和真實文件名一致),并且添加一個DataHandler(),傳入文件的MIME類型。二進制文件可以用application/octet-stream,Word文檔則是application/msword。
最后,通過setContent()把Multipart添加到Message中,即可發(fā)送。
發(fā)送內嵌圖片的HTML郵件
????????如果需要在HTML郵件中內嵌圖片,可以選擇在郵件中加入<img src="http://example.com/test.jpg">,這樣的外部圖片鏈接通常會被郵件客戶端過濾,并提示用戶顯示圖片并不安全。只有內嵌的圖片才能正常在郵件中顯示。所以,這種方式并不推薦。
推薦將內嵌圖片作為一個附件嵌入郵件,即郵件本身也是Multipart,但需要做一點額外的處理:
????????
//郵件內容
BodyPart textpart = new MimeBodyPart();
StringBuilder sb = new StringBuilder();
sb.append("<h3>Hello</h3>");
sb.append("<img src=\"cid:bgls\"/>");
textpart.setContent(sb.toString(), "text/html;charset=utf-8");
//附件
BodyPart filepart = new MimeBodyPart();
filepart.setDataHandler(new DataHandler(
new ByteArrayDataSource(Files.readAllBytes(Paths.get("D:\\niuma\\bao2.jpg")),"application/octet-stream")));
filepart.setHeader("Content-ID", "bgls");//照片ID
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textpart);
multipart.addBodyPart(filepart);
message.setContent(multipart);
小結:
使用JavaMail API發(fā)送郵件本質上是一個MUA軟件通過SMTP協(xié)議發(fā)送郵件至MTA服務器
打開調試模式可以看到詳細的SMTP交互信息
某些郵件服務商需要開啟SMTP,并需要獨立的SMTP登錄密碼文章來源地址http://www.zghlxwxcb.cn/news/detail-554433.html
到了這里,關于JavaMail相關代碼案例(郵件發(fā)送)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!