关于java:更改字符串颜色JavaMail |
您所在的位置:网站首页 › java改变字体颜色 › 关于java:更改字符串颜色JavaMail |
我正在使用JavaMail库,我想更改电子邮件的正文,不同颜色的句子吗? 我该怎么做? 我的应用程序在(Swing / JFrame)中 以HTML格式发送电子邮件的示例:http://www.tutorialspoint.com/java/java_sending_email.htm Baadshah的建议是使用html标签在Content字符串中添加所有颜色格式。 12 message.setContent("This is actual message", "text/html" );您可以以编程方式构造包含正文消息的字符串。 123String line1 ="This is the first line in the body. We want it to be blue." addColor(line1, Color.BLUE);然后创建用于处理html着色的方法: 12345public static String addColor(String msg, Color color) { String hexColor = String.format("#%06X", (0xFFFFFF & color.getRGB())); String colorMsg ="" + msg +""; 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 +红色+蓝色+绿色),而HTML仅希望不带alpha的RGB。我从此链接使用了解决方案,并设置了SSCCE: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import 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 +="" + addColor("This line is blue.", Color.BLUE); bodyText +="" + 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 ="" + msg +""; return colorMsg; } }注意: 在我的环境中,我必须在"运行配置"中设置此参数: -Djava.net.preferIPv4Stack = true 在这里更多。 相关讨论 Integer.ToHexString(color)显示错误,并且显示" colorMsg"而不是设置colorMsg的格式,将整个句子显示在电子邮件中" 对于那个很抱歉。 您想要Integer.ToHexString(Color.getRGB()) 对我有用。 不工作。它只是CSS。 与JAVA无关。浏览器检测到您正在email中发送的HTML内容。 例如 1Dear user 相关讨论 您能解释一下吗,因为我对CSS和html不了解,谢谢。您必须以HTML格式发送邮件才能更改文本颜色。 请参阅JavaMail常见问题解答。 对我来说,这完美无瑕,值得一试: 12String htmlText2 ="Jon Targaryen ";或者如果您想使用十六进制颜色: 12String htmlText2 ="Jon Targaryen ";您可以添加更多属性,例如标题或粗体: 12345String htmlText2 ="Jon Targaryen "; String htmlText2 ="Jon Targaryen "; |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |