Android sends emails. android sends emails.
Some key logs in the project need to be obtained from the customer. To facilitate log collection, add the mail sending function to the client (although the mail can be sent using the sharing function, the customer needs to log on to the email address of the mobile phone, which is troublesome and still enters the page, and the user experience is not good ).
Mainly implements the function of sending emails, CC emails, and attachments
Let's take a look at it first:
Three jar files and encapsulated interfaces are involved.
JEmailHelper.java
Package quota zhangjie.com. javaemail; import android. OS. asyncTask; import java. util. date; import java. util. list; import java. util. properties; import javax. activation. commandMap; import javax. activation. mailcapCommandMap; import javax. mail. authenticator; import javax. mail. message; import javax. mail. multipart; import javax. mail. passwordAuthentication; import javax. mail. session; import javax. mail. transport; import jav Ax. mail. internet. internetAddress; import javax. mail. internet. mimeBodyPart; import javax. mail. internet. mimeMessage; import javax. mail. internet. mimeMultipart;/*** Created by zhangjie on 15/8/31. */public class JEmailHelper {private int port = 553; // port private String host = "smtp.163.com" used by smtp protocol "; // The sender's email server private String user = "******* @ 163.com"; // the user's account private String password = "********"; // enable User Password private List <String> emailTos; private List <String> emailCCs; private String subject; private String body; private List <String> paths; enum SendStatus {SENDING, UNDO, SENDOK, SENDFAIL}; private SendStatus sendStatus; public interface JieEmailInfterface {void startSend (); void SendStatus (SendStatus sendStatus);} private JieEmailInfterface jieEmailInfterface; public void setjieeinfterf Ace (JieEmailInfterface jieEmailInfterface) {this. jieEmailInfterface = jieEmailInfterface;} public JEmailHelper () {sendStatus = SendStatus. UNDO;} // construct the server, port, account, password public JEmailHelper (String host, int port, String user, String password) {this. port = port; this. user = user; this. password = password; this. host = host; sendStatus = SendStatus. UNDO;}/*** @ param emailTos main recipient's email list * @ pa Ram emailCCs CC recipients email List * @ param subject Email subject * @ param body content * @ param paths attachment path set */public void setParams (List <String> emailTos, list <String> emailCCs, String subject, String body, List <String> paths) {this. emailTos = emailTos; this. emailCCs = emailCCs; this. subject = subject; this. body = body; this. paths = paths;} public void sendEmail () {new jieasyntask(.exe cute ();} private void se NdEmailBg () throws Exception {Properties properties = new Properties (); properties. put ("mail. smtp. host ", host); properties. put ("mail. smtp. port ", port); properties. put ("mail. smtp. auth "," true "); // true must be enclosed in quotation marks for properties. put ("mail. transport. protocol "," smtp "); JieAuthenticator jieAuth = new JieAuthenticator (user, password); Session session = Session. getInstance (properties, jieAuth); // create a message MimeMess Age msg = new MimeMessage (session); // sets the sender msg. setFrom (new InternetAddress (user); // sets the primary receiver if (emailTos! = Null &&! EmailTos. isEmpty () {int size = emailTos. size (); InternetAddress [] addresses = new InternetAddress [size]; for (int I = 0; I <size; I ++) {addresses [I] = new InternetAddress (emailTos. get (I);} msg. setRecipients (Message. recipientType. TO, addresses);} // sets the CC email if (emailCCs! = Null &&! EmailCCs. isEmpty () {int size = emailCCs. size (); InternetAddress [] addresses = new InternetAddress [size]; for (int I = 0; I <size; I ++) {addresses [I] = new InternetAddress (emailCCs. get (I);} msg. setRecipients (Message. recipientType. CC, addresses);} msg. setSubject (subject); // create a message body MimeBodyPart msgBodyPart = new MimeBodyPart (); msgBodyPart. setText (body); // create a Multipart to add other parts Multipart mp = New MimeMultipart (); mp. addBodyPart (msgBodyPart); // create a file attachment for (String path: paths) {MimeBodyPart fileBodyPart = new MimeBodyPart (); fileBodyPart. attachFile (path); mp. addBodyPart (fileBodyPart);} // Add Multipart to message body msg. setContent (mp); // set the date msg. setSentDate (new Date (); // set the attachment format MailcapCommandMap mc = (MailcapCommandMap) CommandMap. getdefacommandcommandmap (); mc. addMailcap ("text/html; x-java-conten T-handler = com. sun. mail. handlers. text_html "); mc. addMailcap ("text/xml; x-java-content-handler = com. sun. mail. handlers. text_xml "); mc. addMailcap ("text/plain; x-java-content-handler = com. sun. mail. handlers. text_plain "); mc. addMailcap ("multipart/*; x-java-content-handler = com. sun. mail. handlers. multipart_mixed "); mc. addMailcap ("message/rfc822; x-java-content-handler = com. sun. mail. handlers. message_rfc822" ); CommandMap. setdefacommandcommandmap (mc); // send the message Transport. send (msg);} class JieAuthenticator extends Authenticator {private String strUser; private String strPwd; public JieAuthenticator (String user, String password) {this. strUser = user; this. strPwd = password;} protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (strUser, strPwd);} class JieAsynT Ask extends AsyncTask <Void, Void, SendStatus >{@ Override protected void onPreExecute () {super. onPreExecute (); if (jieEmailInfterface! = Null) {jieEmailInfterface. startSend () ;}@ Override protected void onPostExecute (SendStatus result) {super. onPostExecute (result); if (jieEmailInfterface! = Null) {jieEmailInfterface. sendStatus (result);} sendStatus = SendStatus. UNDO;} @ Override protected SendStatus doInBackground (Void... params) {try {sendStatus = SendStatus. SENDING; sendEmailBg (); sendStatus = SendStatus. SENDOK;} catch (Exception e) {e. printStackTrace (); sendStatus = SendStatus. SENDFAIL;} return sendStatus ;}}}
Package quota zhangjie.com. javaemail; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. toast; import java. util. arrayList; import java. util. list; public class JavaEmailActivity extends Activity {JEmailHelper helper; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_java_email ); Helper = new JEmailHelper ();} public void sendMail (View view) {List <String> files = new ArrayList <String> (); files. add ("/mnt/sdcard/test.txt"); List <String> toEmail = new ArrayList <String> (); toEmail. add ("jwzhangjie@163.com"); List <String> ccEmail = new ArrayList <String> (); ccEmail. add ("1067 ***** @ qq.com"); ccEmail. add ("352 **** @ qq.com"); helper. setParams (toEmail, ccEmail, "Android sends test mail", "Dea R All: this is a test email! ", Files); helper. setJieEmailInfterface (new JEmailHelper. jieEmailInfterface () {@ Override public void startSend () {showInfo ("start sending mail") ;}@ Override public void SendStatus (JEmailHelper. sendStatus sendStatus) {switch (sendStatus) {case SENDOK: showInfo ("successfully sent"); break; case SENDFAIL: showInfo ("failed to send mail"); break; case SENDING: showInfo ("the email is being sent. Please try again later"); break ;}}); helper. sendEmail ();} public void showInfo (String msg) {Toast. makeText (getApplicationContext (), msg, Toast. LENGTH_SHORT ). show ();}}Three jar addresses:
http://pan.baidu.com/s/1bnbi7zp
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.