Question

I'm trying to send an email message via GMail (Google Apps, actually). This works perfectly for normal messages, but of course I have to test the extremes.

Right now I'm trying to send a message without a body and I'm getting a strange error message. As I want to eliminate errors in my code, I'd like to know whether I'm doing something wrong or that there is an error in the libraries I'm using.

HtmlEmail currentMessage = new HtmlEmail();
currentMessage.setSSL(true);
currentMessage.setSslSmtpPort(465);
currentMessage.setHostName("smtp.gmail.com");
currentMessage.setAuthentication("abc@gmail.com", "secret");

/** rs is a ResultSet from database **/

if(rs.getString("mailFrom") != null && !rs.getString("mailFrom").isEmpty())
    currentMessage.setFrom(rs.getString("mailFrom"));
else
    currentMessage.setFrom("abc@gmail.com");
currentMessage.setTo(this.convertRecipientStringToArray(rs.getString("mailTo")));
currentMessage.setCc(this.convertRecipientStringToArray(rs.getString("mailCC")));
currentMessage.setBcc(this.convertRecipientStringToArray(rs.getString("mailBC")));
currentMessage.setSubject(rs.getString("mailSubject"));
if(rs.getString("mailBody") != null && !rs.getString("mailBody").isEmpty())
    currentMessage.setTextMsg(rs.getString("mailBody"));
if(rs.getString("mailHtmlBody") != null && !rs.getString("mailHtmlBody").isEmpty())
    currentMessage.setHtmlMsg(rs.getString("mailHtmlBody"));
if(rs.getString("mailReplyTo") != null && !rs.getString("mailReplyTo").isEmpty())
    currentMessage.addReplyTo(rs.getString("mailReplyTo"));
else
    currentMessage.addReplyTo("def@gmail.com");
currentMessage.send();

This code works for "normal" emails: with a valid body, subject, addressees etc.

When both mailBody and mailHtmlBody are NULL or empty in the databse, I get the following error:

Sending the email to the following server failed : smtp.gmail.com:25

Full error log (own log format) / stacktrace:

[CRITICAL] 2012-12-19 17:08:00 [CET] - Exception occurred in function com.mypackage.mymailobject.outgoing_sendMails: Sending the email to the following server failed : smtp.gmail.com:25

Stack trace:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:25
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
    at org.apache.commons.mail.Email.send(Email.java:1267)
    at com.mypackage.mymailobject.outgoing_sendMails(mymailobject.java:85)
    at com.mypackage.mymailobject.outgoing_do(mymailobject.java:69)
    at com.mypackage.Mailer.main(Mailer.java:132)
Caused by: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.IOException: javax.mail.MessagingException: Empty multipart: multipart/mixed; 
    boundary="----=_Part_0_916488860.1355933279096"
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
    ... 4 more
Caused by: java.io.IOException: javax.mail.MessagingException: Empty multipart: multipart/mixed; 
    boundary="----=_Part_0_916488860.1355933279096"
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105)
    at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
    at javax.activation.DataHandler.writeTo(Unknown Source)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099)
    ... 7 more

I now have 2 questions:

  1. Should it be possible to send an email without a body? Is that allowed?
  2. Why has the error message port number 25 instead of 465?
Was it helpful?

Solution 3

Although I can't find documentation for it, empirical proof shows me that empty bodyparts are not allowed in Apache Commons Email HtmlEmail objects. Either the text message or the html message should be set to a non-empty string.

The error message with port 25 is an error in the code of the Apache Commons Email which doesn't take SSL into account when printing out the smtp port. For this, a bug report has been filed at https://issues.apache.org/jira/browse/EMAIL-123

OTHER TIPS

The MIME spec does not allow multipart content with no body parts. The JavaMail API (as of version 1.5, I think), allows you to set the system property mail.mime.multipart.allowempty to true to work around this.

Should it be possible to send an email without a body? Is that allowed?

Yes, RFC 5322 specifically allows an email message to have an empty body:

"A message consists of header fields, optionally followed by a message body."

Here's the BNF:

body            =   (*(*998text CRLF) *998text) / obs-body

https://www.rfc-editor.org/rfc/rfc5322#section-3.5

I had the exact same exception but ended up as a silly mistake i forgot adding the body of the message to the Multipart Message. hence the error!

BodyPart body = new MimeBodyPart();
body.setText(("Message Body"));
multipart.addBodyPart(body);   // ** here
message.setContent(multipart);
Transport.send(message);

To send email with gmail, think you should enable Transport Layer Security also.

currentMessage.setTLS(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top