1. 程式人生 > >Android Send Email 傳送郵件

Android Send Email 傳送郵件

最近公司有個需求,需要在意見反饋模組中增加發送郵件功能,看了網上好多的例子都是用的HtmlEmail和我需求不大一樣因此重新寫了一版傳送郵件程式碼供大家分享。

整理了個EmailUtils工具:1、傳送文字郵件 2、傳送附件郵件

jar包:activation.jar  additionnal.jar  mail.jar 自己去網上搜很多資源

import java.io.File;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import 
javax.mail.Authenticator; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; /**
* 郵件 * Created by Administrator on 2018/3/26. */ @SuppressWarnings("all") public class EmailUtils { private EmailUtils() { } public static EmailUtils getInstance() { return SafeMode.mEmailUtil; } public static class SafeMode { private static final EmailUtils mEmailUtil
= new EmailUtils(); } /** * 傳送郵件 * * @param fromEmailAddress * @param fromEmailPassword * @param toEmailAddress * @param Subject * @param Content * @return */ public boolean sendEmailMessages(String fromEmailAddress, String fromEmailPassword, String toEmailAddress, String Subject, String Content) {// 配置傳送及接收郵箱 try { Properties props = new Properties(); props.put("mail.smtp.protocol", "smtp"); props.put("mail.smtp.auth", "true");//設定要驗證 props.put("mail.smtp.host", "smtp.exmail.qq.com");//設定host props.put("mail.smtp.port", "465"); //設定埠 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); PassAuthenticator pass = new PassAuthenticator(fromEmailAddress, fromEmailPassword); //獲取帳號密碼 Session session = Session.getInstance(props, pass); //獲取驗證會話 // 始發郵箱 InternetAddress fromAddress = new InternetAddress(fromEmailAddress, fromEmailAddress); // 目標郵箱 InternetAddress toAddress = new InternetAddress(toEmailAddress, toEmailAddress); // 配置傳送資訊 MimeMessage message = new MimeMessage(session); MimeMultipart allMultipart = new MimeMultipart("mixed"); // 附件 message.setContent(allMultipart); // 發郵件時新增附件 message.setSubject(Subject); message.setText(Content, "UTF-8"); message.setFrom(fromAddress); message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress); message.saveChanges(); // 連線郵箱併發送 Transport transport = session.getTransport("smtp"); // 始發郵箱賬號密碼 transport.connect("smtp.exmail.qq.com", fromEmailAddress, fromEmailPassword); transport.send(message); transport.close(); return true; } catch (Exception e) { Log.i("Simon", "sendEmailMessages Exception = " + e.getMessage()); } return false; } /** * 傳送郵件-附件 * * @param fromEmailAddress * @param fromEmailPassword * @param toEmailAddress * @param Subject * @param Content * @param files * @return */ public boolean sendEmailMessagesWithFiles(String fromEmailAddress, String fromEmailPassword, String toEmailAddress, String Subject, List<File> files) { Properties props = new Properties(); props.put("mail.smtp.protocol", "smtp"); props.put("mail.smtp.auth", "true");//設定要驗證 props.put("mail.smtp.host", "smtp.exmail.qq.com");//設定host props.put("mail.smtp.port", "465"); //設定埠 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); PassAuthenticator pass = new PassAuthenticator(fromEmailAddress, fromEmailPassword); //獲取帳號密碼 Session session = Session.getInstance(props, pass); //獲取驗證會話 try { //配置傳送及接收郵箱 InternetAddress fromAddress, toAddress; // 發件郵箱 fromAddress = new InternetAddress(fromEmailAddress, fromEmailAddress); // 收件郵箱 toAddress = new InternetAddress(toEmailAddress, toEmailAddress); // 附件與內容 只能二選一 MimeMessage message = new MimeMessage(session); MimeMultipart allMultipart = new MimeMultipart("mixed"); //附件 for (File file : files) { MimeBodyPart attachPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(file); //開啟要傳送的檔案 attachPart.setDataHandler(new DataHandler(fds)); attachPart.setFileName(MimeUtility.encodeText(file.getName()));//TODO // allMultipart.addBodyPart(attachPart);//新增 } message.setContent(allMultipart); //發郵件時新增附件 message.setSubject(Subject); message.setFrom(fromAddress); message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress); message.saveChanges(); // 連線郵箱併發送 Transport transport = session.getTransport("smtp"); // 傳送郵件的賬號和密碼 transport.connect("smtp.exmail.qq.com", fromEmailAddress, fromEmailPassword); transport.send(message); transport.close(); } catch (Exception e) { Log.i("Simon", "sendEmailMessagesWithFiles Exception = " + e.getMessage()); } return false; } class PassAuthenticator extends Authenticator { String username; String password; public PassAuthenticator(String username, String password) { this.username = username; this.password = password; } public PasswordAuthentication getPasswordAuthentication() { // 添加發送郵箱的賬號和密碼 return new PasswordAuthentication(username, password); } } }  
上面的EmailUtils工具類直接呼叫即可,原始碼地址:DevBySimon-SendEmail