2013-04-08 71 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";