Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Sending Emails without User Intervention (no Intents) in Android

The Android SDK makes it very easy to send emails from an application, but unfortunately, that's only if you want to send them via the built-in mailing app. For most situations this works fine, but if you want to send something out and don't want any input/intervention from the user, it's not as easy.

In this article I'm going to show you how to send an email in the background without the user even knowing - the application will do everything behind the scenes.

Before we begin, you'll need to download a few files via the link below - this is a special version of the JavaMail API, which was written specifically for Android.

http://code.google.com/p/javamail-android/downloads/list

I'll be walking you through a Mail wrapper that I wrote, which makes it much easier to send emails and even add attachments if that's something you'd like to do.

Here is the full wrapper class below, which I'll go through step by step - keeping in mind that you'll have to add the fore-said files if you want this to work. Add them as external libraries - they need to be accessible by the Mail class.


import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class Mail extends javax.mail.Authenticator {
  private String _user;
  private String _pass;

  private String[] _to;
  private String _from;

  private String _port;
  private String _sport;

  private String _host;

  private String _subject;
  private String _body;

  private boolean _auth;
  
  private boolean _debuggable;

  private Multipart _multipart;


  public Mail() {
    _host = "smtp.gmail.com"; // default smtp server
    _port = "465"; // default smtp port
    _sport = "465"; // default socketfactory port

    _user = ""; // username
    _pass = ""; // password
    _from = ""; // email sent from
    _subject = ""; // email subject
    _body = ""; // email body

    _debuggable = false; // debug mode on or off - default off
    _auth = true; // smtp authentication - default on

    _multipart = new MimeMultipart();

    // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    CommandMap.setDefaultCommandMap(mc);
  }

  public Mail(String user, String pass) {
    this();

    _user = user;
    _pass = pass;
  }

  public boolean send() throws Exception {
    Properties props = _setProperties();

    if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
      Session session = Session.getInstance(props, this);

      MimeMessage msg = new MimeMessage(session);

      msg.setFrom(new InternetAddress(_from));
      
      InternetAddress[] addressTo = new InternetAddress[_to.length];
      for (int i = 0; i < _to.length; i++) {
        addressTo[i] = new InternetAddress(_to[i]);
      }
	    msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

      msg.setSubject(_subject);
      msg.setSentDate(new Date());

      // setup message body
      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(_body);
      _multipart.addBodyPart(messageBodyPart);

      // Put parts in message
      msg.setContent(_multipart);

      // send email
      Transport.send(msg);

      return true;
    } else {
      return false;
    }
  }

  public void addAttachment(String filename) throws Exception {
    BodyPart messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);

    _multipart.addBodyPart(messageBodyPart);
  }

  @Override
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(_user, _pass);
  }

  private Properties _setProperties() {
    Properties props = new Properties();

    props.put("mail.smtp.host", _host);

    if(_debuggable) {
      props.put("mail.debug", "true");
    }

    if(_auth) {
      props.put("mail.smtp.auth", "true");
    }

    props.put("mail.smtp.port", _port);
    props.put("mail.smtp.socketFactory.port", _sport);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    return props;
  }

  // the getters and setters
  public String getBody() {
    return _body;
  }

  public void setBody(String _body) {
    this._body = _body;
  }

  // more of the getters and setters …..
}

And now I'm going to go through each bit of code


public Mail() {
  _host = "smtp.gmail.com"; // default smtp server
  _port = "465"; // default smtp port
  _sport = "465"; // default socketfactory port

  _user = ""; // username
  _pass = ""; // password
  _from = ""; // email sent from
  _subject = ""; // email subject
  _body = ""; // email body

  _debuggable = false; // debug mode on or off - default off
  _auth = true; // smtp authentication - default on

  _multipart = new MimeMultipart();

  // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
  MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  CommandMap.setDefaultCommandMap(mc);
}

public Mail(String user, String pass) {
  this();

  _user = user;
  _pass = pass;
}

In this piece of code we initialise the properties, and setup the default values.

Also, we're setting up the mime types for javamail. I've also added a comment which describes why we need this.

And you've probably noticed that there are 2 constructors - one overrides the other, just incase the you want to pass the username and password when instantiating the class.


public boolean send() throws Exception {
  Properties props = _setProperties();

  if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
    Session session = Session.getInstance(props, this);

    MimeMessage msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(_from));
      
    InternetAddress[] addressTo = new InternetAddress[_to.length];
    for (int i = 0; i < _to.length; i++) {
      addressTo[i] = new InternetAddress(_to[i]);
    }
    msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

    msg.setSubject(_subject);
    msg.setSentDate(new Date());

    // setup message body
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(_body);
    _multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    msg.setContent(_multipart);

    // send email
    Transport.send(msg);

    return true;
  } else {
    return false;
  }
}

This is the most important method - here we're putting all the data from the properties and sending the mail.


public void addAttachment(String filename) throws Exception {
  BodyPart messageBodyPart = new MimeBodyPart();
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(filename);

  _multipart.addBodyPart(messageBodyPart);
}

You can call this method at any time if you want to add an attachment, but make sure you call it before the send method.


private Properties _setProperties() {
  Properties props = new Properties();

  props.put("mail.smtp.host", _host);

  if(_debuggable) {
    props.put("mail.debug", "true");
  }

  if(_auth) {
    props.put("mail.smtp.auth", "true");
  }

  props.put("mail.smtp.port", _port);
  props.put("mail.smtp.socketFactory.port", _sport);
  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.socketFactory.fallback", "false");

  return props;
}

Here we're setting up the properties for the mail retrieval - defaulting to SMTP authentication.

Also keep in mind that this is all defaulted to connect to the Gmail (Google) SMTP server.

Below is an example of how to use the Mail wrapper, in an Android activity.


@Override
public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  Button addImage = (Button) findViewById(R.id.send_email);
  addImage.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      Mail m = new Mail("gmailusername@gmail.com", "password");

      String[] toArr = {"bla@bla.com", "lala@lala.com"};
      m.setTo(toArr);
      m.setFrom("wooo@wooo.com");
      m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
      m.setBody("Email body.");

      try {
        m.addAttachment("/sdcard/filelocation");

        if(m.send()) {
          Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
        } else {
          Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show();
        }
      } 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);
      }
    }
  });
}


jon | May 19, 2010 | Comments (366)

Comments

http://leonardjrush.livejournal.com/572.html
Comment by What's up, I log on to your new stuff daily. Your humoristic style is awesome, keep it up! - December 15, 2014 @ 11:38 am
http://www.wiballoongroup.org/guestbook/index.php?page%3D1
Comment by If ʏou desirе to obtain much frlm this article thwn yoou have to apply these teϲhniques to your w - December 12, 2014 @ 6:01 pm
http://www.nlpedia.net/wiki/index.php?title=User:MammieFlinders
Comment by Τhɑnk you for the auspicioսs writeup. It in fact was once a entertainment account it. Look ϲom - December 12, 2014 @ 5:57 pm
http://realsupportpsychics.com/browse-categories/psychic-readings-psychics/
Comment by It is perfect time to make a few plans for the future and it is time to be happy. I've read this p - December 12, 2014 @ 3:35 pm
sir my above code goes into the else part saying , email was not sent , need some help sir
please reply
Comment by Arjun Narahari - December 12, 2014 @ 2:33 pm
http://genericviagra2015shop.com/
Comment by Hello, i believe that i noticed you visited my web site thus i came to return the want?.I am attempt - December 11, 2014 @ 1:08 pm
http://zensenergy.com/quick-cures-home-remedies-for-acid-reflux/
Comment by Truly when someone doesn't be aware of then its up to other users that they will help, so here it oc - December 10, 2014 @ 3:43 pm
http://viagraonline2015shopusa.com
Comment by This site was... how do you say it? Relevant!! Finally I've found something that helped me. Thank y - December 09, 2014 @ 10:29 am
http://ms-york.com/
Comment by If some one wants expert view concerning blogging after that i propose him/her to visit this blog, - December 08, 2014 @ 7:31 pm
http://viagra-101.com/
Comment by I'm not positive where you are getting your info, but good topic. I must spend some time learning m - December 08, 2014 @ 10:44 am
say thanks to so considerably for your website it assists a lot. In case you loved this information and you want to receive much more information regarding true m30 treadmill reviews generously visit our own web site.| http://findyourtreadmill.com
Comment by Denny - December 06, 2014 @ 8:37 am
https://www.behance.net/gallery/21586501/Academia-Researchcom-for-Those-Who-Need-a-Writing-Job?
Comment by I loved as much as you will receive carried out right here. The sketch is attractive, your authored - December 04, 2014 @ 9:42 pm
http://24cialisitalia.com/
Comment by Helpful information. Lucky me I discovered your site by chance, and I am shocked why this twist of - December 04, 2014 @ 9:49 am
http://softwaresforpc.net/driver-toolkit-8-3-5-crack-license-key-download/
Comment by Hi there! I could have sworn I've been to this website before but after reading through some of th - December 01, 2014 @ 3:38 am
http://www.trickslove.com/p/driver-support-review.html
Comment by Hmm is anyone else experiencing problems with the pictures on this blog loading? I'm trying to figu - November 30, 2014 @ 1:29 pm
Great looking web site. Presume you did a bunch of your very own coding. For those who have almost any concerns concerning where as well as how to make use of treadmill reviews 2013 canada, you possibly can e-mail us on our web page.} http://findyourtreadmill.com
Comment by Kourtney - November 26, 2014 @ 11:27 am
What's happening, excellent web site you possess at this time there. If you loved this article and you would certainly such as to receive additional facts pertaining to treadmill reviews australian kindly check out our own page.| http://findyourtreadmill.com/
Comment by Iva - November 26, 2014 @ 11:22 am
http://spaces.hand-china.com/home/link.php?url=http://www.dogtrainingsystems.net/
Comment by I am no longer certain where you are getting your info, but good topic. I needs to spend some time - November 25, 2014 @ 12:11 pm
https://www.youtube.com/watch?v=83CPQaEzGj8
Comment by Hi there, i read your blog from time to time and i own a similar one and i was just curious if you - November 10, 2014 @ 9:49 am
http://www.zensenergy.com/how-to-get-rid-of-gas-pains-for-good/
Comment by I blog often and I genuinely appreciate your information. This article has truly peaked my interes - November 10, 2014 @ 4:02 am
http://documentation.lastria.fr/index.php?title=The_Importance_of_Expectations
Comment by I muust thank you for the efforts you've put in penning this website. I'm hoping to view the same h - November 07, 2014 @ 10:16 am
http://questwriters.org/buy-thesis-paper.php
Comment by Hi there friends, hߋw is еverything, and wɦat yօu wɑnt to saү about this post, іn my view i - November 05, 2014 @ 7:42 am
http://questwriters.org/buy-scholarship-essay.php
Comment by Great blog right here! Additionally your site a lot up very fast! What host are you using? Can I a - November 04, 2014 @ 7:54 am
http://www.cubicleaccessories.org/
Comment by Established in 1993, MAi - SPACE has approximately 250 employees worldwide, and a North American in - November 03, 2014 @ 3:38 am
http://www.ms-york.com/buy-instagram-likes/
Comment by Do you mind if I quote a few of your articles as long as I provide credit and sources back to your b - November 01, 2014 @ 4:52 am
http://www.centre-affaires-brest.fr/index.php/le-blog/item/4-environnement-du-centre-daffaires-c%c3%b4t%c3%a9-mer.html
Comment by I was wondering if you ever considered changijng the structure of your blog? Itts very well written - October 27, 2014 @ 2:39 am
http://sellphotos.skyrock.com
Comment by I like what you guys tend to be up too. Such clever work and exposure! Keep up the awesome works g - October 26, 2014 @ 10:29 pm
http://camsunlocked.tumblr.com
Comment by Heyy just ԝanted to ɡive уou a quick headxs սp and let yοu know a few of the images ɑren't l - October 24, 2014 @ 10:50 am
http://mchaneyfamilydentistry.com/, http://mchaneyfamilydentistry.com/contact-us/
Comment by Hello! Somеone in my Facebook grouρ shared this website wіth us so I сame to ǥive it a lօok. - October 17, 2014 @ 1:00 am
http://blog.wetandwizz.com/nicoleta/
Comment by What i do not realize is actually how you are now not actually a lot more well-liked than you might - October 16, 2014 @ 3:28 am
Works perfectly in emulator..I tried to run same code in phone using apk ...mail was not sent ...
android version of my phone is 4.0.3.....Any suggestions??
Comment by Vidya jangam - October 15, 2014 @ 8:25 am
http://www.brickpaverstampa.org/
Comment by Your means of telling the whole thing in this article is really good, all be able to effortlessly un - October 14, 2014 @ 12:47 pm
This code Works Fine with One Attachment But How To attach More than One File To Send Mail
Can u Plz Help Me....
Comment by Nilesh - October 13, 2014 @ 2:50 pm
While think this kind of Real estate property industry hope against hope turn back in the positive course? Or maybe can it be at a standstill very young to tell? We're glimpse many terracing property foreclosures modish afbeefadgbbf
Comment by Johng287630 - October 12, 2014 @ 7:16 am
http://mt.57883.net/alexa/mt/index.asp?domain=seforums.se
Comment by Thanks for sharing such a good thinking, article is fastidious, thats why i have read it entirely - October 06, 2014 @ 10:47 pm
http://Www.Shoucang.info/plus/guestbook/index.php
Comment by Link exchange is nothing else except it is only placing the other person's weblog link on your page - October 05, 2014 @ 9:50 am
http://support.giambrone.de/pizzeria/index.php?page=User&userID=160993
Comment by I've been surfing online more than 4 hours today, yet I never found any interesting article like y - October 04, 2014 @ 4:04 am
http://contraincendios.tumblr.com/
Comment by Franklin - September 26, 2014 @ 9:04 pm
http://hotorgasm.wordpress.com/
Comment by Hello! I've been following your web site for some time now and finally got the courage to go ahe - September 23, 2014 @ 4:04 pm
http://sir.net.au/ForumRetrieve.aspx?ForumID=317&TopicID=480054&NoTemplate=False
Comment by Diego - September 22, 2014 @ 3:33 pm
http://microwavemaven.drupalgardens.com/content/whats-best-microwave-oven-year
Comment by Bryon - September 19, 2014 @ 10:28 pm
http://www.Jazz-standards.com/
Comment by Felicia - September 19, 2014 @ 12:07 pm
http://globalgoldandsilver.com/sell-diamonds
Comment by Sunny - September 19, 2014 @ 4:16 am
http://www.jazz-standards.com
Comment by Juli - September 17, 2014 @ 11:55 am
Thanks alot for this wrapper!
Comment by Ninad Sonje - September 12, 2014 @ 4:32 am
http://www.jazz-standards.com/buy-instagram-followers/
Comment by Roma - September 05, 2014 @ 10:49 pm
http://www.sos-zahnaerzte.ch/
Comment by Paula - September 04, 2014 @ 1:04 am
https://itetralee.zendesk.com/entries/98928026-Where-To-Uncover-The-Correct-Canvas-Artwork-For-Your-Home
Comment by Emilio - September 02, 2014 @ 3:05 am
http://www.myapplemarketplace.com/2012/09/a-former-kentish-bakery.html
Comment by Aleisha - September 02, 2014 @ 2:28 am
https://tavernkeeperrp.zendesk.com/entries/96888363-Finishing-Your-Canvas-Print-With-Lacquer
Comment by Denise - September 01, 2014 @ 10:58 pm
A megapolis megabucks gratuit failed to seem to cause waste materials in addition dddceeaedaef
Comment by Johnk451614 - August 25, 2014 @ 10:28 pm
http://Www.siteforgolfers.com/how-to-putt-better
Comment by Rudy - August 25, 2014 @ 4:57 am
http://www.jazz-standards.com
Comment by Trisha - August 24, 2014 @ 9:50 am
http://www.pegpropgroup.co.za/UserProfile/tabid/91/userId/59484/Default.aspx
Comment by Karolyn - August 23, 2014 @ 5:24 am
http://www.siteforgolfers.com/how-to-putt-better
Comment by Ronald - August 22, 2014 @ 2:08 pm
its working .Thanks a lot.
Comment by Mohd Irfan - August 22, 2014 @ 5:07 am
http://drugstoredir2014.com
Comment by Dannielle - August 11, 2014 @ 10:36 am
http://themarketingheaven.com/buy-facebook-likes/
Comment by Roseann - August 09, 2014 @ 2:46 am
http://s6x.it/jr66
Comment by Laurence - August 04, 2014 @ 11:18 am
http://www.wholesalecontractfurniture.com/category/8-resin-outdoor-furniture.aspx
Comment by Fernando - August 03, 2014 @ 10:01 am
http://sellinggoldnj.com/
Comment by Camilla - August 02, 2014 @ 1:43 pm
http://xn--17g.ws/malpracticeinflorida662240
Comment by Sabine - July 31, 2014 @ 12:45 am
http://drugstoredir2014.com
Comment by Francine - July 27, 2014 @ 10:11 pm
http://xemaker.com/xe/?document_srl=257766
Comment by Milagro - July 21, 2014 @ 6:00 pm
http://www.jazz-standards.com
Comment by Juanita - July 19, 2014 @ 8:47 pm
http://www.wholesalecontractfurniture.com/manufacturer/2-nardi-furniture.aspx
Comment by Florian - July 19, 2014 @ 6:58 pm
http://www.lucyleeds.co.uk/
Comment by Lakesha - July 15, 2014 @ 2:03 pm
http://wiki.mr-em-forum.org/index.php?title=Benutzer:ArethaWeld
Comment by Ross - July 14, 2014 @ 4:13 am
http://www.lucyleeds.co.uk/
Comment by Leonard - July 13, 2014 @ 10:49 pm
For those who are having an issue with this code ...

Follow This link it has the full code (Full Project to download)
Link: http://www.tiemenschut.com/how-to-send-e-mail-directly-from-android-application/

Then, you have to make sure that in your Manifest.xml File -Delete- the uses-sdk ..... which ever you are using.. then your good to go..

sorry i forgot to add what you should delete :P

Hope this will help you to save time :D
It worked for me!!!!
Comment by MMAB - July 09, 2014 @ 11:47 pm
For those who are having an issue with this code ...

Follow This link it has the full code (Full Project to download)
Link: http://www.tiemenschut.com/how-to-send-e-mail-directly-from-android-application/

Then, you have to make sure that in your Manifest.xml File -Delete- which ever you are using.. then your good to go..

Hope this will help you to save time :D
It worked for me!!!!
Comment by MMAB - July 09, 2014 @ 11:44 pm
https://www.youtube.com/watch?v=aSB5Lfnj4FA
Comment by Monte - July 08, 2014 @ 2:22 am
http://lostbwanaoutfitters.com/
Comment by Wallace - July 03, 2014 @ 3:13 pm
http://v.gd/holders_grosfillex_restaruant_
Comment by Phyllis - June 23, 2014 @ 4:03 pm
http://auuooh.com
Comment by Benito - June 23, 2014 @ 9:27 am
http://www.adipexdispensary.com/phenobestin-375-c-25.aspx
Comment by Valorie - June 19, 2014 @ 2:35 am
http://www.whatarenootropics.com/forums/topic/joel-dicker-%c2%aben-cada-texto-como-en-el-tenis-perpetuamente-se-parte-de-cero%c2%bb/
Comment by Fausto - June 17, 2014 @ 12:29 am
http://www.jafrum.com/Motorcycle-Luggage/Saddlebags-for-Honda
Comment by Lela - June 11, 2014 @ 12:57 pm
http://youtu.be/pYzh3m5G724
Comment by Twila - June 09, 2014 @ 2:37 am
http://babystrollerbike.wordpress.com/
Comment by Geraldine - June 08, 2014 @ 1:38 pm
https://www.youtube.com/watch?v=08tM7KG6bxs
Comment by Sima - June 07, 2014 @ 6:30 pm
http://www.inf.udec.cl/~apolymer/Scuttle/www/bookmarks.php/qepjacqu
Comment by Nadia - June 06, 2014 @ 5:49 pm
http://karma-credit.s3-website-us-east-1.amazonaws.com/
Comment by Latashia - June 06, 2014 @ 5:07 pm
http://tasml.parsons.edu/weathertunnel/projects/how-to-stop-hair-decrease-in-7-days-day-3-4/
Comment by Chance - June 06, 2014 @ 2:31 am
http://skypeaanmaken.denjoe.com/
Comment by Cornelius - June 06, 2014 @ 12:24 am
http://www.scoop.it/t/electronic-cigarette-reviews-by-lewis-anderson
Comment by Jamika - May 29, 2014 @ 9:45 pm
Getting this error, somebody help, pleaseeeeeeeee

05-28 05:28:17.876: E/MailApp(401): Could not send email
05-28 05:28:17.876: E/MailApp(401): java.lang.NullPointerException
05-28 05:28:17.876: E/MailApp(401): at com.hotelcontinental.Mail.send(Mail.java:80)
05-28 05:28:17.876: E/MailApp(401): at com.hotelcontinental.MainActivity$1.onClick(MainActivity.java:31)
05-28 05:28:17.876: E/MailApp(401): at android.view.View.performClick(View.java:2408)
05-28 05:28:17.876: E/MailApp(401): at android.view.View$PerformClick.run(View.java:8816)
05-28 05:28:17.876: E/MailApp(401): at android.os.Handler.handleCallback(Handler.java:587)
05-28 05:28:17.876: E/MailApp(401): at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 05:28:17.876: E/MailApp(401): at android.os.Looper.loop(Looper.java:123)
05-28 05:28:17.876: E/MailApp(401): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-28 05:28:17.876: E/MailApp(401): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 05:28:17.876: E/MailApp(401): at java.lang.reflect.Method.invoke(Method.java:521)
05-28 05:28:17.876: E/MailApp(401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-28 05:28:17.876: E/MailApp(401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-28 05:28:17.876: E/MailApp(401): at dalvik.system.NativeStart.main(Native Method)
Comment by Victor - May 28, 2014 @ 1:34 am
http://www.prodomo.otten.pl/odsniezanie-dachow-koszalin/
Comment by Latrice - May 27, 2014 @ 5:32 pm
https://www.facebook.com/craen.dirk
Comment by Gale - May 26, 2014 @ 1:54 pm
http://r-forge.r-project.org/tracker/download.php/103/458/5532/3133/how-to-make-love-last-longer-how-to-make-love-longer-for-men-how-to-make-your-first-time-last-longer-how-to-make-yourself-last-longer-during-intercourse-how-to-overcome-premature-ejaculation-ebook.html
Comment by Roseann - May 25, 2014 @ 10:21 am
http://Www.Dddsn.com/natural-male-enhancement-pills-work/
Comment by Reynaldo - May 25, 2014 @ 7:07 am
http://www.osservatoriowelfare.it/news/phpinfo.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fwww.midasoftce.com%2F%3Eseo+consulting+fees%3C%2Fa%3E
Comment by Jamey - May 25, 2014 @ 6:24 am
http://www.wenlieren.com/benefits-lucid-dreaming/
Comment by Micheline - May 24, 2014 @ 2:54 pm
http://www.dddsn.com/natural-male-enhancement-herbs-gramps-never-told/
Comment by Marcy - May 24, 2014 @ 12:10 am
http://innerwealthsecretsreview.tumblr.com/
Comment by Buster - May 22, 2014 @ 11:49 pm
http://www.icrsa4.org/
Comment by Ulysses - May 21, 2014 @ 11:41 pm
http://www.at-udon.com
Comment by Katrice - May 20, 2014 @ 11:05 pm
http://theoldschoolnewbodyreview.tumblr.com/
Comment by Berry - May 20, 2014 @ 12:34 am
http://csswish.blogspot.in/2014/04/7-common-mistakes-most-blogger-do.html
Comment by NaveenKumar - May 18, 2014 @ 10:54 pm
http://octoba.net/wiki/Submission_Inside_Web_Directory_For_Seo_Benefits
Comment by Scott - May 15, 2014 @ 3:32 am
http://www.pleasantononlinereports.com
Comment by Regina - May 11, 2014 @ 7:59 pm
http://TheVisualImpactMuscleBuildingReview.blogspot.com/
Comment by Kassie - May 11, 2014 @ 4:48 am
http://www.infracoholdings.com
Comment by Leslee - May 10, 2014 @ 6:41 pm
http://melanotan-2.tumblr.com/post/84312853853/considering-melanotan-2
Comment by Sherman - May 04, 2014 @ 10:07 pm
http://www.yka21.net/xe/?document_srl=1474215
Comment by Cruz - May 04, 2014 @ 12:07 pm
http://www.infracoholdings.com
Comment by Rubin - April 30, 2014 @ 8:01 pm
http://www.caregiverproject.org
Comment by Jaimie - April 28, 2014 @ 3:36 am
http://www.dayonelog.com
Comment by Drew - April 25, 2014 @ 9:25 pm
http://www.mydebate.org
Comment by Shona - April 25, 2014 @ 3:23 am
https://www.facebook.com/2bservice
Comment by Keith - April 25, 2014 @ 1:25 am
http://bonlie-cookies.com
Comment by Dean - April 24, 2014 @ 10:20 am
http://www.infracoholdings.com
Comment by Merissa - April 24, 2014 @ 1:12 am
http://www.pleasantononlinereports.com
Comment by Angeline - April 23, 2014 @ 5:48 pm
in mail.java file i got the error below:

msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

TO cannot be resolved or is not a field
Comment by eray - April 23, 2014 @ 9:29 am
http://tennising.net/
Comment by Josefina - April 22, 2014 @ 9:39 pm
Fantastic website. A lot of useful information here. I'm sending it to a few pals ans also sharing in delicious. And naturally, thank you in your sweat! gedcaddgfacd
Comment by Pharme76823 - April 22, 2014 @ 9:05 pm
http://www.teclis-usa.com
Comment by Monroe - April 22, 2014 @ 5:27 am
http://www.gay-guides.org
Comment by Sandra - April 22, 2014 @ 4:57 am
http://www.at-udon.com
Comment by Lesley - April 21, 2014 @ 11:28 pm
http://as3nui.com/generic-viagra-best/
Comment by Leoma - April 21, 2014 @ 5:05 pm
http://www.at-udon.com
Comment by Denis - April 20, 2014 @ 9:04 am
http://www.caregiverproject.org
Comment by Antje - April 20, 2014 @ 6:31 am
https://th-th.facebook.com/ragnarok.ro.th
Comment by Rod - April 20, 2014 @ 3:54 am
http://mla.mrooms.org/user/view.php?id=1010083&course=1
Comment by Gonzalo - April 18, 2014 @ 6:55 pm
https://kbminjoo.kr:49748/localbackup/3515031
Comment by Hollie - April 18, 2014 @ 5:24 am
http://lareinedesneigesstreaming.blogspot.com/
Comment by Gabrielle - April 17, 2014 @ 8:39 pm
http://agrandirsonpenisnaturellement.com
Comment by Marylyn - April 16, 2014 @ 12:05 pm
http://www.ganbarou-miyazaki.net/news/item_12.html
Comment by Gabriele - April 15, 2014 @ 6:50 pm
http://newseoblog1.com.pl
Comment by Selina - April 15, 2014 @ 8:04 am
http://www.at-udon.com
Comment by Oscar - April 14, 2014 @ 10:27 pm
https://th-th.facebook.com/ragnarok.ro.th
Comment by Brianna - April 14, 2014 @ 9:24 pm
http://galaxistek.com/users.php?mode=profile&uid=89205
Comment by Bennett - April 14, 2014 @ 6:08 pm
http://www.pinterest.com/planetban/
Comment by Minerva - April 12, 2014 @ 4:21 pm
http://www.gay-guides.org
Comment by Anastasia - April 12, 2014 @ 3:13 pm
http://pharmpdf.com/
Comment by Kristian - April 10, 2014 @ 8:31 pm
http://marketingsauce.biz
Comment by Lottie - April 10, 2014 @ 8:12 pm
http://rap-rule.org
Comment by Jamal - April 10, 2014 @ 4:52 pm
http://www.worldpussy.info
Comment by Rosalinda - April 10, 2014 @ 12:30 am
http://www.at-udon.com
Comment by Keeley - April 09, 2014 @ 6:19 pm
http://www.at-udon.com
Comment by Guy - April 09, 2014 @ 2:47 pm
http://www.ukpersonalcredit.co.uk/Consolidate_Payday_loans_over_6_months_lower_rates_APR.html
Comment by Philip - April 05, 2014 @ 3:41 am
Fuck this shit :(
Where is the source code ?
Comment by Axdv - March 22, 2014 @ 2:53 pm
please send me the source in .zip
@ rushic24@gmail.com
Comment by Rushi - March 18, 2014 @ 11:58 am
http://gamin.isloco.com/category/
Comment by Yolanda - March 15, 2014 @ 7:50 pm
please send totla source.zip file
Comment by Sky - March 14, 2014 @ 1:47 am
https://www.facebook.com/dress.rent
Comment by Lorenzo - March 12, 2014 @ 5:18 am
https://suite101.com/peplegetrud/integratori-alimentari-online-tutto-cio-che-ce-da-sapere
Comment by Simon - March 12, 2014 @ 4:55 am
http://Www.Icrsa4.org/impress-girls/
Comment by Jerri - March 07, 2014 @ 10:06 pm
http://www.grupamagazin.com/guestbook-145-65/
Comment by Eric - March 05, 2014 @ 2:34 am
http://taininka.ru/index.php/gallery?func=detail&catid=8&id=32
Comment by Eulalia - March 04, 2014 @ 4:25 am
Finally i solved my problem, i had to use an AsyncTask, this might help you : http://stackoverflow.com/questions/14374578/using-asynctask-to-send-android-email. Thank you again for providing this code!!
Comment by Bonzo Floyd - February 27, 2014 @ 11:36 am
Hello!
It's saying : unfortunately, your app has stopped, and it's giving me only these 2 errors :
FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message

If anyone know what might be the problem please help me. Thank you in advance! and thank you for providing this code!
Comment by Bonzo Floyd - February 26, 2014 @ 1:56 pm
http://jacksonjackson18074.webs.com/apps/profile/
Comment by Maple - February 26, 2014 @ 12:00 am
http://streamtorrent.com/index.php?action=forum
Comment by Daniela - February 20, 2014 @ 2:16 am
http://www.streamtorrent.com
Comment by Porter - February 20, 2014 @ 2:07 am
https://message.diigo.com/message/anti-aging-and-fitness-performance-with-sytropin-human-growth-hormone-2824086?page_num=0
Comment by Charline - February 19, 2014 @ 7:50 pm
http://www.ep2p4u.com/sopcast-p2p-application-for-live-sports/
Comment by Cortney - February 09, 2014 @ 3:55 pm
http://www.atdhe.co.uk
Comment by Nelle - February 08, 2014 @ 9:56 pm
http://Www.Ventemaisonmontpellier.net/
Comment by Vonnie - February 08, 2014 @ 3:59 am
http://www.ep2p4u.com/myp2p-eu-my-p2p-forum/
Comment by Elaine - February 07, 2014 @ 11:33 pm
http://www.flabeloshomehire.co.uk/
Comment by Marcella - February 07, 2014 @ 2:25 pm
http://wiki.devyourself.ru/Modeling_Organizations_In_Virginia
Comment by Letha - February 07, 2014 @ 7:34 am
http://www.uts.cw/node/2445
Comment by Adolph - February 06, 2014 @ 12:53 am
http://www.south-africa.co
Comment by Malorie - February 04, 2014 @ 8:17 am
http://Level-5-motorsports.com/scott-tucker/
Comment by Nelle - February 03, 2014 @ 8:44 pm
http://www.ep2p4u.com/p2p4u-net/
Comment by Shanon - February 03, 2014 @ 7:00 am
http://www.gtanext.de/community/index.php?page=User&userID=4459
Comment by Filomena - February 02, 2014 @ 9:08 pm
http://commentvoyance.eu/
Comment by Shelia - February 02, 2014 @ 7:42 pm
http://www.beckham-fragrances.com/page/articletitlerevealing-quick-programs-way-make-money
Comment by Kristy - February 02, 2014 @ 2:24 pm
http://xtrememind.com/free-ebooks.htm
Comment by Alfonso - February 02, 2014 @ 10:17 am
http://www.findervenue.co.uk/
Comment by Victorina - February 02, 2014 @ 9:49 am
http://jacksonjackson18074.webs.com/apps/util/signinPopover.jsp
Comment by Reggie - February 02, 2014 @ 1:59 am
http://streamtorrent.com/index.php?action=forum
Comment by Saundra - February 01, 2014 @ 11:43 pm
http://www.kidswallartstickers.com.au
Comment by DrKevin - February 01, 2014 @ 11:33 pm
http://www.digg.badsmaru.com/user.php?login=ruthfowle
Comment by Marquita - February 01, 2014 @ 5:51 pm
http://www.newsx.com/newsx9
Comment by Esperanza - February 01, 2014 @ 2:40 pm
http://www.ventemaisonmontpellier.net
Comment by Freddie - January 31, 2014 @ 5:22 pm
http://streamtorrent.com/index.php?action=forum
Comment by Jerrod - January 29, 2014 @ 9:21 pm
http://www.youtube.com/watch?v=5o6xeedhmIw
Comment by Mikel - January 29, 2014 @ 5:48 pm
http://www.bootymaxtv.com/article_detail/black-swan-total-motion-picture-section-one-nine-hd-160543.html
Comment by Philip - January 29, 2014 @ 8:49 am
http://commerciallightingideas.com/industrial-outdoor-lighting/industrial-outdoor-lighting-sacramento-ca/
Comment by Phillip - January 28, 2014 @ 7:26 pm
Hi
can you please send me a "test mailreciver" ia a project file?
because I cant get this fine code to compile whithe the lib, thas ive added, (maybe i get the main faulty :-( )
can you please help me?
(i only whant to look at the first 10 mail and check if it is sender "string@string.com" && "subject containing: "xxxx"String"xyz " if true => show message..
best segards Niklas ( Cykeltur@gmail.com )
Comment by Niklas - January 28, 2014 @ 12:42 pm
http://www.dddsn.com/ph-d-recommended-natural-male-enhancement-techniques/
Comment by Myles - January 25, 2014 @ 9:23 pm
http://www.icrsa4.org/
Comment by Meagan - January 25, 2014 @ 7:08 pm
http://www.cuoliu.com/nlp-hypnotic-seduction/
Comment by Irwin - January 25, 2014 @ 6:10 pm
i am getting this error 01-24 17:59:15.024: E/MailApp(430): Could not send email
01-24 17:59:15.024: E/MailApp(430): java.lang.NullPointerException
01-24 17:59:15.024: E/MailApp(430): at com.example.mailsmtp.Mail.send(Mail.java:80)
01-24 17:59:15.024: E/MailApp(430): at com.example.mailsmtp.AskfatwaActivity.onCreate(AskfatwaActivity.java:47)
01-24 17:59:15.024: E/MailApp(430): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
01-24 17:59:15.024: E/MailApp(430): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
01-24 17:59:15.024: E/MailApp(430): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
01-24 17:59:15.024: E/MailApp(430): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-24 17:59:15.024: E/MailApp(430): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
01-24 17:59:15.024: E/MailApp(430): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 17:59:15.024: E/MailApp(430): at android.os.Looper.loop(Looper.java:126)
01-24 17:59:15.024: E/MailApp(430): at android.app.ActivityThread.main(ActivityThread.java:3997)
01-24 17:59:15.024: E/MailApp(430): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 17:59:15.024: E/MailApp(430): at java.lang.reflect.Method.invoke(Method.java:491)
01-24 17:59:15.024: E/MailApp(430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-24 17:59:15.024: E/MailApp(430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-24 17:59:15.024: E/MailApp(430): at dalvik.system.NativeStart.main(Native Method)
Comment by javalearner - January 24, 2014 @ 8:28 am
can some one please help me i have followed this tutorial but failed to send email i am not using any attachement just sending text no error but no result
Comment by javalearner - January 24, 2014 @ 8:13 am
http://www.instructables.com/member/adrieneodoherty/
Comment by Isobel - January 24, 2014 @ 12:41 am
http://luxuryvillaskiathos.blogspot.be/
Comment by Andreas - January 22, 2014 @ 10:53 pm
Hello everybody, need your help please! I want to send pdf by mail in background using this code and for pdf using the library "droidText.0.4.jar", individually mail and pdf works fine but when I use pdf jar with mail jars in the same project, at the moment of deployment appears an error that says "Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/apache/harmony/awt/internal/nls/Messages" I was looking a way to solve it but only found once that talks about... Import droidText.0.4 from repository like project and change some packages names but the problem is that repository has fallen and I need a new way to resolve. Mi mail is hrodpar@gmail.com if you can help me I will be very grateful. Hernan
Comment by Hernan - January 20, 2014 @ 1:13 pm
http://scotttuckercbs521.tumblr.com/archive/2012/2
Comment by Russ - January 20, 2014 @ 10:50 am
http://efficientseo.biz/
Comment by Eddie - January 20, 2014 @ 3:13 am
If someone get a this exception "javax.mail.AuthenticationFailedException" and then check your google security policy.
You probably set a 2 step verification. :)
Comment by Byeongchan Gwak - January 19, 2014 @ 4:17 am
http://efficientseo.biz/
Comment by Ahmed - January 19, 2014 @ 4:02 am
http://efficientseo.biz/
Comment by Tanya - January 19, 2014 @ 3:58 am
http://efficientseo.biz/
Comment by Sasha - January 19, 2014 @ 3:56 am
http://efficientseo.biz/
Comment by Kelvin - January 19, 2014 @ 3:55 am
http://efficientseo.biz/
Comment by Tracee - January 19, 2014 @ 3:51 am
http://efficientseo.biz/
Comment by Casimira - January 19, 2014 @ 3:26 am
http://efficientseo.biz/
Comment by Kendrick - January 19, 2014 @ 2:01 am
http://efficientseo.biz/
Comment by Stacy - January 19, 2014 @ 1:18 am
http://efficientseo.biz/
Comment by Lachlan - January 19, 2014 @ 1:03 am
http://efficientseo.biz/
Comment by Krystle - January 19, 2014 @ 12:48 am
http://efficientseo.biz/
Comment by Mikel - January 19, 2014 @ 12:47 am
http://efficientseo.biz/
Comment by Mallory - January 19, 2014 @ 12:40 am
http://Efficientseo.biz/
Comment by Anton - January 18, 2014 @ 10:18 am
http://efficientseo.biz/
Comment by Martha - January 18, 2014 @ 10:09 am
http://efficientseo.biz/
Comment by Glory - January 18, 2014 @ 8:52 am
http://efficientseo.biz/
Comment by Etta - January 18, 2014 @ 8:47 am
http://efficientseo.biz/
Comment by Williemae - January 18, 2014 @ 8:21 am
http://efficientseo.biz/
Comment by Myles - January 18, 2014 @ 7:47 am
http://level-5-motorsports.com/scott-tucker/scott-tucker-race-car-driver/
Comment by Florrie - January 16, 2014 @ 6:24 pm
http://lose-a-stone.blogspot.com/2014/01/fat-loss-factor-review.html
Comment by Trena - January 15, 2014 @ 9:50 pm
http://www.forexvps24.com
Comment by Vivian - January 15, 2014 @ 7:53 pm
http://efficientseo.biz/
Comment by Trina - January 15, 2014 @ 1:58 am
http://www.autoparts-stlouis.com/auto-glass.html
Comment by Reinaldo - January 15, 2014 @ 12:39 am
http://www.dekalux.com/catalog/bungalows/bungalow-in-legno-5x440mm/
Comment by Edna - January 15, 2014 @ 12:25 am
http://blogs.rediff.com/tedupshawnqcoggxk/2014/01/10/andre-marques-toronto-accountant/
Comment by Dwayne - January 12, 2014 @ 1:23 am
http://www.youtube.com/watch?v=vU27YZL4KsU
Comment by Donnell - January 11, 2014 @ 1:26 am
http://asztar.com.pl/uslugi/sprzatanie.html
Comment by Lilia - January 11, 2014 @ 1:08 am
hey sir, could you please send me your whole project file, i just badly need it asap! thanks in advance!
Comment by danuel - January 10, 2014 @ 11:49 pm
http://Skiathosinfo.Villacassiopee.com/
Comment by Adrianne - January 10, 2014 @ 3:11 pm
Can anyone tell me how to read gmail messages or inbox using javamail
Thank you!
Comment by yuvaraj - January 10, 2014 @ 12:55 am
007sanketh@gmail.com. This is my..can somebody send the zip file
Comment by sanketh - January 02, 2014 @ 12:54 pm
http://pityprzeznet.pl
Comment by Winnie - January 02, 2014 @ 6:03 am
https://www.101bilge.com/user.php?login=marceldel
Comment by Ines - December 21, 2013 @ 1:59 pm
http://fb-viral.info/user.php?login=glindawoo
Comment by Stanley - December 21, 2013 @ 9:35 am
http://www.lisc.de/wiki/index.php/Benutzer:YNGArlieqmtqmbw
Comment by Lucienne - December 21, 2013 @ 12:46 am
http://n2fc.co.uk/6vzs
Comment by Bonita - December 20, 2013 @ 5:16 am
please send complete zip file of this application
Comment by Manish Bagde - December 20, 2013 @ 1:28 am
http://www.sharedocumentz.com/members/profile/55456/JohnBruns
Comment by Soila - December 19, 2013 @ 11:54 pm
http://www.cucinandoassaggiando.com/2013/11/coniglio-profumato-allo-zafferano.html
Comment by Lavonne - December 19, 2013 @ 5:09 pm
http://4nu.net/index.php?a=stats&u=violettetinline
Comment by Steffen - December 17, 2013 @ 2:01 am
http://www.sunflowerstrategies.com/reply-n1251-12.html
Comment by Cooper - December 17, 2013 @ 1:13 am
Thank you very much! It works perfect. Greetings my friend.
Comment by Hernan - December 09, 2013 @ 12:49 pm
https://twitter.com/seohighend
Comment by Lucile - November 24, 2013 @ 12:37 am
piece of shit. not working code which wastes people's hours.

lazy bastard not even answer any question since 2010
Comment by jason - November 23, 2013 @ 1:06 pm
http://www.gideonlogistics.com/salvage-car-shipping.html
Comment by Ollie - November 14, 2013 @ 10:24 am
Hey Dude this is nice

i also found one good example here

Get Contact Emails By Content Provider
Comment by Android Example - November 14, 2013 @ 9:22 am
Hey Dude this is nice

i also found one good example here

Get Contact Emails By Content Provider
Comment by Android Example - November 14, 2013 @ 9:18 am
Sir will you please send me complete code (kushaljava2@gmail.com)


thanks
Comment by kushal - November 11, 2013 @ 8:16 am
http://autotransportpros.blogspot.com/
Comment by Mona - November 10, 2013 @ 3:27 am
http://Www.Shipmycar123.com/car-hauler-dispatch/
Comment by Domingo - November 10, 2013 @ 2:15 am
http://fr-skiathosinfo.villacassiopee.com/project/notre-priorite/
Comment by Charles - November 06, 2013 @ 2:04 pm
http://www.savebright.com/
Comment by Jacquie - November 04, 2013 @ 1:57 am
Well Done - Thanks for such an awesome class! For exchange users, your ssl port is 993.

More exchange port info here: http://support.microsoft.com/kb/278339.

If you are having any connection issues ("Could not connect to SMTP host: xyz.mail.com Port: 25") - start your debugging process by commenting out the lines below in the _setProperties() function to disable the ssl negotiation.

//DISABLE SSL CONNECTION
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "true");


Also, make sure the value below is set to, "true"
props.put("mail.smtp.socketFactory.fallback", "true");

Cheers!

Comment by Rob Catalano - November 03, 2013 @ 2:19 pm
OK now it works folks, yes even on the latest android versions :)

So here is the deal: for SDK 9 and later, you have to put the code in a different thread than the UI thread. You can, for example, handle the code in an AsyncTask.

Another rude way to overcome the problem is to add the following code before using the Mail. But I truly don't recommend it.

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Your code has worked fine for me including attachments to the email. The only small problem I see is that the name of the attachments are based on the source file full pathname which is certainly too long. The file name would have been sufficient I think. Going to see if I can fix that in the source code provided...

Still, it is a great shoot. You have my thanks for this.
Comment by Alain - November 03, 2013 @ 2:15 pm
Thanks..............
Comment by IWA - October 26, 2013 @ 12:19 am
This works perfectly. Thank you Jon.

I made one slide modification to addAttachment(). Being annoyed that the name of attached files included the entire path I changed the attribute passed to messageBodyPart.setFileName() from:
filename
to
filename.substring(filename.lastIndexOf("/") + 1)

The method now reads:
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename.substring(filename.lastIndexOf("/") + 1));

_multipart.addBodyPart(messageBodyPart);
}
Comment by Andreas Ross - October 17, 2013 @ 9:04 am
This worked for me.. Thank you..
Comment by Priyanka - October 15, 2013 @ 6:12 am
please, what is the " fore-said files" ???
Comment by zahra - October 08, 2013 @ 7:24 pm
Could you send me the full project code for "Sending Emails without User Intervention (no Intents) in Android". I would be a great help in order for me to understand the code. Also when I try to execute the code it gives me a "FORCE CLOSE".
Comment by Nigel Jarvis - August 30, 2013 @ 10:00 am
I'm having problem on my click event this part:
m.setTo(toArr);
m.setFrom("wooo@wooo.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
please help
Comment by japZ - September 03, 2012 @ 9:14 pm
Hey I got the same error like "Comment by fahad alawam - January 11, 2012 @ 5:53 am".

What does this exception "NetworkOnMainThreadException" mean?

Thank you for publishing this code.

i got this error pleas help :

01-11 13:48:59.818: E/MailApp(29958): Could not send email
01-11 13:48:59.818: E/MailApp(29958): android.os.NetworkOnMainThreadException
01-11 13:48:59.818: E/MailApp(29958): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
01-11 13:48:59.818: E/MailApp(29958): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-11 13:48:59.818: E/MailApp(29958): at java.net.InetAddress.getLocalHost(InetAddress.java:371)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.Transport.send(Transport.java:117)
01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Mail.send(Mail.java:125)
01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Add$1.onClick(Add.java:58)
01-11 13:48:59.818: E/MailApp(29958): at android.view.View.performClick(View.java:3480)
01-11 13:48:59.818: E/MailApp(29958): at android.view.View$PerformClick.run(View.java:13983)
01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.handleCallback(Handler.java:605)
01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 13:48:59.818: E/MailApp(29958): at android.os.Looper.loop(Looper.java:137)
01-11 13:48:59.818: E/MailApp(29958): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 13:48:59.818: E/MailApp(29958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-11 13:48:59.818: E/MailApp(29958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-11 13:48:59.818: E/MailApp(29958): at dalvik.system.NativeStart.main(Native Method)
Comment by Thomas - September 02, 2012 @ 7:34 pm
can u plz send the zip.file of the project
Comment by rebai - August 16, 2012 @ 3:12 pm
This code is excellent! Works perfectly for sending mail in the background. I have one question: when the email is being sent does it pause/freeze your app while sending mail? I'm wondering because I want to put a progress dialog for when mail is being sent but the progress spinner won't move. It doesn't even appear unless I sleep the thread. Any help would be greatly appreciated.
Comment by Jordan - August 15, 2012 @ 10:13 am
I have experienced exactly the same problem as
fahad alawam - January 11, 2012 @ 5:53 am. The android version concerned is 4.0.3 (SDK level 15).

This is a good piece of code. So is there a way to fix the problem? Some device settings to make in order to have the feature operate? A solution requiring android permissions of system level is fine with me.
Comment by Alain - August 10, 2012 @ 5:39 am
Great.

But the SDK level must be smaller than 9.

If not, the app will crashed because we are not allowed to do this...
Comment by Felix Group - July 20, 2012 @ 11:04 am
Great.

But the SDK level must be
Comment by Felix Group - July 20, 2012 @ 11:03 am
Is this code will work in Android Emulator???

or Can you send me the zip file of your code??
Comment by AndroidMail - June 24, 2012 @ 3:44 pm
Aw, this was a really nice post. Spending some time and
actual effort to produce a really good article… but what can I say… I put things off a lot and don't seem to get anything done.
Comment by Genesis - June 22, 2012 @ 7:48 pm
Thanks
Comment by Robson - June 13, 2012 @ 10:01 am
Thanks a lot.

But if i want to use my own domain instead of gmail, what should i do?

Thanks again for the guideline.
Comment by Hasan - June 11, 2012 @ 7:42 am
thank u so much... this help me lot...
really helpful..
:)
Comment by Amol Sawant - June 06, 2012 @ 1:17 am
Hi author, can u send me the complete source code in zip. Thank you very much.
Comment by Jasvin - June 03, 2012 @ 12:39 pm
work like a charm!! muaaahhh 10x a lot!!! keep it up!!
Comment by arya - May 11, 2012 @ 6:24 am
Perfect!
Comment by Igor - April 18, 2012 @ 12:44 pm
For those having the java.lang.NoClassDefFoundError issue:

1. Make sure you add the items as JARs rather than External JARs. You do this by clicking the project, then selecting "Build Path", then selecting "Configure Build Path".

2. Make sure you check the box next to the JARs you just added. This is done under the "Order and Export" tab.

Also, make sure you add the following permission to your manifest file:


Comment by Auri Rahimzadeh - April 17, 2012 @ 3:04 pm
I am getting the following error:

Unsupported action - That action is not currently supported

Please let me know how to solve it.
Comment by James - April 13, 2012 @ 5:34 am
First of all thank you for this article.
Here is my question : How can I receive mail with JavaMail Api or another Api on Android Project. I have been trying for this, but I couldnt find any working code on Android Application. I hope you can give me an idea about this situation.
Comment by Merve - March 22, 2012 @ 1:49 pm
Hi Jon,

This is a great howto, the code is very easy to follow & understand! Thanks very much for posting it!
Comment by Andrew - February 13, 2012 @ 10:48 pm
So I will have to embed my email name/password/server right into my code? Where anyone can read it, or sniff it out through the port?
Comment by Donna - January 29, 2012 @ 5:25 pm
getting the following error, please help and if possible mail me at mayurdhwajsinh@gmail.com

No enclosing instance of type MailApp is accessible. Must qualify the allocation with an enclosing instance of type MailApp (e.g. x.new A() where x is an instance of MailApp).
Comment by mayur - January 28, 2012 @ 1:09 am
Is it possible to RECEIVE the Gmail emails? I'm trying to create an app that will display gmail emails but can't find anywhere a way to grab them from the servers. Thanks
Comment by Phil Tucker - January 20, 2012 @ 2:32 pm
Hi,

I am getting javax.mail.AuthenticationFailedException error. I have rechecked username, password and all seem to be correct. Can someone please give hint to resolve the javax.mail.AuthenticationFailedException exception?

Thanks.

ps: I have got the source code of the jar files now. Thanks.
Comment by Ambi - January 15, 2012 @ 1:57 pm
Hi,

Thanks for the useful work.

Is the source code for the three jar files namely "additionnal.jar, mail.jar, activation.jar" available? If yes can you please provide the link?

Thanks,
Ambi.
Comment by Ambi - January 15, 2012 @ 9:04 am
i got this error pleas help :

01-11 13:48:59.818: E/MailApp(29958): Could not send email
01-11 13:48:59.818: E/MailApp(29958): android.os.NetworkOnMainThreadException
01-11 13:48:59.818: E/MailApp(29958): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
01-11 13:48:59.818: E/MailApp(29958): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-11 13:48:59.818: E/MailApp(29958): at java.net.InetAddress.getLocalHost(InetAddress.java:371)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.Transport.send(Transport.java:117)
01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Mail.send(Mail.java:125)
01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Add$1.onClick(Add.java:58)
01-11 13:48:59.818: E/MailApp(29958): at android.view.View.performClick(View.java:3480)
01-11 13:48:59.818: E/MailApp(29958): at android.view.View$PerformClick.run(View.java:13983)
01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.handleCallback(Handler.java:605)
01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 13:48:59.818: E/MailApp(29958): at android.os.Looper.loop(Looper.java:137)
01-11 13:48:59.818: E/MailApp(29958): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 13:48:59.818: E/MailApp(29958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-11 13:48:59.818: E/MailApp(29958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-11 13:48:59.818: E/MailApp(29958): at dalvik.system.NativeStart.main(Native Method)
Comment by fahad alawam - January 11, 2012 @ 5:53 am
To the author
I could not make this work for Microsoft Mail Exchange Server, any idea how to set up for Microsoft Exchange Server. thanks.
Comment by Mukunda - December 31, 2011 @ 5:17 am
Awesome!!! Worked like a charm!!!
Comment by Pravin - December 30, 2011 @ 1:47 am
hi can u please send me the zip file.. Thanks
Comment by senthil - December 27, 2011 @ 4:52 am
I m using emulator to test this, and getting the below error message "MailApp cannot be resolved to a type".

Also how does it recognizes the user's password. We are no where giving the password right? We are only giving From Email address.

Any help is much appreciated.
Comment by Ratnakar - December 25, 2011 @ 3:32 pm
Works perfectly. Thank you very much !
Comment by Bragaadeesh - December 24, 2011 @ 5:05 pm
I couldn't get this to working on Microsoft Email 365, any information would be helpful.
Tried with
Server name: podabc.outlook.com
Port: 587
Encryption method: TLS

but nothing worked.
thanks.
Comment by Mukunda - December 19, 2011 @ 8:21 am
Hello,

Thank you for providing this information, so far your site is the only site that has answered my question!

I'm new at programming in java, and I cant seem to figure out how to 'load' this files into eclipse so I can get this to work. I know this is the answer, implementing it is whats giving me difficulty.

Can you create a quick video and post it on youtube so we can see how you did this?

Thanks alot
Comment by Justin - December 13, 2011 @ 2:21 pm
can u plz send the zip file of this code
Comment by poornima jr - December 10, 2011 @ 11:49 am
Can you please send me this project's .zip file. I got lot of errors from this code.
Comment by Praveen Kumar - December 02, 2011 @ 5:16 am
hi,
Can you plz send the code in zip file on my email mail4satya@gmail.com. I am getting lots of errors in this.
Comment by Satya - November 30, 2011 @ 5:26 am
To author mr. Jon Simon.
Never helped anybody with knowhow. But im in shame today.
Cause I were forced to get Your help.
My project is saved!
Thank You,gentleman.
Comment by Huseynov Iskandar - November 26, 2011 @ 7:19 am
To author mr. Jon Simon.
Never helped anybody with knowhow. But im in shame today.
Cause I were forced to get Your help.
My project is saved!
Thank You,gentleman.
SamGU.
Comment by Huseinov Iskandar - November 26, 2011 @ 7:10 am
My application is used in locations with no network access, and the app should cache any outgoing messages until the network is available. Looks like this example does have that functionality - anyone know of an example that does?
Comment by Tom - November 17, 2011 @ 8:48 pm
I also got this error, can anyone help me on this:
javax.mail.MesagingException: Unknown SMTP host: smtp.gmail.com; nested exception is:
java.net.UnknownHostException: Host is unresolved: smtp.gmail.com:465
Comment by karthik - November 08, 2011 @ 6:19 am
I try this code and it didn't work and they I realize it work only with gmail account that use one step verification.
and not the new 2-step verification.
if someone have an idea how to make it work with 2-step verification it will be great
Thanks for the code is really great
Comment by assaf passal - November 07, 2011 @ 10:25 am
I get this error, can anyone help me on this:
javax.mail.MesagingException: Unknown SMTP host: smtp.gmail.com; nested exception is:
java.net.UnknownHostException: Host is unresolved: smtp.gmail.com:465
Comment by HT - October 20, 2011 @ 1:59 pm
" ... New to droid. Thank you for posting this tutorial. I am not getting any errors but I am getting "There was a problem sending the email.
..."
Hi Carl, you should check permission in the manifest file. Perhaps you forgot add permission for accessing internet
Comment by littleboy - October 09, 2011 @ 10:01 pm
Works perfect for my project, thanks a lot!
Comment by IceCream - October 07, 2011 @ 3:33 pm
Hi..Thanks for this..It works very well if there are no attachments..But the moment I try to attach any file with the email, the app just stops responding..There is no error in logcat etc..but the email is never sent even for very small sized files and even after waiting a lot... Please help..
Comment by kusi - October 03, 2011 @ 1:51 am
This only works for SSL right. What about TLS?
Comment by gT - September 25, 2011 @ 1:30 am
Mail sending is working superb.
But do you know , how can we check whether the mail sent is successful or failed?
Comment by TechnoTalkative - September 23, 2011 @ 6:24 am
Hi, does it send the username and password to gmail encrypted? Where is this done?
Comment by andrew - September 19, 2011 @ 12:26 pm
Works great on Nexus S. Thanks.
Comment by KC - August 08, 2011 @ 10:56 am
New to droid. Thank you for posting this tutorial. I am not getting any errors but I am getting "There was a problem sending the email." Any idea how to trace and fix?
Comment by carl - August 06, 2011 @ 10:38 pm
hii this code is working fine with out attachment. when i am sending with attachment its send the file name is "noname" and inside file content is what ever i gave boby that one only inside file.
please help me send with attachment.
please send me the .zip file
my email is indela447@gmail.com
Comment by indela - July 29, 2011 @ 3:47 am
Thanks for the code.Working properly in all versions

But in Android1.6 It is forcefully closing the application.Any idea about this
Comment by Venkat - July 28, 2011 @ 7:15 pm
Hi! Thanks, got it to work with little effort, after a couple of the tweaks suggested, such as adding the permission to the Manifest file, and I also had to add a button to my own test main.xml named send_email.

Sending a regular e-mail without attachments works fine, but when I try to attach a jpg that is in my res/drawable folder, it errors out on the line:

// send email
Transport.send(msg);

The addAttachment code seemed to handle my filepath OK, but I don't know if my filepath is valid. Do you know what the absolute path is for a jpg in the res/drawable folder? I was trying this code:


String filepath = Uri.parse("android.resource://com.michaeljdougan.mikemail/" + R.drawable.e002_c).toString();
m.addAttachment(filepath);

And filepath looks like:
"android.resource://com.michaeljdougan.mikemail/20137496"

I've used similar code to attach a jpg using code for the built-in e-mail program, in a putExtra(EXTRA_STREAM statement, and it worked.

Do you know if you have to copy the attachments to the SD card before attaching to an email? I'm testing using the simulator, and there is no SD card in the simulator.

For those that say you can't do this with the simulator, it does send the e-mail fine, without an attachment, and I can even send an attachment using the built-in email program, as long as I've setup my e-mail client properties (smtp server etc).

Thanks in advance!
Comment by Mike - July 27, 2011 @ 7:12 pm
Just remember there is an Open Source project for SMS Backup that uses a Gmail account to store and retrieve SMS messages. There should be enough information on both sending and receiving email messages. http://code.google.com/p/android-sms/

You need to use subversion to pull a copy of the source code. svn checkout http://android-sms.googlecode.com/svn/trunk/ android-sms-read-only
Comment by Ernie - July 13, 2011 @ 3:50 pm
When I called the send() method, I am getting a java.io.IOException. Couldn't connect using "javax.net.sll.SLLSocketFactory" ClassNotFoundException: javax.net.sll.SSLSocketFactory.

Shouldn't it be javeax.net.ssl.SSLSocketFactory??

Do I need to go to the JAR files to correct this? TIA.
Comment by Ernie - July 13, 2011 @ 3:34 pm
It took me a while to figure out all the errors I was getting, but I must say, reading through all the comments really helps as well.

Thanks for this post, really awesome.

For future reference, is there no way of adding the missing code back into the main post to save other people the trouble?
Comment by Evan - July 12, 2011 @ 4:58 am
Excellent work!

lol @ the users asking you to contact them personally via email with a solution to their problem
Comment by PodeCoet - July 07, 2011 @ 10:02 pm
zohaibbrohi@gmail.com .. thats my id Thanks
Comment by Zohaib - July 06, 2011 @ 9:34 pm
Please mail me zip file of this program. thanks
Comment by Zohaib - July 06, 2011 @ 9:33 pm
Hi,

perfect tutorial, works very well! Thanks alot for that!

But: I tried to add a bcc recipient, which doesnt work. Instead, a copy to the _to recipient is send twice. I used a similiar code as for adding recipients in mail.java.

Do you know a solution?
Comment by javadev - June 09, 2011 @ 9:17 am
thanks a lot man its really working...you've solve my problem thank you very much...
Comment by Nikunj Dhamat - May 30, 2011 @ 7:26 am
How do I set the mime type for an attachment?? I need to be able to set it to a custom mime type so that when I get the email on another phone I can launch my app from the attachment.

Thanks very much for this code!!
Comment by David Pfeiffer - May 26, 2011 @ 10:51 pm
Hi Rick,
I think that it doesn't suppose to work on an emulator, only on a real device.
Try checking it on your phone.

Good luck, Elad
Comment by Elad - May 18, 2011 @ 9:29 am
Hi Elad

I was wondering are you trying this from an actual phone or are you using the emulator?
Comment by Rick - May 18, 2011 @ 9:24 am
Great, works for me!
But isn't it bit risky to put your password? someone can easily decompile the apk.. Any suggestions?
Thanks
Comment by Elad - May 18, 2011 @ 5:07 am
I get the following exception when running this on the emulator - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; java.net.SocketException: Permission denied. Any one run into this one?

Comment by Rick - May 16, 2011 @ 1:44 pm
Hello, I tried to compile the code and I get the following errors .. please help me to solve them. Thanks in advance.

23:18:00.703 904 ERROR dalvikvm Could not find class 'org.me.emailtestandroid.Mail', referenced from method org.me.emailtestandroid.MainActivity$1.onClick
23:18:11.974 904 ERROR AndroidRuntime FATAL EXCEPTION: main
23:18:11.974 904 ERROR AndroidRuntime at org.me.emailtestandroid.MainActivity$1.onClick(MainActivity.java:33)
23:18:11.974 904 ERROR AndroidRuntime at android.view.View.performClick(View.java:2485)
23:18:11.974 904 ERROR AndroidRuntime at android.view.View$PerformClick.run(View.java:9080)
23:18:11.974 904 ERROR AndroidRuntime at android.os.Handler.handleCallback(Handler.java:587)
23:18:11.974 904 ERROR AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:92)
23:18:11.974 904 ERROR AndroidRuntime at android.os.Looper.loop(Looper.java:123)
23:18:11.974 904 ERROR AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:3683)
23:18:11.974 904 ERROR AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
23:18:11.974 904 ERROR AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:507)
23:18:11.974 904 ERROR AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
23:18:11.974 904 ERROR AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
23:18:11.974 904 ERROR AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
Comment by Luca - May 07, 2011 @ 7:38 pm
Works like a charm !!

Thanks alot!!
Comment by Shrey Malhotra - May 07, 2011 @ 8:27 am
well i am new to android . i had tried this code but it does not work . So can i have the source code of this.
Comment by Ratnesh - May 05, 2011 @ 6:20 am
I'll try and find my project file and post the link in the next couple of days - sorry for the delayed response...
Comment by Jon - April 25, 2011 @ 3:57 pm
I m facing red cross where is written MailApp. what is this and wats the solution. plz help me. If possible, anyone send me zipped file on goforseeking@yahoo.com

Thanx
Comment by Muhammad Asif - April 22, 2011 @ 10:41 am
Thanks it works, it didn't at first because the emulator was stack or something. But it worked after I restarted the emulator.
thx
Comment by NewAndMan - April 17, 2011 @ 10:17 am
"java.lang.NoClassDefFoundError" Solved.

The problem is Eclipse screw up for reference jar. After change .classpath .. able to send mail already..
Comment by kyaw - April 12, 2011 @ 9:37 pm
Its not complain when I compile at eclipse, But In runtime i got this error..
java.lang.NoClassDefFoundError

how do I solve ?
Comment by kyaw - April 12, 2011 @ 3:33 am
hey guys the information here is very useful..but somehow i was not able to make it run..but finally i m able to successfully send mail on click of button..thanks dude..but this guy seems bit busy..let me do it for u ..mail me whoever want the complete running code
..praveenkanade@yahoo.com
Comment by praveen - April 11, 2011 @ 9:11 pm
Thank you for all the usefull information. Can you send me the zip file for your eclipse project, it will help me understand this alot. Thanks
Comment by Brandon - April 11, 2011 @ 8:49 pm
hiii ,
i am trying to do this ,but there is nothing showing (the project is running well,there is no error)..i have done every thing ,like completed uses-permission,and have write extra methods(setFrom,setSubject etc)...but my project is not showing anything ...its only showing a button and also its not showing the Toast materials...please someone help me...its very important for me.

advance thanks,
Manoj
Comment by Manoj Behera - April 06, 2011 @ 5:42 am
thanks
Comment by raju - April 01, 2011 @ 5:54 am
Because of this line:
Mail m = new Mail("myusername@gmail.com", "mypassword");

I get this error any ideas :
jar:
Result: -1
pkg: /data/local/tmp/AutomatedEmail.apk
Success
33 KB/s (6951 bytes in 0.204s)
About to start org.me.automatedemail/org.me.automatedemail.AutoEmail
DDM dispatch reg wait timeout
Can't dispatch DDM chunk 52454151: no handler defined
Can't dispatch DDM chunk 48454c4f: no handler defined
Starting: Intent { cmp=org.me.automatedemail/.AutoEmail }
run:
BUILD SUCCESSFUL (total time: 48 seconds)
Comment by Newbie - March 29, 2011 @ 10:18 pm
Please send me the .zip file..
thank u.......
Comment by praveen - March 28, 2011 @ 7:56 am
excellent tutorial.
worked in the first go.
Comment by saurabh - March 23, 2011 @ 6:07 pm
wowww..
can send me the zip project file??
thankss.
Comment by basit - March 21, 2011 @ 11:13 pm
can u send me .zip file.
thanks in advance!!!!!
Comment by nis - March 12, 2011 @ 3:06 pm
can same code work for yahoo or hotmail also??
Comment by sneha - March 10, 2011 @ 5:24 am
Worked for me - thanks!
Comment by Raj - March 06, 2011 @ 1:46 pm
Getting exception javax.mail.AuthenticationFailedException..

please help

thanks in advance
Comment by chaitanya - March 04, 2011 @ 6:21 am
worked for me, after setting the internet permission on. Thank you.
Comment by prashant sharma - February 28, 2011 @ 6:50 am
Can you please send me your project in .zip for eclipse because i am getting quite a few errors and it would help me out a lot if the .zip file were in front of mr
Comment by Ronil - February 25, 2011 @ 5:52 am
this code is not working for hotmail and yahoo. how this possible?
Comment by kamran - February 23, 2011 @ 5:22 am
Thanks a lot, that was awesome :D. Exactly what i needed
Comment by MrMaffen - February 17, 2011 @ 1:15 pm
Could not connect to SMTP host: mail.geosafetynet.com, port: 25


Any Ideas? I want to use my own mail server. Please contact back via email.
Comment by Walter - January 27, 2011 @ 7:08 pm
I could not run the code.
It giving me SMTP Exception.

Can you please send the code my Email.
Thank you
Comment by Jeevan - January 26, 2011 @ 5:04 pm
Can you please send me your project in .zip for eclipse? I'm getting errors while trying to send an email (cannot connect to SMTP host). Thanks!
Comment by ViewSonic - January 20, 2011 @ 5:59 pm
Thanks its working now!!
Good Work!!
Comment by Deepanshu - January 19, 2011 @ 3:49 am
hi,
Can you plz send the code in zip file on my email deepanshu.wad@gmail.com.
Bcz i am getting errors in this.
I am in urgent need plz help!!
Comment by Deepanshu - January 19, 2011 @ 12:55 am
Hi, can you maybe provide an export of an whole eclipse android project so i can have a look at it and get an impression of how to implement htis whole thing.

Thank you;)
Comment by Spazzt - January 17, 2011 @ 1:22 pm
Very useful code - thanks!

Is there any way to avoid having to specify the sending password, maybe by using AccountManager to authenticate with the phone's gmail account?
Comment by Craig - January 05, 2011 @ 8:24 pm
hey please tell me how to give the path of internal storage to send a file
Comment by rakshith - January 05, 2011 @ 12:22 am
As stated above, your SMTP example works perfected. This has been a great help. I also see that (according to http://code.google.com/p/javamail-android/) you also have imap working and tested. Do you have any sample code for your imap feature.
Again may thanks.
Comment by Max Jackson - January 04, 2011 @ 3:11 pm
Fantastic stuff. Thanks for taking the time to post this.
Comment by owen - January 01, 2011 @ 9:48 pm
hey please help, the mail not delivered
Comment by rakshith - December 28, 2010 @ 12:02 pm
Awesome, thank you! This works perfectly!
Comment by mo - December 21, 2010 @ 4:57 pm
please send totla source.zip file
Comment by kiran - December 17, 2010 @ 1:52 am
Thank you so much!!!
*hug*

I first ran into a "Untrusted Certificate" issue (due to my self signed cert) that I had to resolve by adding this to my code

static {
Security.addProvider(new JSSEProviderHarmony());
}

with:

public final class JSSEProviderHarmony extends Provider {

public JSSEProviderHarmony() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");

AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");

put("KeyManagerFactory.X509", "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509", "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");

return null;
}
});
}
}

and had to add a "TrustAllSSLFactory" by
replacing

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

with

props.setProperty("mail.smtp.socketFactory.class", "com.ur.app.TrustAllSSLSocketFactory");

at ur method setProperties().


public class TrustAllSSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory factory;

public TrustAllSSLSocketFactory() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null,
new TrustManager[] { new TrustAllTrustManager()},
null);

factory = (SSLSocketFactory)sslcontext.getSocketFactory();
} catch(Exception ex) {
// ignore
}
}


public static SocketFactory getDefault() {
return new TrustAllSSLSocketFactory();
}

@Override
public Socket createSocket(Socket socket, String s, int i, boolean flag)
throws IOException {
return factory.createSocket(socket, s, i, flag);
}

@Override
public Socket createSocket(InetAddress inaddr, int i,
InetAddress inaddr1, int j) throws IOException {
return factory.createSocket(inaddr, i, inaddr1, j);
}

@Override
public Socket createSocket(InetAddress inaddr, int i)
throws IOException {
return factory.createSocket(inaddr, i);
}

@Override
public Socket createSocket(String s, int i, InetAddress inaddr, int j)
throws IOException {
return factory.createSocket(s, i, inaddr, j);
}

@Override
public Socket createSocket(String s, int i) throws IOException {
return factory.createSocket(s, i);
}

@Override
public Socket createSocket( ) throws IOException {
//log.debug( "createSocket 0");
return factory.createSocket();
}

@Override
public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}


On the server side the default postfix master.cf file has to have enabled smtps with
smtpd_tls_wrappermode=yes
smtpd_sasl_auth_enable=yes

to prevent:
java.io.IOException: SSL handshake failure: Failure in SSL library, usually a protocol error

error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:604 0xaf076228:0x00000000)
Comment by happydude - December 06, 2010 @ 6:40 pm
Great stuff! Tested it a few minutes ago, and it does work, even with attachments!
Comment by Finkle - November 28, 2010 @ 2:05 pm
I have used attachment part from the above code, I am able to send email with attachment but my email body is also send as attachment.

Can anyone hele me regarding the same.
Comment by ravi - November 22, 2010 @ 12:07 am
Hi,I'm Chinese.new to java,new to android,completely new to javamail.I'm trying to code an apk to mail Message on android phone to email account,your artical helps very much,the only one that workde this days,thank you very much!
Comment by songshijia88888 - November 06, 2010 @ 6:35 pm
Oops, it didn't take. The XML tag is (left bracket) uses-permission android:name="android.permission.INTERNET" /(right bracket).
Comment by SteV-O - October 29, 2010 @ 1:45 am
Hello, thanks for posting this! It works like a dream.

I must suggest one thing to anyone having trouble getting this to work: you must add the XML tag to your AndroidManifest.xml file. It took me several hours to figure this out. Also took before I realized that to set this in Eclipse, don't just type INTERNET; type android.permission.INTERNET. (Also once I got it working, the message I sent landed in Spam.)
Comment by SteV-O - October 29, 2010 @ 1:43 am
This was incredibly useful and works perfectly for me. Thanks!

@Reza Nezami

You can make this send HTML content by replacing this bit of the 'send()' method:

// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);

with

// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(_body, "text/html");
_multipart.addBodyPart(messageBodyPart);


You could obviously make the mime type here a parameter that you pass into setBody().
Comment by zonski - September 28, 2010 @ 9:21 pm
I've been trying to make this class work, but i got this error:


09-28 15:42:26.382: ERROR/MailApp(4363): javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
09-28 15:42:26.382: ERROR/MailApp(4363): nested exception is:
09-28 15:42:26.382: ERROR/MailApp(4363): java.io.IOException: SSL handshake failure: Failure in SSL library, usually a protocol error
09-28 15:42:26.382: ERROR/MailApp(4363): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:585 0xaf589f78:0x00000000)

Any help please?
Comment by Oscar Davila - September 28, 2010 @ 4:42 pm
Wandering if it's possible to send html content (not attachment) like this? sending plain text works fine.
thx
Comment by Reza Nezami - September 23, 2010 @ 11:06 pm
PLEASE NOTE: The example code (wrapper) above is missing a couple of required setters. You will need to add them yourself:


public void setTo(String[] toArr) {
this._to = toArr;
}

public void setFrom(String string) {
this._from = string;
}

public void setSubject(String string) {
this._subject = string;
}
Comment by Matt Farley - September 17, 2010 @ 1:13 pm
Just wanted to say I spent hours searching the web and your solution was the only one I found that worked!

Thank you!
Comment by Matt Farley - September 17, 2010 @ 1:00 pm
Can this be done on the iphone???
Comment by mark - July 17, 2010 @ 5:25 pm

Name (required)
Email (will not be published) (required)
Website

captcha