效果圖:
相關代碼:
test1 [java] view plain copy package com.mail; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * com.mail.reference下面的是在網上找的,還沒來得級看 * Test 是本人寫的,並都測試成功發送了的 郵箱和密碼換成你自已的, * 可能會遇到一個問題, 就是要把你自已myeclipse的javaee.jar檔案中的mail包給刪掉 * 他們會衝突,自已到網上找一個具體的操作吧! * @author liangrui * */ public class Test1 { public static void main(String[] args) throws Exception { System.out.println("hello...."); Properties perps=new Properties(); //認證 perps.setProperty("mail.smtp.auth", "true"); //協議 perps.setProperty("mail.transport.protocol", "smtp"); Session session=Session.getInstance(perps); Message mes=new MimeMessage(session); //收件 內容 、 人 mes.setText(" hello worldddddddddddddddddd"); //設定寄件者地址 mes.setFrom(new InternetAddress("liangrui_1988@126.com")); //發件資訊 Transport transport=session.getTransport(); transport.connect("smtp.126.com",25,"liangrui_1988","******"); //收件者 transport.sendMessage(mes, new Address[]{new InternetAddress("liangrui_1988@sina.com")}); transport.close(); System.out.println("-----"); } }
test2
[java] view plain copy package com.mail; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class Test2 { /** * @param args * @throws MessagingException */ public static void main(String[] args) throws Exception { //郵件Properties設定============================================ Properties pro=new Properties(); //認證 pro.setProperty("mail.smtp.auth", "true"); //協議 pro.setProperty("mail.transport.protocol", "smtp"); //主機 指定伺服器 連接埠預設 pro.setProperty("mail.host", "smtp.sina.com"); //郵件Session設定============================================ Session session= Session.getInstance(pro, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { //使用者名稱密碼 return new PasswordAuthentication("liangrui_1988","******"); } }); //郵件資訊設定============================================ Message ms=new MimeMessage(session); //地址 寄件者 ms.setFrom(new InternetAddress("liangrui_1988@sina.com")); //主題 ms.setSubject("你好嗎。這是郵件主題"); //地址 收件者 ms.setRecipients(RecipientType.TO, InternetAddress.parse("liangrui_1988@126.com,liangrui_1988@sohu.com,liangrui_1988@sohu.com")); //內容 ms.setContent("<span style='color:red'>常值內容 獃獃呆呵呵</span>","text/html;charset=gbk"); //發射================================= Transport.send(ms); System.out.println("okkk--------------"); } }
test2222demo
[java] view plain copy package com.mail; import java.io.FileInputStream; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class Test2Demo { /** * @param args * @throws MessagingException */ public static void main(String[] args) throws Exception { //郵件Properties設定============================================ Properties pro=new Properties(); //認證 pro.setProperty("mail.smtp.auth", "true"); //協議 pro.setProperty("mail.transport.protocol", "smtp"); //主機 指定伺服器 連接埠預設 pro.setProperty("mail.host", "smtp.sina.com"); //郵件Session設定============================================ Session session= Session.getInstance(pro, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { //使用者名稱密碼 return new PasswordAuthentication("liangrui_1988","******"); } }); //郵件資訊設定============================================ /*Message ms=new MimeMessage(session); //地址 寄件者 ms.setFrom(new InternetAddress("liangrui_1988@sina.com")); //主題 ms.setSubject("你好嗎。這是郵件主題"); //地址 收件者 ms.setRecipients(RecipientType.TO, InternetAddress.parse("liangrui_1988@126.com,liangrui_1988@sina.com,liangrui_1988@sohu.com")); //內容 ms.setContent("<span style='color:red'>常值內容 獃獃呆呵呵</span>","text/html;charset=gbk"); //發射================================= Transport.send(ms);*/ //載入產生好的郵件 Test3 並發送 Message me=new MimeMessage(session,new FileInputStream("F:/juxunDocument/email/demo3.eml")); Transport.send(me,InternetAddress.parse("liangrui_1988@sohu.com") ); System.out.println("okkk--------------"); } }
test3 [java] view plain copy