更改字符串颜色 JavaMail

vij*_*jay 3 java colors jakarta-mail

我正在使用 JavaMail 库,我想更改电子邮件的正文、不同颜色的句子?我该怎么做?我的应用程序在 (Swing/JFrame)

Dam*_*ght 5

以 HTML 格式发送电子邮件的示例:http : //www.tutorialspoint.com/java/java_sending_email.htm

Baadshah 建议使用 html 标签在 Content 字符串中添加所有颜色格式。

     message.setContent("<h1>This is actual message</h1>",
                        "text/html" );
Run Code Online (Sandbox Code Playgroud)

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

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

addColor(line1, Color.BLUE);
Run Code Online (Sandbox Code Playgroud)

然后创建一个处理着色 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;
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处检查在 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。我使用了此链接中的解决方案,并设置了 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 = "targetemail@somehost.com";

   // Sender's email ID needs to be mentioned
   String from = "youremail@somehost.com";

   // 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;
}
}
Run Code Online (Sandbox Code Playgroud)

注意:在我的环境中,我必须在运行配置中设置此参数:

-Djava.net.preferIPv4Stack=true

更多关于这里。