我写了一个java代码来发送邮件。
我需要设置电子邮件主题颜色,即我需要为方法message.setSubject()的文本设置颜色。
以下是我的代码:
package comparexmlf1;
import comparexmlf1.validatexml;
import comparexmlf1.CarParser1;
import comparexmlf1.OrderParser2;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;
import javax.lang.model.element.Element;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.text.html.MinimalHTMLWriter;
import javax.xml.soap.MimeHeader;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
public class mailer {
static void sendmail() throws IOException,
MessagingException,AddressException
{
String to1=CarParser1.to1;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy
HH:mm:ss");
Calendar cal = Calendar.getInstance();
String to2 = CarParser1.to2;
String to3= CarParser1.to3;
String to4=CarParser1.to4;
String from = CarParser1.from;
String host = CarParser1.host;
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
int m_toterr,m_totwarn;
String getfilepath="";
String filenamechange="D:/newlog
/"+CarParser1.si_orderid+"_log.txt";
System.out.println("New File Path for mail:"+filenamechange);
String pathLogFile = filenamechange;
Enumeration enumeration =
CarParser1.logger.getRootLogger().getAllAppenders();
try {
m_toterr=validatexml.Total_err;
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to1));
message.setSubject("<html><head></head><body><h1><p
style=color:red> CAR Validation Report at :
"+dateFormat.format(cal.getTime())+" for the Order ID :
"+CarParser1.si_orderid+"</p></h1></body><html>","text/html"
);
StringBuffer sb = new StringBuffer();
FileInputStream fstream = new
FileInputStream(pathLogFile);
BufferedReader br = new BufferedReader(new
InputStreamReader(fstream));
String singleLine;
while ((singleLine = br.readLine()) != null)
{
sb.append(singleLine + "<br>");
}
br.close();
String allLines = sb.toString();
String allLines_html=" <html><head><title></title>
</head>"
+ "<body style=background-
color:skyblue;>"+allLines+"</body ></html>";
message.setContent(allLines_html, "text/html;
charset=ISO-8859-1");
Transport.send(message);
System.out.println("Email Sent successfully....");
CarParser1.logger.info("Email Sent Successfully...");
System.out.println();
}
catch (MessagingException mex)
{
System.out.println("Invalid Email Address.please provide
a valid email id to send with");
mex.printStackTrace();
}
}
}
任何人都可以帮助我完成这项任务。
谢谢
答案 0 :(得分:1)
没有办法做到这一点。显示消息的应用程序可以选择如何显示主题信息,包括要使用的字体和颜色。与邮件正文不同,没有办法为主题提供“富文本”或html属性。