[英]Formatting an email with JavaMail in Java
So I have sent an email in Java, however It doesn't format the email as I'd like. 所以我已经用Java发送了一封电子邮件,但是它没有按照我的意愿格式化电子邮件。 Instead of a formatted email like this: 而不是像这样的格式化电子邮件:
Hello John, 你好约翰,
Welcome to X. 欢迎来到X。
Thanks, 谢谢,
Johnathan. 约翰娜森。
It would show: 它会显示:
Hello John, Welcome to X. Thanks, Johnathan. 约翰您好,欢迎来到X。谢谢约翰娜森。
This is the code: 这是代码:
public class MailReceipt {
String receipt;
String email;
public MailReceipt(String message, String email) {
this.receipt = message;
this.email = email;
}
public void sendMessage() {
final String username = "abc123@gmail.com";
final String password = "abc123";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email));
message.setSubject("DO NOT REPLY: A receipt regarding your recent purchase.");
message.setContent(receipt, "text/html; charset=utf-8");
Transport.send(message);
System.out.println("The mail was sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
} }
I am using '\\n' tags in the string, however it doesn't seem to work here. 我在字符串中使用了'\\ n'标签,但是在这里似乎不起作用。 Would I need to make use of html in Java? 我需要在Java中使用html吗? If so, could I get some examples? 如果可以,我可以举一些例子吗?
You've said its content-type is HTML, so use HTML. 您已经说过它的内容类型是HTML,所以请使用HTML。 A newline isn't significant in HTML. 换行符在HTML中并不重要。 Try <p>
, or <br/>
. 尝试<p>
或<br/>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.