2

我正在尝试通过 GMail(实际上是 Google Apps)发送电子邮件。这对于普通消息非常有效,但当然我必须测试极端情况。

现在我正在尝试发送没有正文的消息,但收到一条奇怪的错误消息。因为我想消除代码中的错误,所以我想知道我是否做错了什么,或者我正在使用的库中有错误。

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();

此代码适用于“普通”电子邮件:具有有效的正文、主题、收件人等。

当数据库中的mailBody mailHtmlBody都为NULL或为空时,我收到以下错误:

将电子邮件发送到以下服务器失败:smtp.gmail.com:25

完整的错误日志(自己的日志格式)/堆栈跟踪:

[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

我现在有两个问题:

  1. 是否可以发送没有正文的电子邮件?这是允许的吗?
  2. 为什么错误消息的端口号是 25 而不是 465?
4

5 回答 5

5

MIME 规范不允许没有正文部分的多部分内容。JavaMail API(我认为是 1.5 版)允许您将系统属性设置mail.mime.multipart.allowemptytrue解决此问题。

于 2013-09-04T06:53:38.840 回答
4

是否可以发送没有正文的电子邮件?这是允许的吗?

是的,RFC 5322 特别允许电子邮件消息有一个空的正文:

“消息由标头字段组成,可选地后跟消息正文。”

这是BNF:

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

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

于 2013-04-10T17:23:49.680 回答
3

尽管我找不到它的文档,但经验证明表明,Apache Commons Email HtmlEmail 对象中不允许空的正文部分。文本消息或 html 消息应设置为非空字符串。

端口 25 的错误消息是 Apache Commons 电子邮件代码中的错误,在打印 smtp 端口时未考虑 SSL。为此,已在https://issues.apache.org/jira/browse/EMAIL-123提交了错误报告

于 2012-12-19T17:21:25.600 回答
0

我有完全相同的例外,但最终成为一个愚蠢的错误,我忘记将消息的正文添加到多部分消息中。因此错误!

BodyPart body = new MimeBodyPart();
body.setText(("Message Body"));
multipart.addBodyPart(body);   // ** here
message.setContent(multipart);
Transport.send(message);
于 2017-05-01T18:36:08.090 回答
-1

要使用 gmail 发送电子邮件,您还应该启用传输层安全性。

currentMessage.setTLS(true);
于 2012-12-19T11:29:54.177 回答