使用 JavaMail 发送带附件的邮件 - 编写 Multipart 时出现异常
Sending mail with attachment with JavaMail - Exception while writing Multipart
我想使用 JavaMail 发送带附件的邮件,但我遇到了一个难以理解的异常。
我的代码分为两部分。第一个是 class EmailSender
,负责管理用于发送邮件的邮件帐户和系统配置。第二个是管理单个电子邮件的 class Mail
。 (代码在post末尾)
创建 EmailSender
时,构造函数会使用方法 setServerHost()
从已知设置列表中自动搜索 SMTP 设置。
当 EmailSender
被要求发送 Email
时,EmailSender
将 Email
字段中包含的信息转换为 MimeMessage
通过调用 Email
的 build()
方法;然后使用 JavaMail 的 Transport
class 发送。
我使用的测试方法比较简单:
public static void sendMail(String subject, String body, String to, String urlAttachment) {
System.out.printf("Username:\t");
String username = readString();
System.out.printf("Password:\t");
String password = readString();
EmailSender account = new EmailSender(username, password);
Email mail = new Email(username, to, subject, body);
mail.addAttachment(urlAttachment);
account.sendMail(to, subject, body);
}
反过来我得到的错误就不那么严重了。
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: Exception writing Multipart
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1365)
at mail.EmailSender.sendMail(EmailSender.java:104)
at mail.EmailSender.sendMail(EmailSender.java:122)
at Test.TestLibraries.sendMail(TestLibraries.java:134)
at Test.TestLibraries.main(TestLibraries.java:51)
Caused by: java.io.IOException: Exception writing Multipart
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:86)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1694)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1913)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1315)
... 4 more
Caused by: javax.mail.MessagingException: Empty multipart: multipart/mixed;
boundary="----=_Part_0_2129789493.1581503162040"
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:556)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:84)
... 9 more
问题是什么(我该如何解决)?
代码如下:
public class EmailSender {
private String from;
private String password;
private String emailHost;
private Properties properties = System.getProperties();
private Session session;
public static final int serverName = 0;
public static final int serverPort = 1;
public static final int serverAutentication = 2;
public static final String[][] knownServerHostData = new String[][]
{
{ "smtp.mail.yahoo.com", "587", "SLL" } ,
{ "smtp.mail.com", "587", "StartTLS" } ,
{ "smtp.gmail.com", "587", "" } ,
{ "out.virgilio.it", "587", "" }
};
public EmailSender(String username, String password) {
this.from = username;
this.password = password;
this.session = Session.getDefaultInstance(properties);
this.setServerHost(password);
}
public boolean sendMail(String to, String subject, String body) {
return sendMail(new Email(from, to, subject, body));
}
public boolean sendMail(Email email) {
MimeMessage message = email.build(session);
Transport transport = null;
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) { e.printStackTrace(); closeTransport(transport); }
try {
transport.connect(emailHost, from, password);
} catch (MessagingException e) { e.printStackTrace(); closeTransport(transport); }
try {
transport.sendMessage(message, message.getAllRecipients()); // <== THIS LINE RETURN EXCEPTION
} catch (MessagingException e) { e.printStackTrace(); closeTransport(transport); }
closeTransport(transport);
return true;
}
private void closeTransport(Transport transport) {
try { transport.close();
} catch (MessagingException e) { e.printStackTrace(); }
}
}
public class Email {
private String sender;
private Vector<String> recipients = new Vector<String>();
private Vector<String> cc = new Vector<String>();
private Vector<String> bcc = new Vector<String>();
private String subject;
private String body;
private Vector<String> attachments = new Vector<String>();
public Email(String from, String to, String subject, String body) {
this.sender = from;
this.recipients.add(to);
this.subject = subject;
this.body = body;
}
/** Returns a {@link MimeMessage} ready to be sent by an {@link EmailSender} with all the fields of {@code this} {@link Email}.
*
* @return
*/
public MimeMessage build(Session session) {
MimeMessage message = new MimeMessage(session);
// STEP 1 - Header
// Sets the sender
try { message.setFrom(new InternetAddress(sender));
} catch (AddressException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// Sets the subject
try { message.setSubject(subject);
} catch (MessagingException e) { e.printStackTrace(); }
// Adds the recipients one by one
int i = 0;
try {
for(i=0 ; i<recipients.size() ; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th recipient gave error."); }
try {
for(i=0 ; i<cc.size() ; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th cc gave error."); }
try {
for(i=0 ; i<bcc.size() ; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th bcc gave error."); }
// STEP 2 - Body
// Adds the body
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
try {
messageBodyPart.setContent(message, "text/plain; charset=" +
MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
} catch (MessagingException e) { e.printStackTrace(); }
try { message.setText(body);
} catch (MessagingException e) { e.printStackTrace(); }
// Adds the attachments
try {
for(i=0 ; i<attachments.size() ; i++) // Preps the attachments
attachFileToMessageMultipart(multipart, attachments.get(i));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
catch (IOException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
// STEP 3 - Appends the MimeMessage's body
try {
message.setContent(multipart);
} catch (MessagingException e1) { e1.printStackTrace(); }
return message;
}
/** This method avoids compatibility problems between JavaMail 1.3 and JavaMail 1.4.
* @throws MessagingException
* @throws IOException
*
*/
private static void attachFileToMessageMultipart(Multipart multipart, String fileUrl) throws MessagingException, IOException {
File file = new File(fileUrl);
if( ! file.isFile() )
throw new IOException("The specified url does not identify a file.");
// JavaMail 1.3
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileUrl);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(file.getName());
multipart.addBodyPart(attachPart);
}
编辑: 阅读 Bill Shannon 的回答后,我编辑了我的方法 build()
。当前版本是:
MimeMessage message = new MimeMessage(session);
System.out.println("\t Building mail.");
// STEP 1 - Header
// Sets the sender
try { message.setFrom(new InternetAddress(sender));
} catch (AddressException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// Sets the subject
try { message.setSubject(subject);
} catch (MessagingException e) { e.printStackTrace(); }
System.out.println("\t\t Sender and subject set mail.");
// Adds the recipients one by one
int i = 0;
try {
for(i=0 ; i<recipients.size() ; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th recipient gave error."); }
try {
for(i=0 ; i<cc.size() ; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th cc gave error."); }
try {
for(i=0 ; i<bcc.size() ; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th bcc gave error."); }
System.out.println("\t\t TO, CC, BCC fields setted.");
// STEP 2 - Body
// Adds the body
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
try {
messageBodyPart.setText(body);
} catch (MessagingException e) { e.printStackTrace(); }
try {
multipart.addBodyPart(messageBodyPart);
} catch (IllegalWriteException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// try {
// messageBodyPart.setContent(message, "text/plain; charset=" +
// MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
// } catch (MessagingException e) { e.printStackTrace(); }
System.out.println("\t\t Body attached.");
// Adds the attachments
for(i=0 ; i<attachments.size() ; i++)
{
// Creates a BodyPart representing the attachment
try {
messageBodyPart.attachFile(attachments.get(i));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
catch (IOException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
// Appends the BodyPart to the MultiPart
try {
multipart.addBodyPart(messageBodyPart);
} catch (IllegalWriteException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
}
System.out.println("\t\t Files attached.");
// STEP 3 - Appends the MimeMessage's body
try {
message.setContent(multipart);
} catch (MessagingException e1) { e1.printStackTrace(); }
System.out.println("\t\t MimeMessage created.");
return message;
}
此版本没有例外,邮件已发送...但没有附件。
我不明白你想在这里做什么:
messageBodyPart.setContent(message, "text/plain; charset=" +
MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
我确定您不是要将 MimeMessage 对象本身添加为
消息正文部分的内容。你永远不需要
以这种方式使用MimeUtility.quote方法。
这条语句:
message.setText(body);
将 MimeMessage 对象的全部内容设置为纯文本
以正文字符串作为内容的文本消息。我不认为
这就是你想要的。
你想要的是使用setText方法来设置内容
messageBodyPart 对象到字符串正文。然后添加 messageBodyPart
对象到多部分对象。
之后,您可以将所有附件添加到多部分对象中。请注意,您可能想要使用 MimeBodyPart.atttachFile
简化代码的方法。
我想使用 JavaMail 发送带附件的邮件,但我遇到了一个难以理解的异常。
我的代码分为两部分。第一个是 class EmailSender
,负责管理用于发送邮件的邮件帐户和系统配置。第二个是管理单个电子邮件的 class Mail
。 (代码在post末尾)
创建 EmailSender
时,构造函数会使用方法 setServerHost()
从已知设置列表中自动搜索 SMTP 设置。
当 EmailSender
被要求发送 Email
时,EmailSender
将 Email
字段中包含的信息转换为 MimeMessage
通过调用 Email
的 build()
方法;然后使用 JavaMail 的 Transport
class 发送。
我使用的测试方法比较简单:
public static void sendMail(String subject, String body, String to, String urlAttachment) {
System.out.printf("Username:\t");
String username = readString();
System.out.printf("Password:\t");
String password = readString();
EmailSender account = new EmailSender(username, password);
Email mail = new Email(username, to, subject, body);
mail.addAttachment(urlAttachment);
account.sendMail(to, subject, body);
}
反过来我得到的错误就不那么严重了。
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: Exception writing Multipart
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1365)
at mail.EmailSender.sendMail(EmailSender.java:104)
at mail.EmailSender.sendMail(EmailSender.java:122)
at Test.TestLibraries.sendMail(TestLibraries.java:134)
at Test.TestLibraries.main(TestLibraries.java:51)
Caused by: java.io.IOException: Exception writing Multipart
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:86)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1694)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1913)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1315)
... 4 more
Caused by: javax.mail.MessagingException: Empty multipart: multipart/mixed;
boundary="----=_Part_0_2129789493.1581503162040"
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:556)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:84)
... 9 more
问题是什么(我该如何解决)?
代码如下:
public class EmailSender {
private String from;
private String password;
private String emailHost;
private Properties properties = System.getProperties();
private Session session;
public static final int serverName = 0;
public static final int serverPort = 1;
public static final int serverAutentication = 2;
public static final String[][] knownServerHostData = new String[][]
{
{ "smtp.mail.yahoo.com", "587", "SLL" } ,
{ "smtp.mail.com", "587", "StartTLS" } ,
{ "smtp.gmail.com", "587", "" } ,
{ "out.virgilio.it", "587", "" }
};
public EmailSender(String username, String password) {
this.from = username;
this.password = password;
this.session = Session.getDefaultInstance(properties);
this.setServerHost(password);
}
public boolean sendMail(String to, String subject, String body) {
return sendMail(new Email(from, to, subject, body));
}
public boolean sendMail(Email email) {
MimeMessage message = email.build(session);
Transport transport = null;
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) { e.printStackTrace(); closeTransport(transport); }
try {
transport.connect(emailHost, from, password);
} catch (MessagingException e) { e.printStackTrace(); closeTransport(transport); }
try {
transport.sendMessage(message, message.getAllRecipients()); // <== THIS LINE RETURN EXCEPTION
} catch (MessagingException e) { e.printStackTrace(); closeTransport(transport); }
closeTransport(transport);
return true;
}
private void closeTransport(Transport transport) {
try { transport.close();
} catch (MessagingException e) { e.printStackTrace(); }
}
}
public class Email {
private String sender;
private Vector<String> recipients = new Vector<String>();
private Vector<String> cc = new Vector<String>();
private Vector<String> bcc = new Vector<String>();
private String subject;
private String body;
private Vector<String> attachments = new Vector<String>();
public Email(String from, String to, String subject, String body) {
this.sender = from;
this.recipients.add(to);
this.subject = subject;
this.body = body;
}
/** Returns a {@link MimeMessage} ready to be sent by an {@link EmailSender} with all the fields of {@code this} {@link Email}.
*
* @return
*/
public MimeMessage build(Session session) {
MimeMessage message = new MimeMessage(session);
// STEP 1 - Header
// Sets the sender
try { message.setFrom(new InternetAddress(sender));
} catch (AddressException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// Sets the subject
try { message.setSubject(subject);
} catch (MessagingException e) { e.printStackTrace(); }
// Adds the recipients one by one
int i = 0;
try {
for(i=0 ; i<recipients.size() ; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th recipient gave error."); }
try {
for(i=0 ; i<cc.size() ; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th cc gave error."); }
try {
for(i=0 ; i<bcc.size() ; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th bcc gave error."); }
// STEP 2 - Body
// Adds the body
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
try {
messageBodyPart.setContent(message, "text/plain; charset=" +
MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
} catch (MessagingException e) { e.printStackTrace(); }
try { message.setText(body);
} catch (MessagingException e) { e.printStackTrace(); }
// Adds the attachments
try {
for(i=0 ; i<attachments.size() ; i++) // Preps the attachments
attachFileToMessageMultipart(multipart, attachments.get(i));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
catch (IOException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
// STEP 3 - Appends the MimeMessage's body
try {
message.setContent(multipart);
} catch (MessagingException e1) { e1.printStackTrace(); }
return message;
}
/** This method avoids compatibility problems between JavaMail 1.3 and JavaMail 1.4.
* @throws MessagingException
* @throws IOException
*
*/
private static void attachFileToMessageMultipart(Multipart multipart, String fileUrl) throws MessagingException, IOException {
File file = new File(fileUrl);
if( ! file.isFile() )
throw new IOException("The specified url does not identify a file.");
// JavaMail 1.3
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileUrl);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(file.getName());
multipart.addBodyPart(attachPart);
}
编辑: 阅读 Bill Shannon 的回答后,我编辑了我的方法
build()
。当前版本是:
MimeMessage message = new MimeMessage(session);
System.out.println("\t Building mail.");
// STEP 1 - Header
// Sets the sender
try { message.setFrom(new InternetAddress(sender));
} catch (AddressException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// Sets the subject
try { message.setSubject(subject);
} catch (MessagingException e) { e.printStackTrace(); }
System.out.println("\t\t Sender and subject set mail.");
// Adds the recipients one by one
int i = 0;
try {
for(i=0 ; i<recipients.size() ; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th recipient gave error."); }
try {
for(i=0 ; i<cc.size() ; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th cc gave error."); }
try {
for(i=0 ; i<bcc.size() ; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th bcc gave error."); }
System.out.println("\t\t TO, CC, BCC fields setted.");
// STEP 2 - Body
// Adds the body
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
try {
messageBodyPart.setText(body);
} catch (MessagingException e) { e.printStackTrace(); }
try {
multipart.addBodyPart(messageBodyPart);
} catch (IllegalWriteException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// try {
// messageBodyPart.setContent(message, "text/plain; charset=" +
// MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
// } catch (MessagingException e) { e.printStackTrace(); }
System.out.println("\t\t Body attached.");
// Adds the attachments
for(i=0 ; i<attachments.size() ; i++)
{
// Creates a BodyPart representing the attachment
try {
messageBodyPart.attachFile(attachments.get(i));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
catch (IOException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
// Appends the BodyPart to the MultiPart
try {
multipart.addBodyPart(messageBodyPart);
} catch (IllegalWriteException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
}
System.out.println("\t\t Files attached.");
// STEP 3 - Appends the MimeMessage's body
try {
message.setContent(multipart);
} catch (MessagingException e1) { e1.printStackTrace(); }
System.out.println("\t\t MimeMessage created.");
return message;
}
此版本没有例外,邮件已发送...但没有附件。
我不明白你想在这里做什么:
messageBodyPart.setContent(message, "text/plain; charset=" +
MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
我确定您不是要将 MimeMessage 对象本身添加为 消息正文部分的内容。你永远不需要 以这种方式使用MimeUtility.quote方法。
这条语句:
message.setText(body);
将 MimeMessage 对象的全部内容设置为纯文本 以正文字符串作为内容的文本消息。我不认为 这就是你想要的。
你想要的是使用setText方法来设置内容 messageBodyPart 对象到字符串正文。然后添加 messageBodyPart 对象到多部分对象。
之后,您可以将所有附件添加到多部分对象中。请注意,您可能想要使用 MimeBodyPart.atttachFile 简化代码的方法。