pound sign is not working in mail content using java.mail package?
HI all,
I am using javax.mail packaage MINEMESSAGE,MimeMultipart class to send a mail, but even though I mention type utf-8, unicode characters are not working in body text. like pound sign is not working. please help what to do. here is my message headers
To: kumar.k@mysite.com
Message-ID: <875158456.1.1294898905049.JavaMail.root@nextrelease>
Subject: My Site Free Trial - 5 days left
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1733237863.1294898905008"
MyHeaderName: myHeaderValue
Date: Thu, 13 Jan 2011 06:08:25 +0000 (UTC)
------=_Part_0_1733237863.1294898905008
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
**Here Body message**
Take advantage of our best value price annual subscription at only= =EF=BF=BD=EF=BF=BD49 per month. Or our no commitment monthly subscription= at just =EF=BF=BD=EF=BF=BD59 per month. "
Actual out put should be: "Take advantage of our best value price annual subscription at only £49 per month. Or our no commitment monthly subscription at ju开发者_如何学运维st £59 per month.
Here is the code
MimeMessage msg = new MimeMessage(session);
InternetAddress addressTo[] = new InternetAddress[emsg.getRecipeants()
.size()];
int i = 0;
try {
for (String email : emsg.getRecipeants()) {
InternetAddress address = new InternetAddress(email);
addressTo[i] = address;
log.info(acc.getSenderEmailID() + " sending mail To " + email);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
if (emsg.getReplayTO() != null) {
msg.setReplyTo(new InternetAddress[] { new InternetAddress(emsg
.getReplayTO()) });
}
String from = "";
if (!acc.getAccoutName().equals("")) {
from = acc.getAccoutName() + "<" + acc.getSenderEmailID() + ">";
} else {
from = acc.getSenderEmailID();
}
msg.setFrom(new InternetAddress(from));
// MultiPart body part
Multipart multipart = new MimeMultipart();
// This is the message body content part
MimeBodyPart messageContentPart = new MimeBodyPart();
String content = processContent(emsg.content, multipart,
comapanyDBName);
if (emsg.isPlain) {
messageContentPart.setContent(content,
"text/plain; charset=UTF-8");
} else {
messageContentPart.setContent(content,
"text/html; charset=UTF-8");
}
multipart.addBodyPart(messageContentPart);
// This is the template Attachment part
if (emsg.getAttachment() != null) {
for (File file : emsg.getAttachment()) {
MimeBodyPart messageAttachmentBodyPart = new MimeBodyPart();
messageAttachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageAttachmentBodyPart.setDataHandler(new DataHandler(
source));
messageAttachmentBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageAttachmentBodyPart);
}
}
// Put parts in message
msg.setContent(multipart);
// end
// Optional : You can also set your custom headers in the Email
// if
// you
// Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(emsg.subject, "UTF-8");
// msg.setSubject(emsg.subject);//text/plain; charset=ISO-8859-1
// msg.setContent( map.getValue().getContent(), "text/html");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
return msg;
Two possibilities.
First, are you sure that emsg.content
contains non-munged data? If it's corrupt before you get to this code, you're already out of luck.
Second, if the content
is fine, try using MimeBodyPart.setText()
instead:
if (emsg.isPlain) {
messageContentPart.setText(content, "UTF-8", "plain");
} else {
messageContentPart.setText(content, "UTF-8", "html");
}
I think that although your set content-type charset UTF-8 you do not write UTF8 stream. Are you using BufferedWriter? Check whether you pass correct charset to its constructor.
精彩评论