无法使用 javamail 将 HTML 电子邮件发送到 Gmail
Posted
技术标签:
【中文标题】无法使用 javamail 将 HTML 电子邮件发送到 Gmail【英文标题】:Unable to send HTML emails using javamail to Gmail 【发布时间】:2015-05-21 13:19:22 【问题描述】:我正在尝试使用 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 但它不起作用,这里消息头的内容类型仍然是文本/纯文本。 检查此成绩单(通过在 gmail 中显示原件):
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--
需要注意的是header还是
------=_Part_0_1477499235.1426694244225
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
对于 html 正文,尽管设置为 text/plain 并执行 saveChanges()(请注意,标题行已被注释掉,但取消注释它们也会产生相同的结果) 为什么会发生这种情况以及解决方案是什么?
编辑: messageBodyPart.setText(someText,"text/html") 不存在。检查这张图片。
我使用的是 javamail 1.4.7。
【问题讨论】:
【参考方案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);
【讨论】:
这样的函数签名不存在。检查编辑。 方法是setContent(String,String)
不是setText
哦,我明白了。但请注意,如果我有带附件的邮件,您的解决方案将不起作用。所以我正在寻找一种同样适用于附件的解决方案。
这也适用于附件。您必须创建一个新的MimeBodyPart
,使用setDataHandler
和setFileName
并将其添加到您的MultiPart (multipart.addBodyPart(messageBodyPart)
)。我会更新我的答案
是的,我在另一个函数中做同样的事情。但是 mimeBodyPart.setContent(emailBody or What,String) 不存在以上是关于无法使用 javamail 将 HTML 电子邮件发送到 Gmail的主要内容,如果未能解决你的问题,请参考以下文章