我們在安卓開發中,可能需要安卓發送郵件的功能。往往,我們必須手動輸入賬號和密碼后發生郵件。這種方式費時費力,因此在這里介紹使用一種方法實現自動發送郵件的功能,我們可以用其收集bug發送郵箱等功能的實現。
一 下載jar包
Android實現發送郵件,首先需要依賴additional.jar、mail.jar和activation.jar這3個jar包。
首先前往百度網盤 密碼: 8s5b下載所需要的jar包,然后將其放入app/libs文件加下
二 創建發送郵件所需要的EmailSender 類
/**
* Created by cs_android on 2018/1/16.
*/
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailSender {
private Properties properties;
private Session session;
private Message message;
private MimeMultipart multipart;
public EmailSender() {
this.properties = new Properties();
}
public void setProperties(String host,String post){
//地址
this.properties.put("mail.smtp.host",host);
//端口號
this.properties.put("mail.smtp.post",post);
//是否驗證
this.properties.put("mail.smtp.auth",true);
this.session=Session.getInstance(properties);
this.message = new MimeMessage(session);
this.multipart = new MimeMultipart("mixed");
}
/**
* 設置收件人
* @param receiver 收件人
* @throws MessagingException
*/
public void setReceiver(String[] receiver) throws MessagingException{
Address[] address = new InternetAddress[receiver.length];
for(int i=0;i<receiver.length;i++){
address[i] = new InternetAddress(receiver[i]);
}
this.message.setRecipients(Message.RecipientType.TO, address);
}
/**
* 設置郵件
* @param from 來源
* @param title 標題
* @param content 內容
* @throws AddressException
* @throws MessagingException
*/
public void setMessage(String from,String title,String content) throws AddressException, MessagingException{
this.message.setFrom(new InternetAddress(from));
this.message.setSubject(title);
//純文本的話用setText()就行,不過有附件就顯示不出來內容了
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(content,"text/html;charset=gbk");
this.multipart.addBodyPart(textBody);
}
/**
* 添加附件
* @param filePath 文件路徑
* @throws MessagingException
*/
public void addAttachment(String filePath) throws MessagingException{
FileDataSource fileDataSource = new FileDataSource(new File(filePath));
DataHandler dataHandler = new DataHandler(fileDataSource);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(dataHandler);
mimeBodyPart.setFileName(fileDataSource.getName());
this.multipart.addBodyPart(mimeBodyPart);
}
/**
* 發送郵件
* @param host 地址
* @param account 賬戶名
* @param pwd 密碼
* @throws MessagingException
*/
public void sendEmail(String host,String account,String pwd) throws MessagingException{
//發送時間
this.message.setSentDate(new Date());
//發送的內容,文本和附件
this.message.setContent(this.multipart);
this.message.saveChanges();
//創建郵件發送對象,并指定其使用SMTP協議發送郵件
Transport transport=session.getTransport("smtp");
//登錄郵箱
transport.connect(host,account,pwd);
//發送郵件
transport.sendMessage(message, message.getAllRecipients());
//關閉連接
transport.close();
}
}
三 發送郵件
//耗時操作要起子線程
new Thread(new Runnable() {
@Override
public void run() {
try {
EmailSender sender = new EmailSender();
//設置服務器地址和端口,可以查詢網絡
sender.setProperties("smtp.163.com", "25");
//分別設置發件人,郵件標題和文本內容
sender.setMessage("×××××××××@163.com", "title" + "-" +
"content");
//設置收件人
sender.setReceiver(new String[]{"×××××××@163.com"});
//添加附件換成你手機里正確的路徑
// sender.addAttachment("/sdcard/emil/emil.txt");
//發送郵件
//sender.setMessage("你的163郵箱賬號", "EmailS//ender", "Java Mail !");這里面兩個郵箱賬號要一致
sender.sendEmail("smtp.163.com", "*****@163.com", "password");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}).start();