這裏使用Transport靜態方法發送郵件,不可以在發送時臨時設置發送人的用戶名密碼,這要移到 Session.getInstance 參數中。同時smtp服務器的地址也沒有設置,在props中設置。
public static void main(String[] args) throws Exception {
Properties props=new Properties();
props.setProperty("mail.smtp.auth", "true"); //表明會通過用戶名密碼驗證
props.setProperty("mail.transport.protocol", "smtp");//設置傳輸協議。
props.setProperty("mail.host", "smtp.aliyun.com"); //設置發送smtp服務器的地址
Session session=Session.getInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "password");
}
}
);//每次都返回一個新的對象
// Session.getDefaultInstance()每次都返回相同的對象,不推薦,或許發郵件和收郵件會有影響
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));//設置發送人
msg.setSubject("中文主題"); //設置主題
msg.setContent("<a href=\"http://www.baidu.com\">點我</a>","text/html;charset=utf-8");
msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));//這樣寫會使每個受件者看到所有收件人
Transport.send(msg);
}
由於我們在文件內容中可能有特殊的數據,我們可以把數據進行編碼。
常用編碼方式 base64 quote-printable
若是內容大部分爲中文,使用base64編碼,若內容大部分爲英文,則使用 quote-printable .可以給郵件中添加附件,在郵件內容中添加圖片,下面代碼把郵件另存在桌面上
public static void main(String[] args) throws Exception {
Session session=Session.getInstance(new Properties());
MimeMessage msg=new MimeMessage(session);
Multipart multipart=new MimeMultipart("mixed");
msg.setContent(multipart);
msg.setSubject("挑戰書");
msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("萬豪")+"\""+" <[email protected]>"));
msg.setReplyTo(new Address[]{new InternetAddress("<[email protected]>")});
//顯示友好名稱,編碼是爲了使漢字正確顯示
msg.setRecipient(RecipientType.TO, new InternetAddress(MimeUtility.encodeText("張三 <[email protected]>")));
MimeBodyPart content=new MimeBodyPart();
MimeBodyPart attch1=new MimeBodyPart();
MimeBodyPart attch2=new MimeBodyPart();
multipart.addBodyPart(attch2);
multipart.addBodyPart(attch1);
multipart.addBodyPart(content);
DataSource ds=new FileDataSource("C:\\Users\\Administrator\\Desktop\\第一個附件.txt");
DataHandler dataHandler1=new DataHandler(ds);
attch1.setDataHandler(dataHandler1);
attch1.setFileName(MimeUtility.encodeText("附件1.txt"));
DataSource ds2=new FileDataSource("C:\\Users\\Administrator\\Pictures\\a.jpg");
DataHandler dataHandler2=new DataHandler(ds2);
attch2.setDataHandler(dataHandler2);
attch2.setFileName("a.jpg");
Multipart bodymp=new MimeMultipart("related");
content.setContent(bodymp);
MimeBodyPart htmlPart=new MimeBodyPart();
MimeBodyPart gifPart=new MimeBodyPart();
bodymp.addBodyPart(gifPart);
bodymp.addBodyPart(htmlPart);
DataSource gifds=new FileDataSource("resource\\logo.gif");
DataHandler gifdh=new DataHandler(gifds);
gifPart.setDataHandler(gifdh);
gifPart.setHeader("Content-Location", "http://www.ecfun.cn/images/user-img.png");//①
htmlPart.setContent("<img src='http://www.ecfun.cn/images/user-img.png'>你真的很厲害嗎?大家都這樣說,我想和你比試一下","text/html;charset=utf-8");
msg.saveChanges();
OutputStream os=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.eml");
msg.writeTo(os);
os.close();
}
在①中一般要把 gifPart 中的鏈接對應的圖片,與FileDataSource();中的圖片保持一致。因爲保存在本地顯示的是DataSource中關聯的文件對應的圖片,而發送過去的郵件中卻顯示鏈接對應的圖片。
接下來便可以把郵件發送給某個用戶了。
public static void main(String[] args) throws Exception {
Properties props=new Properties();
props.setProperty("mail.smtp.auth", "true"); //表明會通過用戶名密碼驗證
props.setProperty("mail.transport.protocol", "smtp");//設置傳輸協議。
props.setProperty("mail.host", "smtp.aliyun.com"); //設置發送smtp服務器的地址
Session session=Session.getInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
);//每次都返回一個新的對象
// Session.getDefaultInstance()每次都返回相同的對象,不推薦,或許發郵件和收郵件會有影響
FileInputStream fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.eml");
Message msg=new MimeMessage(session,fis);
//收件地址要與 原來文件中msg.setRecipient中地址一致
Transport.send(msg,new Address[]{new InternetAddress("[email protected]")});
}