Android Background Send mail sample (collect application exception information +demo code)

Source: Internet
Author: User
Tags http post set time mailmessage
Today we introduce a simpler method, we collect the exception information, through the background to send the mail method, the relevant exception information sent to our designated mailbox inside

The last time I said how to collect the error messages of our published applications, we can debug and improve the program. The last collection method is to send the information collected through the HTTP POST request to the server by turning the relevant exception information into the request parameter. For those who have done web development, server-side processing is simple. But it's a hassle for a lot of people who don't make the web. Today we introduce a simpler method, we collect the exception information, through the background to send mail method, the relevant exception information sent to our designated mailbox.

This article is a practical article and will not involve too much theoretical analysis. The main is to let everyone look at the future know how to add this function in their own applications.

1. Third Party Library
this time send the background mail need to use three third party's library, these several libraries in Java inside should be more famous. Used to be a friend of Java Mail development, should be used more or less. Android default to send mail method, need to call the system via intent mail program, this is not practical for our background operation.

Activation.jar
Additionnal.jar
Mail.jar

below I demo example inside will enclose these three packages, these several packs on-line also many resources, may download by oneself.

2, Mail information
because we are sending mail in the background, we need to collect some necessary information because the user is not required to enter this information.

Copy Code code as follows:


{

IP and port of the
//mail-Sending server


private String Mailserverhost;


private String Mailserverport = "25";





//Mail sender's address


private String fromaddress;


//Mail Recipient's address


private String toaddress;


//Login mail send server username and password


private String UserName;


private String password;


//Whether authentication is required


Private Boolean validate = true;


//Mail subject


private String subject;


//Message text content


private String content;


The file name of the message attachment


private string[] attachfilenames;


}


These are all the information we need to send emails. It should be noted here that we send background mail need to give the account password and other sensitive information. These email messages, we can write in the program, so that when we send the mail, we do not need users to enter any information.

3. Send mail

Copy Code code as follows:


public boolean sendtextmail (Mailsenderinfo mailinfo)


    {


//Determine if identity authentication is required


Myauthenticator authenticator = null;


Properties Pro = Mailinfo.getproperties ();


if (mailinfo.isvalidate ())


        {   


If authentication is required, create a password validator


authenticator = new Myauthenticator (Mailinfo.getusername (), Mailinfo.getpassword ());


        }  


//Construction of a session to send a message based on Message conversation properties and password validators


Session sendmailsession = Session.getdefaultinstance (pro,authenticator);


Try


        {   


//Create a mail message based on session


Message MailMessage = new MimeMessage (sendmailsession);


//Create Mail Sender address


address from = new InternetAddress (mailinfo.getfromaddress ());


//Set the sender of the mail message


Mailmessage.setfrom (from);


//Create the recipient address of the message and set it to the mail message


Address to = new InternetAddress (mailinfo.gettoaddress ());


Mailmessage.setrecipient (message.recipienttype.to,to);


//Set the subject of the mail message


Mailmessage.setsubject (Mailinfo.getsubject ());


//Set time to send mail messages


mailmessage.setsentdate (New Date ());


//Set the main content of the mail message


String mailcontent = Mailinfo.getcontent ();


Mailmessage.settext (mailcontent);


//Send mail


transport.send (MailMessage);


return true;


        }


catch (Messagingexception ex)


        {   


Ex.printstacktrace ();


        }   


return false;


    }


Send mail is mainly using the Mail.jar package inside the method, first of all, will use Myauthenticator class to judge some user authentication information, and then is to set up the message we collected above, and finally call the Transport.send () method to send us a set of good mail.

With me my personal test results, I was using QQ mailbox test, send the speed quickly, call the Send interface, basic immediately can receive mail. The demo provided below is also based on QQ mailbox, you can modify some of the mailbox parameters (SMTP, port and other information) to send to other mailbox servers. Using this method, I can send the exception information collected to our designated mailboxes by combining the previous article that collects the exception information of the program.

Finally, the point is that if you are directly in the background to send mail, it is best to give the user a hint, or let the user choose whether to send. Otherwise easy to back up rogue software or the background stolen traffic, O (∩_∩) o ha! Also need to pay attention to send the message frequency, avoid sending too frequently, the mail service provider has blocked your mail.

Provide QQ Email example: Click to download

Related Article

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.