2016-07-05 57 views
0

所以我用Java發送了一封電子郵件,但它並沒有按照我的意願格式化電子郵件。取而代之的是格式的電子郵件是這樣的:在Java中使用JavaMail格式化電子郵件

你好,約翰,

歡迎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 = "[email protected]"; 
    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

你說過它的內容類型是HTML,所以使用HTML。換行符在HTML中不重要。嘗試<p><br/>

+0

它的工作,非常感謝。 – bedtime21