Today, we will introduce a simpler method. After collecting exception information, we will send the relevant exception information to our specified mailbox through the background email sending method.
The last time I talked about how to collect error information about our released applications, so that we can debug and improve our programs. The last collection method mainly refers to converting the collected information into the request parameter through the Http post request and sending it to the server. For web developers, server processing is very simple. However, it is troublesome for many people who do not have the web. Today, we will introduce a simpler method. After collecting exception information, we will send the relevant exception information to our specified mailbox through the background email sending method.
This article is a practical article that does not involve too much theoretical analysis. It mainly shows you how to add this function to your application.
1. Third-party Libraries
Three third-party libraries are required to send background emails this time. These libraries should be well known in java. I used to be a java email developer. I should have used it more or less. Android sends emails by default. You need to call the system email program through Intent, which is not practical for background operations like ours.
• Activation. jar
• Additionnal. jar
• Mail. jar
The three packages will be attached in the Demo below. These packages also have a lot of resources online and can be downloaded on your own.
2. email information
Because we send emails in the background, we need to collect necessary information because users do not need to enter this information.
Copy codeThe Code is as follows:
{
// IP address and port of the server sending the mail
Private String mailServerHost;
Private String mailServerPort = "25 ";
// The sender's address
Private String fromAddress;
// Email recipient's address
Private String toAddress;
// Log on to the email sending server's username and password
Private String userName;
Private String password;
// Whether authentication is required
Private boolean validate = true;
// Email Subject
Private String subject;
// Text of the email
Private String content;
// File Name of the email attachment
Private String [] attachFileNames;
}
The above are all the information we need to use when sending emails. Note that we need to provide sensitive information such as the account and password when sending background emails. You can write the email information in the program so that you do not need to enter any information when sending the email.
3. Email sending
Copy codeThe Code is as follows:
Public boolean sendTextMail (MailSenderInfo mailInfo)
{
// Determine whether identity authentication is required
MyAuthenticator authenticator = null;
Properties pro = mailInfo. getProperties ();
If (mailInfo. isValidate ())
{
// If You Need identity authentication, create a password authenticator
Authenticator = new MyAuthenticator (mailInfo. getUserName (), mailInfo. getPassword ());
}
// Construct a mail sending session based on the mail session attribute and the password validator
Session sendMailSession = Session. getDefaultInstance (pro, authenticator );
Try
{
// Create an email message based on the session
Message mailMessage = new MimeMessage (sendMailSession );
// Create the mail sender address
Address from = new InternetAddress (mailInfo. getFromAddress ());
// Set the sender of the email message
MailMessage. setFrom (from );
// Create the email receiver address and set it to the email message
Address to = new InternetAddress (mailInfo. getToAddress ());
MailMessage. setRecipient (Message. RecipientType. TO, );
// Set the subject of the email message
MailMessage. setSubject (mailInfo. getSubject ());
// Set the mail message sending time
MailMessage. setSentDate (new Date ());
// Set the main content of the email message
String mailContent = mailInfo. getContent ();
MailMessage. setText (mailContent );
// Send an email
Transport. send (mailMessage );
Return true;
}
Catch (MessagingException ex)
{
Ex. printStackTrace ();
}
Return false;
}
Mail is used to send emails. the method in the jar package first uses the MyAuthenticator class to determine some user authentication information, then sets the email information we collected above, and finally calls Transport. send () method to send the email we set.
I personally tested the results with me. I used my QQ mail to test the results. The sending speed was very fast. I called the sending interface and basically got an email right away. The Demo example provided below is based on QQ mail. You can modify some mailbox parameters (smtp, port, and other information) as needed and send them to other mailbox servers. Using this method, combined with my previous article on collecting program exception information, we can send the collected exception information to our specified mailbox.
The last note is that if you send an email directly in the background, you 'd better give the user a prompt or ask the user to choose whether to send the email. Otherwise, it is easy to name the rogue software or the backend stealing traffic! In addition, pay attention to the frequency of sending emails to avoid sending too frequently. The email service provider shields your emails.
Example of sending an email via QQ mail: Click to download