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

Source: Internet
Author: User
Tags http post mailmessage

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 server that sent the message
Private String Mailserverhost;
Private String Mailserverport = "25";

Address of the sender of the message
Private String fromaddress;
Address of the Mail recipient
Private String toaddress;
User name and password for the login mail sending server
Private String UserName;
private String password;
Whether authentication is required
Private Boolean validate = true;
Message subject
Private String subject;
Text content of a message
Private String content;
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)
{
To determine whether an identity certificate 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 ());
}
Constructs a session that sends a message based on the message conversation properties and the password validator
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 a 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 a mail message
Mailmessage.setsubject (Mailinfo.getsubject ());
Set when mail messages are sent
Mailmessage.setsentdate (New Date ());
Set the main contents of a 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

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.