SoFunction
Updated on 2025-04-04

Android implements automatic sending of emails

This example shares with you a demo that implements android automatic email sending. Support 163, qq mailbox

Need to add, and these three packages

First, there is an EmailSender class

import ;
 
import ;
import ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
public class EmailSender {
 private Properties properties;
 private Session session;
 private Message message;
 private MimeMultipart multipart;
 
 public EmailSender() {
 super();
  = new Properties();
 }
 public void setProperties(String host,String post){
 //address ("",host);
 //Port number ("",post);
 //Whether to verify ("",true);
 =(properties);
  = new MimeMessage(session);
  = new MimeMultipart("mixed");
 }
 /**
  * Set the recipient
  * @param receiver
  * @throws MessagingException
  */
 public void setReceiver(String[] receiver) throws MessagingException{
 Address[] address = new InternetAddress[];
 for(int i=0;i<;i++){
  address[i] = new InternetAddress(receiver[i]);
 }
 (, address);
 }
 /**
  * Set up email
  * @param from Source
  * @param title title
  * @param content
  * @throws AddressException
  * @throws MessagingException
  */
 public void setMessage(String from,String title,String content) throws AddressException, MessagingException{
 (new InternetAddress(from));
 (title);
 //If you want plain text, just use setText(), but if there is an attachment, the content will not be displayed. MimeBodyPart textBody = new MimeBodyPart();
 (content,"text/html;charset=gbk");
 (textBody);
 }
 /**
  * Add attachment
  * @param filePath file path
  * @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();
 (dataHandler);
 (());
 (mimeBodyPart);
 }
 /**
  * Send email
  * @param host address
  * @param account Account name
  * @param pwd Password
  * @throws MessagingException
  */
 public void sendEmail(String host,String account,String pwd) throws MessagingException{
 //Send time (new Date());
 //Send content, text and attachments ();
 ();
 //Create a mail sending object and specify that it sends mail using SMTP protocol Transport transport=("smtp"); 
 //Login email (host,account,pwd); 
 //Send email (message, ());
 //Close the connection ();
 }
}

Below is the mainactivity code

import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
public class MainActivity extends Activity {
 
 private Button btnOK; 
  @Override
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState);
    setContentView(.activity_main);
    btnOK = (Button) findViewById();
    (new OnClickListener() {
      @Override
      public void onClick(View arg0) {
//        sendEmail();
       //The time-consuming operation requires a thread... There are several newbies who have this problem        new Thread(new Runnable() {
 
       @Override
       public void run() {
        try {
        EmailSender sender = new EmailSender();
        //Set the server address and port, search online        ("smtp.", "25");
        //Set the sender, email title and text content separately        ("Your 163 email account", "EmailSender", "Java Mail !");
        //Set the recipient        (new String[]{"Recipient Email"});
        //Add attachment        //The path of this attachment is from my phone. If you want to send it, you have to change it to the correct path in your phone.//        ("/sdcard/DCIM/Camera/");
        //Send email        ("smtp.", "Your 163 email account", "Your Email Password");//<span style="font-family: Arial, Helvetica, sans-serif;">("Your 163 email account", "EmailS//ender", "Java Mail!");The two email accounts here must be the same</span> 
        } catch (AddressException e) {
        // TODO Auto-generated catch block
        ();
        } catch (MessagingException e) {
        // TODO Auto-generated catch block
        ();
        }
       }
       }).start();
      }
    });
 
  }
 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.