Reputation: 11205
I am trying to send html email with javamail. Here is my function:
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);
}
}
As its clear from above, I am calling the message.saveChanges() as given here But it does not work and here the content type of the message header is still text/plain. Check this transcript (via show original in gmail):
Delivered-To: [email protected]
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: <[email protected]>
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 <[email protected]>
(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 [email protected] 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 [email protected] designates 2607:f8b0:400e:c02::231 as permitted sender) [email protected];
dkim=pass [email protected];
dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Received: by mail-pd0-x231.google.com with SMTP id op1so45152900pdb.2
for <[email protected]>; 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: <[email protected]>
Received: from Admin ([123.236.200.54])
by mx.google.com with ESMTPSA id os6sm28300083pac.28.2015.03.18.08.57.39
for <[email protected]>
(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: [email protected]
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--
The point to note is that header is still
------=_Part_0_1477499235.1426694244225
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
for the html body, despite of setting as text/plain and doing saveChanges() (Pls note that the header lines are commented out, but uncommenting them also gives the same result) Why is that happening and whats the solution?
EDIT: messageBodyPart.setText(someText,"text/html") does not exist. Chk this image.
I am using javamail 1.4.7.
Upvotes: 1
Views: 933
Reputation: 1337
Add
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...
instead of
// messageBodyPart.setHeader("Content-Type", "text/html");
messageBodyPart.setText(emailBody);
Upvotes: 1