我正在做的是一个电子邮件模板模块。我可以发送成功,但当我阅读发送的邮件时,中断行消失了,例如,这就是我发送的内容:
"
Dear ñ ,
ABCDEFGHIJKLMNOPQRSTUVWXYZ. abcdefghijklmnopqrstuvwxyz. 1234567890. !@#$%^&*()_. Loren ipsum blahbity blahbity blah blah. WQWERTYUIOP{}ASDFGHJKL"ZXCVBNM<>
I am white spaces
"
这是我收到的:
"Dear ñ , ABCDEFGHIJKLMNOPQRSTUVWXYZ. abcdefghijklmnopqrstuvwxyz. 1234567890. !@#$%^&*()_. Loren ipsum blahbity blahbity blah blah.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<> I am white spaces"
请注意,电子邮件正文来了,然后将数据库存储到pojo中。
发送的测试方法:
@Test
public void sendServiceTest() {
String hostName = null;
// Set up the SMTP server.
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", getHost);
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
msg.setFrom(new InternetAddress(hostName));
InternetAddress manyAddressTo = null;
// InternetAddress[] manyAddressCC = null;
// InternetAddress[] manyAddressBCC = null;
List<EmailRecipient> recipients = emailRecipientService
.selectRecipientByCode("E1");
manyAddressTo = new InternetAddress("sample@sample.com");
msg.setRecipient(Message.RecipientType.TO, manyAddressTo);
msg.setSubject(recipients.get(0).getEmailTemplate().getSubject());
msg.setContent(recipients.get(0).getEmailTemplate().getEmail_body()+"\r\n","text/html; charset=utf-8");
Transport.send(msg);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
发布于 2014-01-01 19:11:03
由于您使用的是html内容,所以需要使用html标记来控制格式设置。如果不愿意使用text/平原而不是text/html,只需插入行间隔和空格就不可能做到这一点。
发布于 2014-01-01 18:15:25
考虑将电子邮件格式化为HTML。这无疑是一种维护格式的方式。此外,您还可以使用公用电子邮件库,它将删除大量需要编写的锅炉板代码。
看看这个
http://commons.apache.org/proper/commons-email/userguide.html
https://stackoverflow.com/questions/20875032
复制相似问题