How to send mail using the Java Mail API in Android

Source: Internet
Author: User
Tags integer thread email account how to send mail port number

Recently, considering adding a user feedback function to an existing application, users can feedback the user's opinions and suggestions and the problems of the program in a way that is more in line with the user's habit. There are also some services that implement good feedback programs, including bug submissions, program recommendations, and problems with program use, but such services are not completely open source and free, And I'm positioning the feedback of the program or the user can put his ideas through the writing method to send back can (do not need to record the system state of the crash), so still decided to fully understand the implementation mechanism on the basis of implementation of feedback functions.

By sending mail without having to build the server yourself, but also with my management habits of feedback, I decided to use the feedback function in the way that I sent the message within the application.

StackOverflow's answer http://stackoverflow.com/a/2033124/1767800 basically gives you the means to send mail within an application in Android, While reading the code I was always looking for how SMTP was certified. It is found that Gmailsender directly inherits the Javax.mail.Authenticator class in the implementation method, realizes the Getpasswordauthentication method, and passes the Session.getdefaultinstance (Props, this) (Getdefaultinstance (Properties props, authenticator authenticator)) statement writes the Authenticator property while the session is established.

In order to deliver mail within an Android application, there are two possible modifications that need to be noted:

1. The answer uses the Gmail account as the email account, and if you use a different account, you need to set up separate settings for the outgoing mail server and port number.

For 126 or 163 mailboxes, you only need to change the part of the properties setting to:

Properties Props = new properties ();      
        Props.setproperty ("Mail.transport.protocol", "SMTP");      
        Props.setproperty ("Mail.host", mailhost);      
        Props.put ("Mail.smtp.auth", "true");      
        Props.put ("Mail.smtp.port", "25");

Reference: http://www.cnblogs.com/peakzheng/archive/2012/02/04/2338671.html

2. The sending mail operation in the UI thread (the main thread) needs to be transferred to a separate thread for processing

Google has made stricter demands on the operation of the main thread after the release of Honeycomb. In order to ensure the user experience, in the main thread can not network data transmission operations, because the main thread in the network data transmission may bring a long time without responding to the user's click operation, resulting in bad user experience (reference). For lightweight network operations, the Android system is typically implemented through the Asynctask class.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Supposedly, Android can prompt android.os.NetworkOnMainThreadException errors in a newer version of the system for an application to access the network on the main interface, but when you use the Java Mail API to send messages within an application, What I have encountered is running the program on the 2.3 version of the simulator, and the receiver can receive the sent message. The same code fails to send messages successfully on a 4.2.2 version of the phone, and there is no error message in the Logcat interface under Eclipse. A similar comment was given after the StackOverflow answer http://stackoverflow.com/a/2033124/1767800.

However, after the Sender.sendmail () function is transferred to the Asynctask class, the message can also be sent successfully on 4.2.2 's phone. The specific implementation is:

1 Create a new class that inherits from Asynctask

Class Sendmailtask  extends Asynctask<url, Integer, long> {   
    mailsender sender;   
    Public Sendmailtask (MailSender sender) {   
        //TODO auto-generated constructor stub   
        this.sender=sender;   
    }   
    Protected long Doinbackground (URL ... urls) {   
               
        long totalsize = 0;   
        try {   
            This.sender.sendMail (' This is Subject ', ' This are body ', '      
                    senderaddress ', '      
                    receiveraddress ");   
        } catch (Exception e) {   
            //TODO auto-generated catch block   
            e.printstacktrace ();   
        }   
        return totalsize;   
    }   
       
    protected void Onprogressupdate (Integer ... progress) {   
    }   
       
    protected void OnPostExecute (Long result) {   
    }< c24/>}

2 Send the message through the following statement

New Sendmailtask (Sender). Execute ();

Messages received via Gmail are shown in the following image

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.