Android background email sending
Zookeeper
Public class SendEmail {private static final String TAG = "SendEmail"; // private String mailTo = null; // private String mailFrom = null; // SMTP host address private String smtpHost = null; // whether to enable debugging private boolean debug = false; private String messageBasePath = null; // Email topic private String subject; public void setMailTo (String mailTo) {this. mailTo = mailTo;} public void setMailFrom (String mailFrom) {this. mailFrom = mailFrom;} public void setSmtpHost (String smtpHost) {this. smtpHost = smtpHost;} public void setDebug (boolean debug) {this. debug = debug;} public void setMessageBasePath (String messageBasePath) {this. messageBasePath = messageBasePath;} public void setSubject (String subject) {this. subject = subject;} public void setMsgContent (String msgContent) {this. msgC Ontent = msgContent;} public void setAttachedFileList (Vector attachedFileList) {this. attachedFileList = attachedFileList;} public void setEmailAccount (String emailAccount) {this. emailAccount = emailAccount;} public void setEmailPwd (String emailPwd) {this. emailPwd = emailPwd;} public void setMessageContentType (String messageContentType) {this. messageContentType = messageContentType;} publi C void setEmailbccTo (String emailbccTo) {this. emailbccTo = emailbccTo;} public void setEmailccTo (String emailccTo) {this. emailccTo = emailccTo;} // Email content private String msgContent; private Vector attachedFileList; private String emailAccount = null; private String emailPwd = null; private String messageContentType = "text/html; charset = UTF-8 "; private String emailbccTo = null; private String e MailccTo = null;/* default constructor */public SendEmail () {super ();} private void writeEmail (Session session, Message message) throws MessagingException {String fileName; multipart multipart = new MimeMultipart (); // set the sender address if (mailFrom! = Null) {message. setFrom (new InternetAddress (mailFrom); Log. I (TAG, "sender email address:" + mailFrom);} else {Log. I (TAG, "No sender email address specified"); return ;}// set the recipient address if (mailTo! = Null) {message. setRecipient (Message. recipientType. TO, new InternetAddress (mailTo); Log. I (TAG, "recipient email address:" + mailTo);} else {Log. I (TAG, "no recipient email address specified"); return;} // sets the CC address if (emailccTo! = Null) {message. setRecipient (Message. recipientType. CC, new InternetAddress (emailccTo); Log. I (TAG, "cc email address:" + emailccTo);} else {Log. I (TAG, "No CC email address specified"); return ;}// set the BCC address if (emailbccTo! = Null) {message. setRecipient (Message. recipientType. BCC, new InternetAddress (emailbccTo); Log. I (TAG, "BCC email address:" + emailbccTo);} else {Log. I (TAG, "No BCC address specified"); return ;}// set the mail subject message. setSubject (subject); Log. I (TAG, "subject:" + subject); // sets the reply Address message. setReplyTo (new InternetAddress [] {new InternetAddress (mailFrom)}); // create and set the first part of MimeBodyPart bodyPart = new MimeBodyPart (); if (msgContent! = Null) {Log. I (TAG, "email content:" + msgContent); bodyPart. setContent (msgContent, messageContentType);} else {bodyPart. setContent ("", messageContentType);} multipart. addBodyPart (bodyPart); // Add the attachment file to the email if (attachedFileList! = Null) {for (Enumeration fileList = attachedFileList. elements (); fileList. hasMoreElements ();) {fileName = (String) fileList. nextElement (); MimeBodyPart mBodyPart = new MimeBodyPart (); FileDataSource fds = new FileDataSource (messageBasePath + fileName); Log. I (TAG, "the attachment sent by Email is:" + messageBasePath + fileName); mBodyPart. setDataHandler (new DataHandler (fds); mBodyPart. setFileName (fileName); multipart. addBodyPart (mBodyPart);} Log. I (TAG, "set email"); message. setContent (multipart); message. setSentDate (new Date ();}/*** email sending method ** @ return true indicates that the email is sent successfully, and false indicates that the email is not sent successfully */public boolean sendEmail () {int loopCount; properties properties = System. getProperties (); properties. setProperty ("mail. smtp. host ", smtpHost); properties. setProperty ("mail. smtp. auth "," true "); properties. put ("mail. smtp. port "," 25 "); MailAuthenticator authenticator = new MailAuthenticator (); Session session = Session. getInstance (properties, authenticator); session. setDebug (debug); MimeMessage mimeMessage = new MimeMessage (session );
// If Transport is used, the following error occurs: SMTPTransport transport = new SMTPTransport (session, new URLName ("smtp", "smtp.qq.com", 25, null, MailAuthenticator. TENCENT_EMAIL_USER, MailAuthenticator. TENCENT_EMAIL_PWD); try {writeEmail (session, mimeMessage); // transport = session. getTransport ("smtp"); try {Log. I (TAG, "Start connecting to the server"); transport. connect (smtpHost, 25, MailAuthenticator. TENCENT_EMAIL_USER, MailAuthenticator. T ENCENT_EMAIL_PWD);} catch (AuthenticationFailedException e) {e. printStackTrace (); Log. I (TAG, "failed to connect to the server"); return false;} catch (MessagingException e) {e. printStackTrace (); Log. I (TAG, "An error occurred during email sending"); return false;} Log. I (TAG, "Start sending mail"); transport. sendMessage (mimeMessage, mimeMessage. getAllRecipients (); transport. close (); Log. I (TAG, "close connection");} catch (MessagingException e) {e. printStackTrace (); Log. I (TAG, "failed to send mail"); return false;} finally {try {if (transport! = Null & transport. isConnected () {transport. close (); Log. I (TAG, "close connection in finally") ;}} catch (MessagingException e) {e. printStackTrace () ;}} Log. I (TAG, "email sent successfully"); return true ;}}
Tips --- the MyAuthenticator class inherits from Authenticator. Just rewrite this method.
@ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (email user name, password );}
Zookeeper