十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要講解了Java如何實(shí)現(xiàn)帶附件的郵件發(fā)送功能,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
成都創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、豐滿網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為豐滿等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
1、需要導(dǎo)入mail.jar、activation.jar這兩個(gè)郵件發(fā)送的jar包,可在網(wǎng)上搜索并下載
2、需要設(shè)置相關(guān)郵箱服務(wù)器,我用的是QQ郵箱,操作如下所示:開啟相關(guān)服務(wù),并生產(chǎn)授權(quán)碼(這個(gè)代碼中會(huì)用到)。

代碼如下所示:
package com.ecg.controller;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import com.sun.mail.util.MailSSLSocketFactory;
/**
* 郵件發(fā)送
*
* @author wanglongfei
* E-mail: islongfei@gmail.com
* @version 2017年8月27日
*
*/
public class mailtest {
public static void main(String [] args) throws GeneralSecurityException, UnsupportedEncodingException
{
// 收件人電子郵箱
String to = "2528621082@qq.com";
// 發(fā)件人電子郵箱
String from = "1135237317@qq.com";
// 指定發(fā)送郵件的主機(jī)為 smtp.qq.com
String host = "smtp.qq.com"; //QQ 郵件服務(wù)器
// 獲取系統(tǒng)屬性
Properties properties = System.getProperties();
// 設(shè)置郵件服務(wù)器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 獲取默認(rèn)session對(duì)象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{ //qq郵箱服務(wù)器賬戶、第三方登錄授權(quán)碼
return new PasswordAuthentication("1135237317@qq.com", "dgahhkkjrtgafejj"); //發(fā)件人郵件用戶名、密碼
}
});
try{
// 創(chuàng)建默認(rèn)的 MimeMessage 對(duì)象
MimeMessage message = new MimeMessage(session);
// Set From: 頭部頭字段
message.setFrom(new InternetAddress(from));
// Set To: 頭部頭字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: 主題文字
message.setSubject("家醫(yī)康心電診斷結(jié)果");
// 創(chuàng)建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("233333333333333");
// 創(chuàng)建多重消息
Multipart multipart = new MimeMultipart();
// 設(shè)置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
//設(shè)置要發(fā)送附件的文件路徑
String filename = "C:/Users/下雨天-lalala/Desktop/家醫(yī)康心電圖/十二導(dǎo)聯(lián)同步心電圖-.png";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
//messageBodyPart.setFileName(filename);
//處理附件名稱中文(附帶文件路徑)亂碼問題
messageBodyPart.setFileName(MimeUtility.encodeText(filename));
multipart.addBodyPart(messageBodyPart);
// 發(fā)送完整消息
message.setContent(multipart );
// 發(fā)送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
} 看完上述內(nèi)容,是不是對(duì)Java如何實(shí)現(xiàn)帶附件的郵件發(fā)送功能有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。