以下Java代码用于将文件附加到HTML电子邮件并发送。我想在这封HTML电子邮件中添加附件。任何建议将不胜感激。
以下是翻译后的内容:
以下是翻译后的内容:
以下Java代码用于将文件附加到HTML电子邮件并发送。我希望能够在这封HTML电子邮件中添加附件。如有建议,敬请指教。
public void sendEmail(final String userName, final String password, final String host, final String html, final List<String> emails, String subject, String file) throws MessagingException
{
System.out.println("User Name: " + userName);
System.out.println("Password: " + password);
System.out.println("Host: " + host);
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
});
if (!emails.isEmpty())
{
//Compose the message
InternetAddress[] address = new InternetAddress[emails.size()];
for (int i = 0; i < emails.size(); i++)
{
address[i] = new InternetAddress(emails.get(i));
}
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(userName));
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String fileName = "attachmentName";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(html, "text/html; charset=utf-8");
message.setContent(multipart);
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} else
{
System.out.println("No Recieptions");
}
}
这只给我带来了附件。但我想发送包含此附件的HTML电子邮件。