大家好,
我正在使用 javax.mail 包 MINEMESSAGE,MimeMultipart 类发送邮件,但即使我提到类型 utf-8,unicode 字符在正文中也不起作用。就像英镑符号不起作用一样。请帮忙该怎么办。这是我的消息标题
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 just £59 per month.
这是代码
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;
最佳答案
两种可能性。
首先,您确定 emsg.content
包含未修改的数据吗?如果在你看到这段代码之前它就已经损坏了,那么你就运气不好了。
其次,如果内容
没问题,请尝试使用MimeBodyPart.setText()
:
if (emsg.isPlain) {
messageContentPart.setText(content, "UTF-8", "plain");
} else {
messageContentPart.setText(content, "UTF-8", "html");
}
关于java - 井号在使用 java.mail 包的邮件内容中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4677440/