0

I have written a java code to send a mail.

I need to set the Email subject color,ie I need to set the color for the text of method message.setSubject().

Below are my codes :

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();


            }



   }
  }

Can anyone help me to achieve this task.

Thank you

1 Answers1

1

There's no way to do that. The application that displays the message gets to choose how to display the Subject information, including what font and color to use. Unlike the body of the message, there's no way to supply "rich text" or html attributes for the Subject.

Bill Shannon
  • 27,854
  • 5
  • 34
  • 37