1.參考文獻
https://javaee.github.io/javamail/?xd_co_f=56d1d069-5c5e-41ff-bf54-df52d3d6ccb7
2.javax.mail.jar的下載下傳位址
https://github.com/javaee/javamail/releases
3.在eclipse中導入jar包
流程如下
一、在項目名上右擊,依次點選【New】-->【Floder】-->命名為lib
二、将需要導入的jar包從本地檔案系統複制或拖拽到lib檔案夾内,彈出的提示框點copy files ok
三、配置路徑
我們再在項目名上右擊,依次選擇【Build Path】-->【Configure Build Path...】。
先選中【Libraries】頁,再從右邊的按鈕中點選【add JARs...】
我們在剛才打開的【Libraries】頁中可以看到我們引入的jar包的名稱。點選【OK】确認。
二、Demo
packagecom.star.networktest;importjavax.mail.BodyPart;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;import javax.mail.internet.*;import java.util.*;import javax.activation.*;public classAccess {private String host = ""; //smtp伺服器
private String from = ""; //發件人位址
private String to = ""; //收件人位址
private String affix = ""; //附件位址
private String affixName = ""; //附件名稱
private String user = ""; //使用者名
private String pwd = ""; //密碼
private String subject = ""; //郵件标題
public voidsetAddress(String from, String to, String subject) {this.from =from;this.to =to;this.subject =subject;
}public voidsetAffix(String affix, String affixName) {this.affix =affix;this.affixName =affixName;
}public voidsend(String host, String user, String pwd) {this.host =host;this.user =user;this.pwd =pwd;
Properties props= newProperties();//設定發送郵件的郵件伺服器的屬性(這裡使用網易的smtp伺服器)
props.put("mail.smtp.host", host);//需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證(一定要有這一條)
props.put("mail.smtp.auth", "true");//用剛剛設定好的props對象建構一個session
Session session =Session.getDefaultInstance(props);//有了這句便可以在發送郵件的過程中在console處顯示過程資訊,供調試使//用(你可以在控制台(console)上看到發送郵件的過程)
session.setDebug(true);//用session為參數定義消息對象
MimeMessage message = newMimeMessage(session);try{//加載發件人位址
message.setFrom(newInternetAddress(from));//加載收件人位址
message.addRecipient(Message.RecipientType.TO, newInternetAddress(to));//加載标題
message.setSubject(subject);//向multipart對象中添加郵件的各個部分内容,包括文本内容和附件
Multipart multipart = newMimeMultipart();//設定郵件的文本内容
BodyPart contentPart = newMimeBodyPart();
contentPart.setText("郵件的具體内容在此");
multipart.addBodyPart(contentPart);//添加附件
BodyPart messageBodyPart = newMimeBodyPart();
DataSource source= newFileDataSource(affix);//添加附件的内容
messageBodyPart.setDataHandler(newDataHandler(source));//添加附件的标題//這裡很重要,通過下面的Base64編碼的轉換可以保證你的中文附件标題名在發送時不會變成亂碼//報錯參考https://jingyan.baidu.com/article/e73e26c0a2617a24adb6a7d4.html
sun.misc.BASE64Encoder enc = newsun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"
+ enc.encode(affixName.getBytes()) + "?=");
multipart.addBodyPart(messageBodyPart);//将multipart對象放到message中
message.setContent(multipart);//儲存郵件
message.saveChanges();//發送郵件
Transport transport = session.getTransport("smtp");//連接配接伺服器的郵箱
transport.connect(host, user, pwd);//把郵件發送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e) {
e.printStackTrace();
}
}public static voidmain(String[] args) {
Access cn= newAccess();//設定發件人位址、收件人位址和郵件标題
cn.setAddress("starz[email protected]", "[email protected]", "網絡連通狀态檢測報告");//設定要發送附件的位置和标題
cn.setAffix("G:/onlinelog.txt", "onlinelog.txt");cn.send("smtp.tom.com", "[email protected]", "自己的郵箱密碼");
}
}