We send letters from midlet or How I remotely caught errors

    Many probably came up with the idea that sending letters from the application to e-mail would be very helpful. For example, as a feedback about the program’s work, or with registration data, but for what else. So when developing the program I wanted to receive error messages in the program, since being an indie developer, I have a dozen or two of the most common phones on hand, and it’s not so easy to conduct large-scale testing on a wide range of devices, especially new ones. Therefore, a bug notification mechanism would be very helpful, at least at the beta stage. Rummaging through the network, I found several libraries for working with e-mail, including mobilab. But no matter how hard I tried, I still couldn’t send a letter, although the demos worked. And time was running out.
    As popular wisdom says

    Do you want to do well, do it yourself


    I found the source code of the mail client on this site , and at least it worked. But unfortunately I found problems with the encoding of special characters and Cyrillic. I had to quickly add my own string encoder. I designed everything in the library, wrote a class for working with it, and added it to the application. At first it was a separate item from which users could, in which case, send messages that came to my mail.
    image
    Later I came to the conclusion that it would be more reliable to automate this business, having previously warned users about collecting information about errors.
    image
    As a result, the final sending code became:

    } catch (Exception ex ){
     sendMail("main class","some method" , ex); 
    }
    public void sendMail(string classname, String methodname, Exception e)
    {
    SendMail mail=new SendMail();
    mail.setText(classname+":"+methodname+":"+ e.toString());
    mail.start();
    }
    


    And my letters will look something like this
    image

    And actually the sender handler

    import Mail.Connection;
    import Mail.Decoder;
    import Mail.Message;
    import Mail.SmtpClient;
    public class SendMail extends Thread {
     String host = "smtp.mail.ru";
     int port=25;
     String adressfrom = "bugreport@mail.ru";
     String pass = "123456";
     String adressto = "s.komlach@gmail.com";
     String subject = "Bugreport";
     String text = "";
        public void run() {
            try {
                String string = Decoder.encode(text, false);
                SmtpClient smtpclient = new SmtpClient(new Connection());
                smtpclient.open(host, port, adressfrom, pass);
                Message message = new Message(adressfrom, adressto, Decoder.encode(subject), true));
                message.addHeaderLine("X-mailer: 1.0");
                message.addHeaderLine("Content-Type: text/plain; charset=UTF-8");
                message.addHeaderLine("Content-Transfer-Encoding: quoted-printable");
                message.addBodyLine(string.concat("\r\n"));
                smtpclient.sendMessage(message);
                smtpclient.close();
            } catch (Exception exception) {
            }
        }
        public void setText(String text) {
            this.text = text;
        }
    }
    


    Well, goo.gl/oVv8e library sources

    Also popular now: