我正在尝试使用javamail发送html电子邮件。这是我的功能:
private void sendBCCEmailWithoutAttachments(ArrayList<String> recipients
, String emailBody, final String senderEmail, final String senderPassword, String subject) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmail, senderPassword);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderEmail));
String addressList = "";
for (String st : recipients) {
addressList = addressList+"," + st;
}
addressList = addressList.substring(1);
message.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(addressList));
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
// messageBodyPart.setHeader("Content-Type", "text/html");
messageBodyPart.setText(emailBody);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart,"text/html");
message.saveChanges();
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
从上面可以看出,我将message.saveChanges()称为给定的here但它不起作用,这里消息头的内容类型仍然是text / plain。 检查此成绩单(通过gmail中的show original):
Delivered-To: somegmail@gmail.com
Received: by 10.27.30.195 with SMTP id e186csp1346918wle;
Wed, 18 Mar 2015 08:57:42 -0700 (PDT)
X-Received: by 10.70.124.138 with SMTP id mi10mr104883801pdb.39.1426694262128;
Wed, 18 Mar 2015 08:57:42 -0700 (PDT)
Return-Path: <someother@gmail.com>
Received: from mail-pd0-x231.google.com (mail-pd0-x231.google.com. [2607:f8b0:400e:c02::231])
by mx.google.com with ESMTPS id iu3si29690992pac.32.2015.03.18.08.57.41
for <somegmail@gmail.com>
(version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
Wed, 18 Mar 2015 08:57:42 -0700 (PDT)
Received-SPF: pass (google.com: domain of someother@gmail.com designates 2607:f8b0:400e:c02::231 as permitted sender) client-ip=2607:f8b0:400e:c02::231;
Authentication-Results: mx.google.com;
spf=pass (google.com: domain of someother@gmail.com designates 2607:f8b0:400e:c02::231 as permitted sender) smtp.mail=someother@gmail.com;
dkim=pass header.i=@gmail.com;
dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Received: by mail-pd0-x231.google.com with SMTP id op1so45152900pdb.2
for <somegmail@gmail.com>; Wed, 18 Mar 2015 08:57:41 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=date:from:message-id:subject:mime-version:content-type;
bh=WSWojtu/8q0G3uIiPm3laNDTSWDX+1m/PuoKmXMINx4=;
b=lFUGafjwCev6jX2/In3pdwygnPgKWSOTFsjcZKILhXNqiS5ipInje4dd5aVvtVzY0o
wfzHgmtrw8lqdjnAdQt2BJW6PmVavzUU7mmj+VMAU5oi0iSsAYYUUAbIl3FW24EVG03d
EcDqUcT9o9/iJCwOBkyaNj5BHEj/AFIog3tmk7hU8K51mXo/Gk5ZPjAi8ZwKPgr2txuA
KdeJ08p7qquAKxgpMD5UYw1Un6WrAccIrlWM4kpk6CVVDJpQuAeu63MHrf2MablQQ3PI
MDKmp3ZsfBhPmpZkMw77ADGs6GRe3v7TqnEZ2LIoOahB+ESLnfkil5oBIjFCHMPoACIq
eH/g==
X-Received: by 10.66.181.144 with SMTP id dw16mr167500378pac.100.1426694261402;
Wed, 18 Mar 2015 08:57:41 -0700 (PDT)
Return-Path: <someother@gmail.com>
Received: from Admin ([123.236.200.54])
by mx.google.com with ESMTPSA id os6sm28300083pac.28.2015.03.18.08.57.39
for <somegmail@gmail.com>
(version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
Wed, 18 Mar 2015 08:57:40 -0700 (PDT)
Date: Wed, 18 Mar 2015 08:57:40 -0700 (PDT)
From: someother@gmail.com
Message-ID: <915430510.2.1426694244252.JavaMail.somegmail@Admin>
Subject: dub
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1477499235.1426694244225"
------=_Part_0_1477499235.1426694244225
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html>
<head>
<title></title>
</head>
<body>
<p>
Type in Text here and Click on <strong>Source button </strong>to get HTML Source Code. </p>
</body>
</html>
------=_Part_0_1477499235.1426694244225--
要注意的是标题仍然是
------=_Part_0_1477499235.1426694244225
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
对于html正文,尽管设置为text / plain并执行saveChanges()(请注意标题行已注释掉,但取消注释它们也会得到相同的结果) 为什么会发生这种情况并解决方案?
修改 messageBodyPart.setText(someText,&#34; text / html&#34;)不存在。查看此图片。
我使用的是javamail 1.4.7。
答案 0 :(得分:1)
添加
MimeBodyPart messageBodyPart=new MimeBodyPart();
messageBodyPart.setContent(emailBody, "text/html");
// Add an attachment
attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(attachment));
attachmentBodyPart.setFileName(attachment.getName());
multipart.addBodyPart(attachmentBodyPart);
// and so on...
而不是
// messageBodyPart.setHeader("Content-Type", "text/html");
messageBodyPart.setText(emailBody);