Java發送郵件樣本

來源:互聯網
上載者:User

標籤:郵件內容   aging   pwd   rac   main   efault   ops   分享圖片   nec   

利用Java發送郵件樣本:

1、發送QQ郵件

 1 import java.util.Properties; 2 import javax.mail.Message; 3 import javax.mail.MessagingException; 4 import javax.mail.Session; 5 import javax.mail.Transport; 6 import javax.mail.internet.AddressException; 7 import javax.mail.internet.InternetAddress; 8 import javax.mail.internet.MimeMessage; 9 10 public class SendQQMailUtil {11     12     public static void main(String[] args) throws AddressException,MessagingException {13         Properties properties = new Properties();14         properties.put("mail.transport.protocol", "smtp");// 連線協定15         properties.put("mail.smtp.host", "smtp.qq.com");// 主機名稱16         properties.put("mail.smtp.port", 465);// 連接埠號碼17         properties.put("mail.smtp.auth", "true");18         properties.put("mail.smtp.ssl.enable", "true");// 設定是否使用ssl安全連線 ---一般都使用19         properties.put("mail.debug", "true");// 設定是否顯示debug資訊 true 會在控制台顯示相關資訊20         // 得到回話對象21         Session session = Session.getInstance(properties);22         // 擷取郵件對象23         Message message = new MimeMessage(session);24         // 設定寄件者郵箱地址25         message.setFrom(new InternetAddress("[email protected]"));26         // 設定收件者郵箱地址 27         message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("[email protected]"),new InternetAddress("[email protected]"),new InternetAddress("[email protected]")});28         //message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));//一個收件者29         // 設定郵件標題30         message.setSubject("xmqtest");31         // 設定郵件內容32         message.setText("郵件內容郵件內容郵件內容xmqtest");33         // 得到郵差對象34         Transport transport = session.getTransport();35         // 串連自己的郵箱賬戶36         transport.connect("[email protected]", "xxxxxxxxxxxxx");// 密碼為QQ郵箱開通的stmp服務後得到的用戶端授權碼37         // 發送郵件38         transport.sendMessage(message, message.getAllRecipients());39         transport.close();40     }41 }

2、發送163郵箱

 1 import java.io.IOException; 2 import java.util.*; 3  4 import javax.mail.*; 5 import javax.mail.internet.*; 6 import javax.activation.*; 7  8 public class SendMailUtil { 9 10     static String HOST = ""; // smtp伺服器11     static String FROM = ""; // 寄件者地址12     static String TO = ""; // 收件者地址13     static String AFFIX = ""; // 附件地址14     static String AFFIXNAME = ""; // 附件名稱15     static String USER = ""; // 使用者名稱16     static String PWD = ""; // 163的授權碼17     static String SUBJECT = ""; // 郵件標題18     static String[] TOS = null;19     20     static {21         try {22             Properties props = new Properties(); 23             props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));//從自訂設定檔擷取相關參數24             HOST=props.getProperty("host");25             FROM=props.getProperty("from");26             TO=props.getProperty("to");27             TOS=TO.split(",");28             AFFIX=props.getProperty("affix");29             AFFIXNAME=props.getProperty("affixName");30             USER=props.getProperty("user");31             PWD=props.getProperty("pwd");32             SUBJECT=props.getProperty("subject");33         } catch (IOException e) {34             e.printStackTrace();35         }36     }37 38     /**39      * 發送郵件40      * @param host41      * @param user42      * @param pwd43      */44     public static void send(String context) {45         Properties props = new Properties();46         props.put("mail.smtp.host", HOST);//設定發送郵件的郵件伺服器的屬性(這裡使用網易的smtp伺服器)47         props.put("mail.smtp.auth", "true");  //需要經過授權,也就是有戶名和密碼的校正,這樣才能通過驗證(一定要有這一條)48         Session session = Session.getDefaultInstance(props);//用props對象構建一個session49         session.setDebug(true);50         MimeMessage message = new MimeMessage(session);//用session為參數定義訊息對象51         try {52             message.setFrom(new InternetAddress(FROM));// 載入寄件者地址53             InternetAddress[] sendTo = new InternetAddress[TOS.length]; // 載入收件者地址54             for (int i = 0; i < TOS.length; i++) {  55                 sendTo[i] = new InternetAddress(TOS[i]);  56             }57             message.addRecipients(Message.RecipientType.TO,sendTo);58             message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(FROM));//設定在發送給收信人之前給自己(發送方)抄送一份,不然會被當成垃圾郵件,報554錯59             message.setSubject(SUBJECT);//載入標題60             Multipart multipart = new MimeMultipart();//向multipart對象中添加郵件的各個部分內容,包括常值內容和附件61             BodyPart contentPart = new MimeBodyPart();//設定郵件的常值內容62             contentPart.setText(context);63             multipart.addBodyPart(contentPart);64             if(!AFFIX.isEmpty()){//添加附件65                  BodyPart messageBodyPart = new MimeBodyPart();66                  DataSource source = new FileDataSource(AFFIX);67                  messageBodyPart.setDataHandler(new DataHandler(source));//添加附件的內容68                  sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();//添加附件的標題69                  messageBodyPart.setFileName("=?GBK?B?"+ enc.encode(AFFIXNAME.getBytes()) + "?=");70                  multipart.addBodyPart(messageBodyPart);71             }72             message.setContent(multipart);//將multipart對象放到message中73             message.saveChanges(); //儲存郵件74             Transport transport = session.getTransport("smtp");//發送郵件75             transport.connect(HOST, USER, PWD);//串連伺服器的郵箱76             transport.sendMessage(message, message.getAllRecipients());//把郵件發送出去77             transport.close();//關閉串連78         } catch (Exception e) {79             e.printStackTrace();80         }81     }82     83 84 //    public static void main(String[] args) {85 //        send("內容");86 //    }87     88 }

3、關於郵箱服務授權配置自行參考官方文檔。

如163郵箱設定:

 

Java發送郵件樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.