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. )
Public Boolean isnetworkconnected (context context) {ifnull== Mconnectivitymanager.getactivenetworkinfo (); if NULL ) {return mnetworkinfo.isavailable ();}} return false;
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, but can also be manipulated in service servicesclassSendMessage extends Thread {@Override Public voidrun () {//TODO auto-generated Method StubSuper.run (); Try { //creating the Htmlemail classHtmlemail email =NewHtmlemail (); //The host of the message is filled out, and I'm using 163.Email.sethostname ("smtp.163.com"); Email.settls (true); Email.setssl (true); //set character encoding format to prevent Chinese garbled charactersEmail.setcharset ("GBK"); //set the recipient's mailboxEmail.addto ("[email protected]"); //Set the sender's mailboxEmail.setfrom ("[email protected]"); //fill in the sender's user name and passwordEmail.setauthentication ("123","123"); //fill in the message subjectEmail.setsubject ("Hello"); //fill in the email contentEmail.setmsg (S1 +"\ n"+S2); //Send mailEmail.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.
The Mail jar package required for Android to send messages
Android development of the implementation of the Send mail function (source code sharing)