gpt4 book ai didi

java - 无法使用 javamail 向 Gmail 发送 HTML 电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:29 26 4
gpt4 key购买 nike

我正在尝试使用 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);
}
}

从上面可以清楚地看出,我正在按照给定的 here 调用 message.saveChanges()但它不起作用,这里消息头的内容类型仍然是文本/纯文本。检查此文字记录(通过在 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.&nbsp;</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") 不存在。检查这个图像。 enter image description here

我使用的是javamail 1.4.7。

最佳答案

添加

   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);

关于java - 无法使用 javamail 向 Gmail 发送 HTML 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com