0

I have to send email from my android application.

I have to ruh the app means am getting following error on my logcat window:

01-09 05:53:12.263: E/AndroidRuntime(824): FATAL EXCEPTION: main
01-09 05:53:12.263: E/AndroidRuntime(824): java.lang.RuntimeException: javax.mail.internet.ParseException
01-09 05:53:12.263: E/AndroidRuntime(824):  at com.ssmobileproductions.catalogue.InvoiceOrder$1.onClick(InvoiceOrder.java:91)
01-09 05:53:12.263: E/AndroidRuntime(824):  at android.view.View.performClick(View.java:2408)
01-09 05:53:12.263: E/AndroidRuntime(824):  at android.view.View$PerformClick.run(View.java:8816)
01-09 05:53:12.263: E/AndroidRuntime(824):  at android.os.Handler.handleCallback(Handler.java:587)
01-09 05:53:12.263: E/AndroidRuntime(824):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 05:53:12.263: E/AndroidRuntime(824):  at android.os.Looper.loop(Looper.java:123)
01-09 05:53:12.263: E/AndroidRuntime(824):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-09 05:53:12.263: E/AndroidRuntime(824):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 05:53:12.263: E/AndroidRuntime(824):  at java.lang.reflect.Method.invoke(Method.java:521)
01-09 05:53:12.263: E/AndroidRuntime(824):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-09 05:53:12.263: E/AndroidRuntime(824):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-09 05:53:12.263: E/AndroidRuntime(824):  at dalvik.system.NativeStart.main(Native Method)
01-09 05:53:12.263: E/AndroidRuntime(824): Caused by: javax.mail.internet.ParseException
01-09 05:53:12.263: E/AndroidRuntime(824):  at javax.mail.internet.ContentType.<init>(ContentType.java:102)
01-09 05:53:12.263: E/AndroidRuntime(824):  at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1291)
01-09 05:53:12.263: E/AndroidRuntime(824):  at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2074)
01-09 05:53:12.263: E/AndroidRuntime(824):  at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
01-09 05:53:12.263: E/AndroidRuntime(824):  at javax.mail.Transport.send(Transport.java:117)
01-09 05:53:12.263: E/AndroidRuntime(824):  at com.ssmobileproductions.catalogue.InvoiceOrder$1.onClick(InvoiceOrder.java:86)
01-09 05:53:12.263: E/AndroidRuntime(824):  ... 11 more
01-09 05:53:14.023: I/Process(824): Sending signal. PID: 824 SIG: 9

These is my code:

public class InvoiceOrder extends Activity {


              String mGrandTotal;

          @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub  
             super.onCreate(savedInstanceState);
                setContentView(R.layout.invoice);

               Button login = (Button) findViewById(R.id.mBtnSubmit);
                 login.setOnClickListener(new View.OnClickListener() {

                  public void onClick(View arg0) {
                   Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");

            Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("xxxxxx@gmail.com","xxxxx");
                    }
                });

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("krishnaveni.veesxxman@mercuxxsxxryminds.com"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("mercy.krishnaxxxdsdveni@gmail.com"));
                message.setSubject("Testing Subject");
              //  message.setContent("This is your product name : "+
                    //    "Hi Krishna" +"<br></br>This is your price : "+ "Hi veni", "text/html; charset=utf-8");
                for (int i = 0; i < Constants.mItem_Detail
                        .size(); i++) {

                        String title = Constants.mItem_Detail
                            .get(i).get(
                                    SingleMenuItem.KEY_PNAME);

                    String qty = Constants.mItem_Detail.get(i)
                            .get(SingleMenuItem.KEY_QTY);

                    String cost = Constants.mItem_Detail.get(i)
                            .get(SingleMenuItem.KEY_PRICE);

                    String total = Constants.mItem_Detail
                            .get(i).get(
                                    SingleMenuItem.KEY_TOTAL);

                    message.setContent("<tr>" + "<td>" + title
                            + "</td><td>" + qty + " * " + cost
                            + "</td>" + " = <td>" + total
                            + "  " + "</td></tr>", total);
              }

                Transport.send(message);

                System.out.println("Done");

            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
           }



  });

please help me.whats wrong in my code.why am getting above error.please give me some idea to resolve my error.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
android
  • 89
  • 3
  • 12
  • what is line no.91. Please share that line – Neha.R Jan 09 '13 at 06:10
  • @user1503665 these is my 91th line: throw new RuntimeException(e); – android Jan 09 '13 at 06:13
  • you are facing these problem becoz you are not setting your content properly.The error occuring bcoz of thses line : message.setContent("" + "" + title + "" + qty + " * " + cost + "" + " = " + total + " " + "", total); Please check thses link : http://stackoverflow.com/questions/5177045/getting-parseexception-on-transport-sendmessage – Neha.R Jan 09 '13 at 06:19

5 Answers5

1

As per the Javadoc, , while you need text/html.As your are using MimeMessage#setContent(), you need to add "text/html;charset==utf-8" at message.setContent();

message.setContent(someHtmlMessage, "text/html; charset=utf-8");

Note that the HTML should not contain the <html>, <head> or <body>. Gmail will ignore it.

message.setContent( "<tr>
                        <td>" + title + "</td>
                        <td>" + qty + "*" + cost+ "</td>
                        <td>" + "=" + total + "</td>
                    </tr>",   "text/html;charset=utf-8");
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • As `MimeMessage#setContent` sets `text/plain` by default so just use these `message.setContent("Hi Krishna"); or try these message.setContent(Hi Krishna)`. – Vikalp Patel Jan 09 '13 at 06:39
0
    Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Sourabh Saldi
  • 3,567
  • 6
  • 34
  • 57
0

you can try like this.

  String mailContent = "mailto:" + mailTo + "?subject=" + subject + "&body=" + body;

  Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(mailContent));
  startActivity(intent);

I think this will help you.

Raj
  • 1,843
  • 2
  • 21
  • 47
0
Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
    email.putExtra(Intent.EXTRA_SUBJECT, "subject");
    email.putExtra(Intent.EXTRA_TEXT, "message");
    email.setType("message/rfc822");
    startActivity(Intent.createChooser(email, "Choose an Email client :"));
Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
0

You are having only one Recipient in the To field, so you should use setRecipient method and not setRecipients.
Try this:

message.setRecipient(Message.RecipientType.TO, new InternetAddress(mercy.krishnaxxxdsdveni@gmail.com));

Hope this helps!!

ThePCWizard
  • 3,338
  • 2
  • 21
  • 32