java 發送郵件 email相關作業碼測試,產生複雜格式郵件,發送郵件相關操作

來源:互聯網
上載者:User


效果圖:


相關代碼:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.