Several ways to send Android mail

Source: Internet
Author: User

Send mail in Android I've probably found 3 of them, the code is as follows

Package Src.icetest;  Import org.apache.commons.mail.EmailException;    Import Org.apache.commons.mail.HtmlEmail;  Import;  Import android.content.Intent;  Import Android.os.Bundle;    Import Android.util.Log;  Public classIcetestactivity extends Activity {/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);          Setcontentview (R.layout.main); LOG.I ("icetestactivity","start ice test Step 1"); //sendmailintent (); //Sendmailbyapache (); Sendmailbyjavamail (); }        //You need config the Mail app in your Android moble first,and the mail would send by the Mail app. And there is one bi G BUG://You can ' t send the mail silently and your need to click the Send button     Public intsendmailbyintent () {string[] reciver=NewString[] {"[email protected]" }; String[] Mysbuject=NewString[] {"Test" }; String MYCC="cc"; String Mybody="Test Email Intent"; Intent myintent=NewIntent (Android.content.Intent.ACTION_SEND); Myintent.settype ("Plain/text");          Myintent.putextra (Android.content.Intent.EXTRA_EMAIL, reciver);          Myintent.putextra (Android.content.Intent.EXTRA_CC, MYCC);          Myintent.putextra (Android.content.Intent.EXTRA_SUBJECT, mysbuject);          Myintent.putextra (Android.content.Intent.EXTRA_TEXT, mybody); StartActivity (Intent.createchooser (myintent,"Mail Test")); return 1; }     /*This method can ' t is used in the Android mobile successful,but it can run normally in PC.  Because It'll cause the Java.lang.NoClassDefFoundError:javax.activation.DataHandler error may be there is some     To Solove it ... there is always javax the package is found in Android virtual Mobile. By the the-the method use Apache mail jar*/       Public intSendmailbyapache () {Try{htmlemail Email=NewHtmlemail (); //This is the name of the sending server.Email.sethostname (""); //setting of the encoding setEmail.settls (true); Email.setssl (true); Email.setcharset ("GBK"); //Recipient's MailboxEmail.addto ("[email protected]"); //Sender's MailboxEmail.setfrom ("[email protected]"); //If authentication information is required, set up authentication: username-password. The registered name and password of the sender on the mail server, respectivelyEmail.setauthentication ("wcf1000","00000"); Email.setsubject ("Test Email Apache"); //the information to be sentEmail.setmsg ("Test Email Apache"); //SendEmail.send (); } Catch(emailexception e) {//TODO auto-generated Catch blockLOG.I ("icetestactivity", E.getmessage ()); }            return 1; }  /** This method with JavaMail for Android, it's a good jar, * can see the demo inHttp:// _in_android * and you also need three jars, which I offered in Attachement * **/       Public intSendmailbyjavamail () {Mail m=NewMail ("[email protected]","XXXXX"); M.set_debuggable (true); String[] Toarr= {"[email protected]"};          M.set_to (Toarr); M.set_from ("[email protected]"); M.set_subject ("This was an e- mail sent using icetest from an Android device"); M.setbody ("Email body. Test by Java Mail"); Try {              //m.addattachment ("/sdcard/filelocation");             if(M.send ()) {LOG.I ("icetestactivity","Email was sent successfully."); } Else{log.i ("icetestactivity","Email was sent failed."); }          } Catch(Exception e) {//Toast.maketext (Mailapp.this,//"There was a problem sending the email.",//Toast.length_long). Show (); LOG.E ("Mailapp","Could not send email", E); }            return 1; }  }  

The first method is to call the system's Mail app, you first configure the system's mail app, but the biggest problem with this method is that after you run this method

He will not send the mail by default, but pop up the Mail app interface, you need to manually click Send,

The second is to call Apache's Common library, which works on the PC, but it will get an error in the Android virtual machine Java.lang.NoClassDefFoundError:javax.activation.DataHandler Error

Javax package can not be found, I looked at this question is still more common, we generally say that the virtual machine is castrated version, Javax seems to exist, this can not actually run

Third, the actual is javamail someone did the transplant, specifically for Android to do the development, this is better, the online demo code is also relatively in place, there is only one problem, is to add a, and stmp to manually add.

In fact, there should be a kind of, their own implementation of the SMTP server, the entire use of socket programming, direct communication with the target server, the win below I wrote, but the android will forget, and the long-term face of the SMTP server will be carried out after the direction of query to improve security.

Several ways to send Android mail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.