Hi
I am trying to send emails with 1)plain text and 2)html .
My problem is while sending html emails if I have some special character say (ź
, this character gets screwed up. This character is what the user inputs in a jsp, so is generated dynamically. With plain text its fine.
I use the below code for plain text it:
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
props.put("mail.mime.charset","UTF-8");
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Send the message --
Transport.send(msg);
For HTML I just comment
props.put("mail.mime.charset","UTF-8");
and instead of
msg.setText(body);
I use
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(strMessageBody,"text/html")));
Am I doing something wrong for HTML? Whats the correct way for it
Thanks
M