這幾篇文章寫的就挺好了,傳送過去看看吧:html
一、 使用JavaMail建立郵件和發送郵件前端
可能遇到的問題:
一、由於端口號問題致使的錯誤:java
javax.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketTimeoutException: Read timed out javax.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketTimeoutException: Read timed out at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124)
問題和解決這裏能夠看到,把port configuration from 465 to 587(把端口從465改爲587)
https://stackoverflow.com/que...python
要在郵件中包含圖片簡單辦法是使用image標籤,src指向服務器上圖片的位置。spring
package com.example.emaildemo; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * @program: email-demo * @description: * @author: smallsoup * @create: 2019-01-27 16:44 **/ public class SendEmailUtil { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.exmail.qq.com"); props.setProperty("mail.smtp.auth", "true"); //使用JavaMail發送郵件的5個步驟 //一、建立session Session mailSession = Session.getInstance(props); //開啓Session的debug模式,這樣就能夠查看到程序發送Email的運行狀態 mailSession.setDebug(true); //二、經過session獲得transport對象 Transport transport = mailSession.getTransport(); //三、使用郵箱的用戶名和密碼連上郵件服務器,這裏有多個構造器,可傳入host、端口、user、password transport.connect( "你的郵箱地址", "你的郵箱AUTH密碼,不是登錄密碼哦,在郵箱的設置裏單獨開啓和設置"); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail Hello"); message.setFrom(new InternetAddress("你的郵箱地址")); //四、建立郵件 message.setContent("<h1>This is a test</h1>" + "<img src=\"http://www.rgagnon.com/images/jht.gif\">", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("接收人郵箱地址")); //五、發送郵件 // transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } }
上面發送帶圖片郵件的方法很簡單,可是有些郵件客戶端會把是否包含有服務器端圖片做爲垃圾郵件的判斷機制。咱們能夠將圖片內嵌到郵件中,而後用cid加content-id引用內嵌的圖片。編程
package com.example.emaildemo; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; 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 java.util.Properties; /** * @program: email-demo * @description: * @author: smallsoup * @create: 2019-01-27 16:44 **/ public class SendEmailUtil { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.exmail.qq.com"); props.setProperty("mail.smtp.auth", "true"); //使用JavaMail發送郵件的5個步驟 //一、建立session Session mailSession = Session.getInstance(props); //開啓Session的debug模式,這樣就能夠查看到程序發送Email的運行狀態 mailSession.setDebug(true); //二、經過session獲得transport對象 Transport transport = mailSession.getTransport(); //三、使用郵箱的用戶名和密碼連上郵件服務器,這裏有多個構造器,可傳入host、端口、user、password transport.connect( "你的郵箱地址", "你的郵箱AUTH密碼,不是登錄密碼哦,在郵箱的設置裏單獨開啓和設置"); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail Hello"); message.setFrom(new InternetAddress("你的郵箱地址")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("接收人郵箱地址")); //四、建立郵件 //This HTML mail have to 2 part, the BODY and the embedded image MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource("C:\\images\\jht.gif"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID","image"); // add it multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); //五、發送郵件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } }
SpringBoot發送郵件須要加依賴:api
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
具體可參考:
java發送html模板的高逼格郵件服務器
本公衆號免費提供csdn下載服務,海量IT學習資源,若是你準備入IT坑,勵志成爲優秀的程序猿,那麼這些資源很適合你,包括但不限於java、go、python、springcloud、elk、嵌入式 、大數據、面試資料、前端 等資源。同時咱們組建了一個技術交流羣,裏面有不少大佬,會不定時分享技術文章,若是你想來一塊兒學習提升,能夠公衆號後臺回覆【2】,免費邀請加技術交流羣互相學習提升,會不按期分享編程IT相關資源。session
掃碼關注,精彩內容第一時間推給你