感谢您阅读我的帖子。
我需要自动发送包含以下内容的电子邮件:
我无法在电子邮件中正确显示图像,并且PDF"正确管理"通过电子邮件客户端。
案例1
(见下面的代码)multipart = new MimeMultipart("mixed");
案例2
(见下面的代码)multipart = new MimeMultipart("related");
这是相反的:
我经常收到包含徽标和PDF附件的电子邮件(正如我们大多数人所做的那样)。
我注意到(在这些电子邮件的消息来源中)通常有两个部分:一个是multipart / mixed,另一个是multipart / related。
我无法编写电子邮件客户端正确处理徽标和附件的代码。
您可以在下面找到我已编写的代码。
感谢您的帮助和最好的问候。
public void send(String s_fromEmailAddress, String s_toEmailAddress, String s_messageSubject, String s_messageAttachmentFileName, String s_messageAttachmentFileNameToDisplay)
throws AddressException, MessagingException, IOException
{
Properties properties = null;
Session session = null;
Message message = null;
BodyPart messageBodyPart = null;
Multipart multipart = null;
DataSource fileDataSource = null;
String s_htmlText = null;
DataSource fds = null;
//------------------------------------------------------------------
// Get system properties.
//------------------------------------------------------------------
properties = System.getProperties();
//------------------------------------------------------------------
// Setup mail server.
//------------------------------------------------------------------
properties.setProperty("mail.smtp.host", "smtp.wanadoo.fr");
//------------------------------------------------------------------
// Get the default Session object.
//------------------------------------------------------------------
session = Session.getDefaultInstance(properties);
//------------------------------------------------------------------
// Define message.
//------------------------------------------------------------------
message = new MimeMessage(session);
message.setFrom(new InternetAddress(s_fromEmailAddress));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(s_toEmailAddress));
message.setSubject(s_messageSubject);
//------------------------------------------------------------------
// Create the message part.
//------------------------------------------------------------------
multipart = new MimeMultipart("related");
//------------------------------------------------------------------
// HTML part
//------------------------------------------------------------------
s_htmlText =
"<html> \n"
+ " <head> \n"
+ " <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"> \n"
+ " </head> \n"
+ " <body> \n"
+ " <img width=\"100px\" \n"
+ " height=\"37px\" \n"
+ " style=\"margin-left: 1em;\" \n"
+ " src=\"cid:image\" \n"
+ " alt=\"LOGO\" />\n"
+ " <br /> \n"
+ ms_messageBodyPartText
+ " </body> \n"
+ "</html> ";
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(s_htmlText, "text/html; charset=utf-8");
messageBodyPart.setHeader("Content-Transfer-Encoding", "8bit");
multipart.addBodyPart(messageBodyPart);
//------------------------------------------------------------------
// Logo (image)
//------------------------------------------------------------------
messageBodyPart = new MimeBodyPart();
fds = new FileDataSource("where_the_image_can_be_found//Logo1_25_p_cent.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
messageBodyPart.addHeader("Content-Type", "image/png");
multipart.addBodyPart(messageBodyPart);
//------------------------------------------------------------------
// This part is the attachment.
//------------------------------------------------------------------
if((s_messageAttachmentFileName != null)
&& (s_messageAttachmentFileNameToDisplay != null))
{
messageBodyPart = new MimeBodyPart();
fileDataSource = new FileDataSource(s_messageAttachmentFileName);
messageBodyPart.setDataHandler(new DataHandler(fileDataSource));
messageBodyPart.addHeader("Content-Type", "application/pdf");
messageBodyPart.setFileName(s_messageAttachmentFileNameToDisplay);
multipart.addBodyPart(messageBodyPart);
}
//------------------------------------------------------------------
// Put parts in message.
//------------------------------------------------------------------
message.setContent(multipart);
//------------------------------------------------------------------
// Send the message
//------------------------------------------------------------------
Transport.send(message);
}