Properties props =
new
Properties();
props.put(
"mail.smtp.host"
,
"localhost"
);
Session session = session.getDefaultInstance(props);
MimeMessage mimeMessage =
new
MimeMessage(session);
MimeMultipart rootMixedMultipart =
new
MimeMultipart(
"mixed"
);
mimeMessage.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart =
new
MimeMultipart(
"related"
);
MimeBodyPart relatedBodyPart =
new
MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
MimeMultipart messageBody =
new
MimeMultipart(
"alternative"
);
MimeBodyPart bodyPart =
null
;
for
(
int
i =
0
; i < nestedRelatedMultipart.getCount(); i++) {
BodyPart bp = nestedRelatedMultipart.getBodyPart(i);
if
(bp.getFileName() ==
null
) {
bodyPart = (MimeBodyPart) bp;
}
}
if
(bodyPart ==
null
) {
MimeBodyPart mimeBodyPart =
new
MimeBodyPart();
nestedRelatedMultipart.addBodyPart(mimeBodyPart);
bodyPart = mimeBodyPart;
}
bodyPart.setContent(messageBody,
"text/alternative"
);
MimeBodyPart plainTextPart =
new
MimeBodyPart();
plainTextPart.setText(
"This is plain text message"
,
"UTF-8"
);
messageBody.addBodyPart(plainTextPart);
MimeBodyPart htmlTextPart =
new
MimeBodyPart();
htmlTextPart.setContent(
"<h1>This is plain HTML message"
,
"text/html;charset=UTF-8"
);
messageBody.addBodyPart(htmlTextPart);
mimeMessage.setReplyTo(
new
InternetAddress[]{
new
InternetAddress(
"<a href="
https:
mimeMessage.setFrom(
new
InternetAddress(
"<a href="
https:
mimeMessage.setRecipient(Message.RecipientType.TO,
new
InternetAddress(
"<a href="
https:
mimeMessage.setSentDate(
new
Date());
mimeMessage.setSubject(
"Mixed message"
);
Transport.send(mimeMessage);