Android background mail sample of collect application exception information +Demo code

  • 2020-05-17 06:27:24
  • OfStack

Last time, we talked about how to collect the error information of our published application so that we can debug and improve the application. The collection method described last time is mainly to send the collected information to the server through the post request of Http and the relevant exception information into the request parameters. For those of you who have done web development, server-side processing is simple. But for many people who don't have an web, it's a hassle. Today, we will introduce a simpler method. After collecting the exception information, we will send the relevant exception information to our designated mailbox through the background email method.

This article is a practical one and will not involve much theoretical analysis. The main thing is to let you know how to add this function in your own application.

1. Third square library
The sending of background mail requires the use of three third side libraries, which should be well-known in java. I used to be a friend of java email development, and I think I have used it more or less. The default method of Android to send mail needs to call the system mail program through Intent, which is not practical for our background operation.

•activation.jar
•additionnal.jar
•mail.jar

In the following example of Demo, I will attach these three packages. These packages are also full of resources on the Internet, which can be downloaded by yourself.

2. Email messages
Because we are sending emails in the background, we need to collect some necessary information, because we do not need users to enter this information.


{
    //  Of the server that sent the mail IP And port     
    private String mailServerHost;    
    private String mailServerPort = "25";   

    //  The address of the sender     
    private String fromAddress;    
    //  The address of the recipient     
    private String toAddress;    
    //  Login the username and password of the mail sending server     
    private String userName;    
    private String password;    
    //  Whether authentication is required     
    private boolean validate = true;    
    //  Email subject     
    private String subject;    
    //  The text content of the message     
    private String content;    
    //  The file name of the email attachment     
    private String[] attachFileNames;      
}

These are all the information we need to send emails. What needs to be noted here is that we need to give sensitive information such as account password when sending background emails. These messages can be programmed so that when we send them, we don't need the user to enter any information.

3. Send emails


public boolean sendTextMail(MailSenderInfo mailInfo) 
    {
        //  Determine if identification is required     
        MyAuthenticator authenticator = null;    
        Properties pro = mailInfo.getProperties();   
        if (mailInfo.isValidate()) 
        {    
            //  If identity is required, create 1 Two password validators     
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());    
        }   
        //  Constructed from mail session properties and password validators 1 The person who sent the email session    
        Session sendMailSession = Session.getDefaultInstance(pro,authenticator);    
        try 
        {    
            //  According to the session create 1 Email messages     
            Message mailMessage = new MimeMessage(sendMailSession);    
            //  Create the email 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 the time for the mail message to be sent     
            mailMessage.setSentDate(new Date());    
            //  Sets the main content of the mail message     
            String mailContent = mailInfo.getContent();    
            mailMessage.setText(mailContent);    
            //  Send E-mail     
            Transport.send(mailMessage);   
            return true;    
        } 
        catch (MessagingException ex) 
        {    
            ex.printStackTrace();    
        }    
        return false;    
    }

The method in mail.jar package is mainly used to send the mail. First, MyAuthenticator class will be used to judge some user authentication information, and then we will set up the mail information collected above. Finally, Transport.send () method will be called to send the mail we set up.

I personally tested the effect with QQ mailbox, and the sending speed was very fast. The sending interface was called, and the mail could be received almost immediately. The following example of Demo is also based on QQ mailbox. You can modify some mailbox parameters (smtp, port, etc.) to send to other mailbox servers as needed. Using this method, combined with my previous article on collecting program exception information, we can send the collected exception information to our designated mailbox.

Last but not least, if you're sending an email directly in the background, it's best to give the user a hint, or let the user choose whether to send or not. Otherwise, it is easy to be accused of rogue software or background sneaking traffic. O(∩_∩)O ha! Also need to pay attention to the frequency of the email, to avoid sending too often, mail service providers to your email blocked.

Provide QQ mailbox to send mail example: click download


Related articles: