發送郵件的主要步驟html
1.設置發送的協議,也就是設置smtp和驗證機制(通常協議都是經過Properties鍵值形式來設置)java
2.發送郵件須要的幾個重要類Session ,Message,Transport服務器
3.Session對象能夠經過Session的getInstance(java.util.Properties props)session
或getInstance(java.util.Properties props, Authenticator authenticator) Authenticator 能夠理解爲密碼和用戶名的驗證器ide
或getDefaultInstance(java.util.Properties props)函數
或getDefaultInstance(java.util.Properties props, Authenticator authenticator)ui
4.郵件的一些重要內容都是經過Message設置,比例內容,主題,接收人...net
接收人能夠經過Message的setRecipients(Message.RecipientType type,Address address)debug
Message.RecipientType 有三個重要屬性 1.BCC(密送),2.CC(抄送) ,3.TO(收件人)code
5.能夠經過Transport.send(Message msg)發送郵件
通常發送簡單的郵件上面幾個步驟就能夠實現了,可是要發送一封複雜的郵件(有附件和文本,圖片)還須要如下步驟
A.發送複雜郵件須要這個兩個重要類MimeBodyPart 和 MimeMultipart
先要搞清楚文本和圖片的關係是related,而文本和圖片再加上附件的關係就是mixed
B.文本用一個MimeBodyPart對象保存
C.圖片也是用一個MimeBodyPart對象保存
D.用一個MimeMultipart A對象來保存,保存文本的MimeBodyPart對象和保存圖片的MimeBodyPart對象
這個MimeMultipart對象 經過構造函數 new MimeMultipart("related")獲取
E.附件也是用一個MimeBodyPart C對象保存
F.用一個總MimeBodyPart B對象來保存MimeMultipart A對象
G.還得用一個總的MimeMultipart all對象來保存MimeBodyPart C對象和MimeBodyPart B對象
這個all對象經過構造函數 new MimeMultipart("mixed")獲取
H.最後用Message.setContent(Multipart mp)方法保存總的郵件
I.能夠經過Transport.send(Message msg)發送郵件
如今發送一份簡單的郵件,郵件內容只含有一些文字
Code:
package com.michael.email;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// @author Michael.Wu
//JavaMail 發送郵件
public class JavaEmail {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");//發送郵件協議
properties.setProperty("mail.smtp.auth", "true");//須要驗證
// properties.setProperty("mail.debug", "true");//設置debug模式 後臺輸出郵件發送的過程
Session session = Session.getInstance(properties);
session.setDebug(true);//debug模式
//郵件信息
Message messgae = new MimeMessage(session);
messgae.setFrom(new InternetAddress("whyao@sina.cn"));//設置發送人
messgae.setText("what's up man");//設置郵件內容
messgae.setSubject("哥們該吃飯了");//設置郵件主題
//發送郵件
Transport tran = session.getTransport();
// tran.connect("smtp.sohu.com", 25, "wuhuiyao@sohu.com", "xxxx");//鏈接到新浪郵箱服務器
tran.connect("smtp.sina.com", 25, "whyao@sina.cn", "xxxxxxx");//鏈接到新浪郵箱服務器
// tran.connect("smtp.qq.com", 25, "Michael8@qq.vip.com", "xxxx");//鏈接到QQ郵箱服務器
tran.sendMessage(messgae, new Address[]{ new InternetAddress("Michael8@qq.vip.com")});//設置郵件接收人
tran.close();
}
}
====================================================================================
把郵件的用戶名和密碼改成大家本身的,就能夠發送郵件了。有一個地方得注意,設置發送人和和你當前 發送郵件用的帳號要相同,否則會報異常或會收不到郵件。之前發送人和發送郵件的帳號是能夠不一樣的現 在就不行,多是郵箱服務商不容許的緣由吧。
接下來發送一份複雜的郵件(含有附件和圖片,文本)
Code:
package com.michael.email;
import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//@author Michael.wu
// 發送複雜的郵件(文本內容,附件,圖片)
public class JavaEmail3 {
public static void main(String[] args) throws MessagingException {
//發送郵件的協議
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth","true");//設置驗證機制
properties.setProperty("mail.transport.protocol","smtp");//發送郵件協議
properties.setProperty("mail.smtp.host","smtp.sina.com");//設置郵箱服務器地址
properties.setProperty("mail.smtp.port","25");
Session session = Session.getInstance(properties,new MyAuthenticator());
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("whyao@sina.cn"));
message.setSubject("一封複雜的郵件");
message.setRecipients(RecipientType.TO,InternetAddress.parse("michael8@vip.qq.com"));//接收人
message.setRecipients(RecipientType.CC,InternetAddress.parse("1348800595@qq.com"));//抄送人
message.setRecipients(RecipientType.BCC,InternetAddress.parse("1348800595@qq.com"));//密送人
MimeBodyPart bodyPartAttch = createAttachMent("C:\\Users\\Administrator\\Desktop\\mail.jar");//附件
MimeBodyPart bodyPartContentAndPic = createContentAndPic("I just want to Fuck","C:\\Users\\Administrator\\Desktop\\0.jpg");//文本內容
MimeMultipart mimeMuti = new MimeMultipart("mixed");
mimeMuti.addBodyPart(bodyPartAttch);
mimeMuti.addBodyPart(bodyPartContentAndPic);
message.setContent(mimeMuti);
message.saveChanges();
//message.setContent("Michael", "text/html;charset=gbk");
Transport.send(message);
}
//建立附件
public static MimeBodyPart createAttachMent(String path) throws MessagingException{
MimeBodyPart mimeBodyPart = new MimeBodyPart();
FileDataSource dataSource = new FileDataSource( new File(path));
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
mimeBodyPart.setFileName(dataSource.getName());
return mimeBodyPart;
}
//建立文本和圖片
public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException{
MimeMultipart mimeMutiPart = new MimeMultipart("related");
//圖片
MimeBodyPart picBodyPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource( new File(path));
picBodyPart.setDataHandler(new DataHandler(fileDataSource));
picBodyPart.setFileName(fileDataSource.getName());
mimeMutiPart.addBodyPart(picBodyPart);
//文本
MimeBodyPart contentBodyPart = new MimeBodyPart();
contentBodyPart.setContent(content,"text/html;charset=gbk");
mimeMutiPart.addBodyPart(contentBodyPart);
//圖片和文本結合
MimeBodyPart allBodyPart = new MimeBodyPart();
allBodyPart.setContent(mimeMutiPart);
return allBodyPart;
}
}
Code:
package com.michael.email;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
private static final String userName = "whyao@sina.cn";
private static final String passWord = "xxxxxxx";
// * @author Michael.wu
//* 密碼和用戶的驗證
public MyAuthenticator() {
super();
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, passWord);
}
}
//注意上面經過Main方法發送出去的郵件,圖片會以附件的形式顯示。若是你想把圖片當作背景圖或者是想在文本中插一張圖片,還要把上面的createContentAndPic方法改改。
code:
//建立文本和圖片
public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException, UnsupportedEncodingException{
MimeMultipart mimeMutiPart = new MimeMultipart("related");
//圖片
MimeBodyPart picBodyPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource( new File(path));
picBodyPart.setDataHandler(new DataHandler(fileDataSource));
picBodyPart.setFileName(MimeUtility.encodeText("米克我的照"));//解決中文亂碼問題
picBodyPart.setHeader("Content-Location", "http://www.michael.com/mike.jpg");
//http://www.michael.com/mike.jpg這個路徑是後面文本圖片的路徑
//文本
MimeBodyPart contentBodyPart = new MimeBodyPart();
//img的src要和setHeader中設置的值同樣
contentBodyPart.setContent(content+"<img src='http://www.michael.com/mike.jpg'></img>","text/html;charset=gbk");
mimeMutiPart.addBodyPart(contentBodyPart);
mimeMutiPart.addBodyPart(picBodyPart);
//圖片和文本結合
MimeBodyPart allBodyPart = new MimeBodyPart();
allBodyPart.setContent(mimeMutiPart);
return allBodyPart;
}
//經過上面的程序發送出的郵件,在Foxmail中打開,圖片會在文本中正常顯示。而在QQ郵箱 新浪郵箱 打開這封郵件,圖片會以附件的形式顯示,在文本中顯示不出來。這究竟是什麼緣由,或是還要設置什麼。我尚未弄明白。但願看到這篇博客的網友,知道是什麼緣由的,還得請大家也告訴我一聲,謝謝。