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

Unable to send HTML emails using javamail to Gmail

我正在尝试使用 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 但它不起作用,这里消息的内容类型 header 仍然是 text/plain。 检查此成绩单(通过在 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 body,尽管设置为 text/plain 并执行 saveChanges() (请注意 header 行已被注释掉,但取消注释它们也给出相同的结果) 为什么会这样,解决方案是什么?

编辑: messageBodyPart.setText(someText,"text/html") 不存在。检查这张图片。

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