📜 ⬆️ ⬇️

We send letters from the midlet or How I remotely caught errors

Many probably had the idea that sending emails from an application would be very helpful. For example, as a feedback on the work of the program, or with registration data, and why else. So, when developing a program, I wanted to receive messages with errors in the program, since being an indie developer, I have a dozen other popular phones on hand, and it is not so easy to conduct large-scale testing on a wide range of devices, especially new ones. Therefore, the error notification mechanism would be very helpful, at least at the beta testing stage. Having rummaged in a network found several libraries for work with e-mail, including from mobilab'a. But no matter how hard I tried, I never managed to send a letter, although the demos worked. And time was running out.
As popular wisdom says,

Want to do well-do it yourself


I found the source 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 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 me in the mail.
image
Later I came to the conclusion that it would be safer to automate this business by notifying users in advance about the collection of information about errors.
image
As a result, the final dispatch code is as follows:

} 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 look something like this
image
')
And actually handler sender

 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, the source code of the library goo.gl/oVv8e

Source: https://habr.com/ru/post/146646/


All Articles