2013-04-08 141 views
2

我正在使用JavaMail庫,我想更改電子郵件的正文,不同顏色的句子?我該怎麼做?我的應用程序是在(Swing/JFrame)更改字符串顏色JavaMail

回答

3

發送電子郵件作爲HTML的一個例子:http://www.tutorialspoint.com/java/java_sending_email.htm

什麼Baadshah是在暗示是增加你所有的顏色格式的使用html標籤內容字符串內。

 message.setContent("<h1>This is actual message</h1>", 
         "text/html"); 

您可以以編程方式構造包含正文消息的字符串。

String line1 = "This is the first line in the body. We want it to be blue." 

addColor(line1, Color.BLUE); 

然後用於處理着色HTML創建一個方法:

public static String addColor(String msg, Color color) { 
    String hexColor = String.format("#%06X", (0xFFFFFF & color.getRGB())); 
    String colorMsg = "<FONT COLOR=\"#" + hexColor + "\">" + msg + "</FONT>"; 
    return colorMsg; 
} 

您可以檢查HTML這裏着色不同的方式:http://www.htmlgoodies.com/tutorials/colors/article.php/3479011/How-To-Change-Text-Color-Using-HTML-and-CSS.htm。這包括舊的做法,如使用FONT(如上面的示例)或使用CSS做現代的方式。

編輯:toHexString返回一個8個字符的十六進制代碼(alpha + red + blue + green),而HTML只需要沒有alpha的RGB。我使用的解決方案,從this link,並設置一個SSCCE:

import java.awt.Color; 
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class EmailTestHTML 
{ 
public static void main(String [] args) 
{ 

    // Recipient's email ID needs to be mentioned. 
    String to = "[email protected]"; 

    // Sender's email ID needs to be mentioned 
    String from = "[email protected]"; 

    // Assuming you are sending email from localhost 
    String host = "putYourSMTPHostHere"; 

    // Get system properties 
    Properties properties = System.getProperties(); 

    // Setup mail server 
    properties.setProperty("mail.smtp.host", host); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(properties); 

    // String with body Text 
    String bodyText = addColor("This line is red.", Color.RED); 
    bodyText += "<br>" + addColor("This line is blue.", Color.BLUE); 
    bodyText += "<br>" + addColor("This line is black.", Color.BLACK); 

    System.out.println(bodyText); 

    try{ 
     // Create a default MimeMessage object. 
     MimeMessage message = new MimeMessage(session); 

     // Set From: header field of the header. 
     message.setFrom(new InternetAddress(from)); 

     // Set To: header field of the header. 
     message.addRecipient(Message.RecipientType.TO, 
           new InternetAddress(to)); 

     // Set Subject: header field 
     message.setSubject("This is the Subject Line!"); 

     // Send the actual HTML message, as big as you like 
     message.setContent(bodyText, 
         "text/html"); 

     // Send message 
     Transport.send(message); 
     System.out.println("Sent message successfully...."); 
    }catch (MessagingException mex) { 
     mex.printStackTrace(); 
    } 
} 

public static String addColor(String msg, Color color) { 
    String hexColor = String.format("#%06X", (0xFFFFFF & color.getRGB())); 
    String colorMsg = "<FONT COLOR=\"#" + hexColor + "\">" + msg + "</FONT>"; 
    return colorMsg; 
} 
} 

注: 在我的環境我必須設置這個參數在運行配置:

-Djava.net.preferIPv4Stack =真

更多對here.

+0

Integer.ToHexString(color)顯示錯誤和「colorMsg」,而不是格式化colorMsg,總的語句打印在電子郵件「 vijay 2013-04-09 13:40:10

+0

對不起。你想要Integer.ToHexString(Color.getRGB()) – Damienknight 2013-04-09 15:26:59

2

它只是css。

JAVA無關。瀏覽器檢測到您發送的HTML內容爲email

例如

<div style="font-size:14px">Dear user</div> 
+0

你能解釋一下,因爲我不知道CSS和HTML的想法,謝謝。 – vijay 2013-04-08 15:26:04

+0

http://www.codecademy.com/en/tracks/web – Damienknight 2015-06-17 19:35:32

1

你必須發送HTML格式的郵件能夠改變文字顏色。

查看JavaMail FAQ

0

對於我這個工作得十分完美,值得一試:

String htmlText2 = "<font color=red>Jon Targaryen</font>\n"; 

,或者如果你想使用十六進制顏色:

String htmlText2 = "<font color=#93cff2>Jon Targaryen</font>\n"; 

您可以添加更多的屬性,如標題或粗體:

String htmlText2 = "<H1><font color=red>Jon Targaryen</font></H1>\n"; 

String htmlText2 = "<b><H1><font color=red>Jon Targaryen</font></H1></b>\n";