Search code examples
androidjakarta-mail

When sending email using javamail-android the body is attached as a text file


I am using the JavaMail port for android to send emails without user intervention in the application. And I have noticed that some email systems does not display the email body correctly. When I send the email to Gmail the body is shown correctly, but when I send it to a company email (I am not exactly sure what it is using but it's a business mail from Telecom) the body is blank and instead there is an attachment of a text file ATT00001.txt with the content of body.

I am not an expert on the mail systems but from the code I use to set the body of email it seems like it is very similar to how you attach a file, and maybe that is why the body is not recognised as the body, but as an attachment.

The following code snippet is how the mail is created. The body and subjects are all initialised beforehand.

public synchronized void send() throws MessagingException {   
    MimeMessage message = new MimeMessage(session);   
    BodyPart bodyPart = new MimeBodyPart();
    bodyPart.setText(body);
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    //message.setDataHandler(handler);
    multipart.addBodyPart(bodyPart);
    message.setContent(multipart);
    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
    Transport.send(message);  
    user = null;
    password = null;
    session = null;
}   

If anyone knows how to get around this problem please help me! It is important for my application to show the body as the body. Also, if there is a better way of sending emails without user intervention, please suggest them too.

Thanks for your help.

EDIT:

I tried using .setText() instead of BodyPart

message.setText(body);

This did not show the body at all from Gmail, and showed some weird string from the company mail

------=_Part_2_1079750440.1339556360521--

And while testing these, I created an email test activity which basically sends an email with a button click. After trying out a few things, I changed my class back to how it was (using BodyPart) and then it worked! It looks like the attachment was causing the problem and I think the way I'm attaching my files isn't correct.

public void addAttachment(File file) throws MessagingException {
    BodyPart messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(file);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(file.getName());
    multipart.addBodyPart(messageBodyPart);     
}

This addAttachment() function is called before send().

What is wrong with this?

EDIT:

In case anyone else is having the same issues, here's how I fixed it. The problem was fixed by setting the body before adding attachments. So instead of setting the body when sending, I set the body separately beforehand.

public void setBody(String body) throws MessagingException {
    BodyPart bodyPart = new MimeBodyPart();
    bodyPart.setText(body);
    multipart.addBodyPart(bodyPart);
}

public synchronized void send() throws MessagingException {   
    MimeMessage message = new MimeMessage(session);   
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    message.setContent(multipart);
    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
    Transport.send(message);  
    user = null;
    password = null;
    session = null;
}   

So when I call the functions, I call setBody() first, then addAttachment(), then send(). Now the body of the email is shown as it should.


Solution

  • Try just creating a plain text message without using a multipart. Get rid of message.setContent(multipart) and use message.setText(body).