Android開發之JavaMail發送郵件(使用者反饋)

來源:互聯網
上載者:User

標籤:ref   hand   failed   word   sde   dma   失敗   客戶   bug   

給APP增加了一個使用者反饋的小功能,由於懶的搭伺服器,所以就用郵件的形式進行通訊,有如下兩種方式:

1.使用調用手機上的其他程式完成郵件發送

2.使用javamail進行郵件發送

這裡果斷使用javamail,因為我們大多數並不會在手機上使用郵件APP

使用javamail需要三個jar包,分別是additional.jar、mail.jar和activation.jar,可以到google官網下載:https://code.google.com/archive/p/javamail-android/downloads

我當時是因為沒法上google的網域名稱,baidu了好多,導包之後各種 java.lang.NoClassDefFoundError: javax.activation.DataHandler 錯誤,這是因為匯入的activation.jar包有問題,這裡放上從google下載的:

http://download.csdn.net/detail/u012062785/9685867

 

eclipse匯入jar包方法:

1.在項目的根目錄下,建立檔案夾libs,將下載的3個jar包放入該檔案夾

2.選中項目,右鍵->Properties->Java Build Path->Libraries,選擇Add External JARs,找到項目下lib目錄的3個jar包,完成匯入

 

接下來直接上源碼

 1 package com.pngcui.testmail; 2  3 import java.util.Properties; 4  5 import javax.mail.Message; 6 import javax.mail.MessagingException; 7 import javax.mail.PasswordAuthentication; 8 import javax.mail.Session; 9 import javax.mail.Transport;10 import javax.mail.internet.InternetAddress;11 import javax.mail.internet.MimeMessage;12 13 14 public class MailSend {15     16     private String mailContext;17     18     public MailSend(String context){19         this.mailContext = context;20     }21     22     public void sendMail() throws MessagingException{23         Properties props = new Properties();24         //使用smtp代理,且使用網易163郵箱25         props.put("mail.smtp.host", "smtp.163.cn");26         //設定驗證27         props.put("mail.smtp.auth", "true");28         MyAuthenticator myauth = new MyAuthenticator("寄件者郵箱@163.com", "密碼");29         Session session = Session.getInstance(props,myauth);30         //開啟調試開關31         session.setDebug(true);32         MimeMessage message = new MimeMessage(session);33         InternetAddress fromAddress = null;34         //寄件者郵箱地址35         fromAddress = new InternetAddress("寄件者郵箱@163.com");36         message.setFrom(fromAddress);37         38         InternetAddress toAddress = new InternetAddress("收件者郵箱地址");39         message.addRecipient(Message.RecipientType.TO, toAddress);40         message.setSubject("郵件標題");41         message.setText(mailContext);// 設定郵件內容42         //message.setFileName("郵件附件");43         message.saveChanges(); //儲存資訊44         45         46         Transport transport = null;47         transport = session.getTransport("smtp");48         transport.connect("smtp.163.com", "寄件者郵箱@163.com", "密碼");49         transport.sendMessage(message, message.getAllRecipients());50         51         transport.close();52     }53     54     class MyAuthenticator extends javax.mail.Authenticator {55         private String strUser;  56         private String strPwd;  57       58         public MyAuthenticator(String user, String password) {  59             this.strUser = user;  60             this.strPwd = password;  61         }  62       63         @Override64         protected PasswordAuthentication getPasswordAuthentication() {  65             return new PasswordAuthentication(strUser, strPwd);  66         }  67     }68 }

 

最後附錄幾個我遇到的問題

1.Authentication認證失敗,是因為你的密碼或者郵箱地址有誤,163的郵箱盡量不要使用新申請的,而且這個密碼是用戶端授權密碼,不是你的郵箱登入密碼!

2. com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp4,遇到這個問題是被反垃圾了==,在郵件標題或者郵件內文不要出現test或者helloworld的內容,否則網易會認為是垃圾郵件,導致無法發送出去。無法發送的原因可以參考http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html

 

最後調用new MailSend("郵件內文").sendMail();就可以發送出去了!!

 

Android開發之JavaMail發送郵件(使用者反饋)

聯繫我們

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