使用Java中的JavaMail格式化电子邮件

时间:2016-07-05 00:50:45

标签: java email

所以我用Java发了一封电子邮件,但它并没有像我一样格式化电子邮件。而不是这样的格式化电子邮件:

你好约翰,

欢迎来到X。

谢谢,

乔纳森。

它会显示:

John,欢迎来到X.谢谢,Johnathan。

这是代码:

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);
    }












}

}

我正在使用' \ n'字符串中的标签,但它似乎没有在这里工作。我需要在Java中使用html吗?如果是这样,我能得到一些例子吗?

1 个答案:

答案 0 :(得分:1)

您说其内容类型是HTML,因此请使用HTML。新行在HTML中并不重要。试试<p><br/>