java 發郵件

來源:互聯網
上載者:User

標籤:儲存   txt   coder   private   div   pac   發送郵件   code   set   

//先從oracle 下載mail.jar
package
test;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.*;import java.util.*;import javax.activation.*;public class MailUtils { private String host = ""; // smtp伺服器 private String from = ""; // 寄件者地址 private String to = ""; // 收件者地址 private String affix = ""; // 附件地址 private String affixName = ""; // 附件名稱 private String user = ""; // 使用者名稱 private String pwd = ""; // 密碼 private String subject = ""; // 郵件標題 public void setAddress(String from, String to, String subject) { this.from = from; this.to = to; this.subject = subject; } public void setAffix(String affix, String affixName) { this.affix = affix; this.affixName = affixName; } public void send(String host, String user, String pwd) { this.host = host; this.user = user; this.pwd = pwd; Properties props = new Properties(); // 設定發送郵件的郵件伺服器的屬性(這裡使用網易的smtp伺服器) props.put("mail.smtp.host", host); // 需要經過授權,也就是有戶名和密碼的校正,這樣才能通過驗證(一定要有這一條) props.put("mail.smtp.auth", "true"); // 用剛剛設定好的props對象構建一個session Session session = Session.getDefaultInstance(props); // 有了這句便可以在發送郵件的過程中在console處顯示過程資訊,供調試使 // 用(你可以在控制台(console)上看到發送郵件的過程) session.setDebug(true); // 用session為參數定義訊息對象 MimeMessage message = new MimeMessage(session); try { // 載入寄件者地址 message.setFrom(new InternetAddress(from)); // 載入收件者地址 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 載入標題 message.setSubject(subject); // 向multipart對象中添加郵件的各個部分內容,包括常值內容和附件 Multipart multipart = new MimeMultipart(); // 設定郵件的常值內容 BodyPart contentPart = new MimeBodyPart(); contentPart.setText("郵件的具體內容在此"); multipart.addBodyPart(contentPart); // 添加附件 BodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(affix); // 添加附件的內容 messageBodyPart.setDataHandler(new DataHandler(source)); // 添加附件的標題 // 這裡很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼 sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); messageBodyPart.setFileName("=?GBK?B?" + enc.encode(affixName.getBytes()) + "?="); multipart.addBodyPart(messageBodyPart); // 將multipart對象放到message中 message.setContent(multipart); // 儲存郵件 message.saveChanges(); // 發送郵件 Transport transport = session.getTransport("smtp"); // 串連伺服器的郵箱 transport.connect(host, user, pwd); // 把郵件發送出去 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { MailUtils cn = new MailUtils(); // 設定寄件者地址、收件者地址和郵件標題 cn.setAddress("[email protected]", "****@qq.com", "一個帶附件的JavaMail郵件"); // 設定要發送附件的位置和標題 cn.setAffix("e:/1.txt", "1.txt"); /** * 設定smtp伺服器以及郵箱的帳號和密碼 * 用QQ 郵箱作為發生者不好使 (原因不明) * 163 郵箱可以,但是必須開啟 POP3/SMTP服務 和 IMAP/SMTP服務 * 因為程式屬於第三方登入,所以登入密碼必須使用163的授權碼 */ // 注意: [授權碼和你平時登入的密碼是不一樣的] cn.send("smtp.163.com", "[email protected]", "password"); }}

 

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.