Android realizes the Mail sending function with the attachment _android

Source: Internet
Author: User
Tags auth how to send mail

This article explains the implementation of the Android mail based on JMail based on the function of sharing for everyone to refer to, the specific contents are as follows

How to send Mail on Android:

The first: with Gmail app client, the disadvantage is that the Gmail account must be used, one thing is more convenient, do not need to write a lot of code, but not very flexible.

The second: based on jmail implementation, you can be very flexible to set up a variety of properties, do not need a Gmail account

Before realizing the second way, look at the jmail of the email structure:

Email is sent based on the SMTP protocol, so the client must know the SMTP host.

The SMTP host for Tencent Mail is: stmp.qq.com port is 465 based on SSL protocol.

Finally, I made a simple package, the sending text plus image attachment to make the function.

A separate class that can be done by just one call:

Package Com.gloomyfish.jmail.demo; 
Import Java.util.Date; 
 
Import java.util.Properties; 
Import Javax.activation.DataHandler; 
Import Javax.activation.DataSource; 
Import Javax.activation.FileDataSource; 
Import javax.mail.Address; 
Import Javax.mail.Message; 
Import Javax.mail.Multipart; 
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; 
  public class Emailsender {private String host; 
  Private String Port; 
  Private String UserName; 
  private String password; 
 
  Private string[] images; 
  Public string[] Getimagepath () {return images; 
  } public void setImagePath (string[] imagepath) {this.images = ImagePath; 
    Public Emailsender (string host, String port, String userName, string password) {this.host = host; 
    This.port = port; This.username = userName; 
  This.password = password;  public void SendEmail (string subject, String recepits, string sender, String content) {Properties props = 
    New Properties (); Props.put ("Mail.smtp.host", host); 
    Set SMTP server address//props.put ("Mail.smtp.starttls.enable", "true"); Props.put ("Mail.smtp.port", port); Set port//Props.put ("Mail.smtp.auth", "true"); 
     
    Set the SMTP server to authenticate. 
    Props.put ("Mail.smtp.socketFactory.port", port); 
    Props.put ("Mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    Props.put ("Mail.smtp.auth", "true"); 
     
    Props.put ("Mail.smtp.port", port); 
    Returns the authorization Base64 encoding popupauthenticator auth = new Popupauthenticator (userName, password);  
    Get the Session object sessions = session.getinstance (props, auth); 
     
    Set to debug mode Session.setdebug (TRUE); 
    Mail Content Object assembly mimemessage message = new MimeMessage (session); try {Address Addressfrom = new InternetAddress (SendeR, "Jia Zhi Gang"); 
      Address Addressto = new InternetAddress (recepits, "My QQ e-mail"); 
      Message.setsubject (subject); 
      Message.setsentdate (New Date ()); 
      Message.setfrom (Addressfrom); 
        
      Message.addrecipient (Message.recipienttype.to,addressto); 
      Message text/html content Multipart Multipart = new Mimemultipart (); 
      MimeBodyPart Messagebodypart = new MimeBodyPart (); 
      Messagebodypart.setcontent (Content, "text/html"); 
       
      Multipart.addbodypart (Messagebodypart); 
          Add message attachment if (images!= null && images.length > 0) {for (String filepath:images) {   
          MimeBodyPart Attachpart = new MimeBodyPart (); 
          DataSource Source = new Filedatasource (FilePath); 
          Attachpart.setdatahandler (new DataHandler (source)); 
          Attachpart.setfilename (FilePath); 
        Multipart.addbodypart (Attachpart); 
     }//Save message Content message.setcontent (multipart);  
      Gets the SMTP protocol client object, which is connected to the specified SMPT server transport transport = Session.gettransport ("SMTP"); 
      Transport.connect (Host, Integer.parseint (port), userName, password); 
       
      System.out.println ("Connet IT success!!!!"); 
      Send mail to SMTP server Thread.CurrentThread (). Setcontextclassloader (GetClass (). getClassLoader ()); 
      Transport.send (message); 
       
      SYSTEM.OUT.PRINTLN ("Send it success!!!!"); 
    Close connection transport.close (); 
    catch (Exception e) {e.printstacktrace (); 
  } public String GetHost () {return host; 
  public void Sethost (String host) {this.host = host; 
  Public String Getport () {return port; 
  The public void Setport (String port) {this.port = port; 
  Public String GetUserName () {return userName; 
  } public void Setusername (String userName) {this.username = UserName; 
  Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password; 
 } 
 
}

User Authorization class:

Package Com.gloomyfish.jmail.demo; 
 
 
Import Javax.mail.Authenticator; 
Import javax.mail.PasswordAuthentication; 
 
 
Class Popupauthenticator extends Authenticator { 
  private String userName; 
  private String password; 
  Public Popupauthenticator (String userName, string password) 
  { 
    this.username = userName; 
    This.password = password; 
  } 
  Public Passwordauthentication getpasswordauthentication () {return 
    new passwordauthentication (UserName, password); 
  } 
 

Special attention:
Sending Mail on Android must import three related Java files yourself

The above is the entire content of this article, I hope to learn more about Android software programming help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.