Android Development may encounter how to send mail trouble, before I also checked the relevant documents, Bo Friends also shared a lot of ways to send mail, a total of 3 kinds of put, I carefully read the next, found that some talk too complicated with trouble, not clear, I'm here today to share what I think is the best way to solve this problem, using the Andorid-mail jar package. First of all, we need to download the following four files first
Then, on the Android platform to send mail, must first check the status of the network, network status is available to send mail success AH (do not go to obtain network information need to add the appropriate permissions in the Androidmanifest.xml file. <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>)
The next step is even easier to read the code comments directly, we can solve the need to send mail!
Network connection-related operations are done in a sub-thread, of course, you can also operate the service services class SendMessage extends Thread {@Overridepublic void Run () {//TODO Auto-generated method Stubsuper.run (); try {//Create Htmlemail class Htmlemail email = new Htmlemail ();//fill in the message host, I am using 163email.sethostname ("smtp.163.com"), Email.settls (True), Email.setssl (TRUE);//Set character encoding format, Prevent Chinese garbled email.setcharset ("GBK");//Set the recipient's mailbox Email.addto ("[email protected]");//Set the sender's mailbox Email.setfrom ("[Email Protected]///fill in the sender's username and password email.setauthentication ("123", "123");//fill in the subject of the message email.setsubject ("Hello");// Fill in the Message content email.setmsg (S1 + "\ n" + s2);//Send mail Email.send ();} catch (Emailexception e) {//TODO auto-generated catch blocklog.i ("TAG", "---------------->" +e.getmessage ());}}}
Better than the completion of the Android Send mail function, you can also study the other two ways to send mail. Share the shared address of the jar package that you have just described here.
Mail's jar package for Android to send mail