How to add header in smtp javax.mail

To add a header in SMTP using JavaMail, you can use the setHeader() method of the MimeMessage class. Here's an example:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

// Create a new MimeMessage
MimeMessage msg = new MimeMessage(session);

// Set the sender and recipient
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));

// Add a header
msg.setHeader("Subject", "Hello World!");
msg.setHeader("From", "John Doe <[email protected]>");
msg.setHeader("To", "Jane Doe <[email protected]>");
msg.setHeader("CC", "[email protected]");
msg.setHeader("BCC", "[email protected]");

// Add the message body
msg.setText("Hello World!");

// Send the message
Transport.send(msg);

In this example, we create a new MimeMessage object and set the sender and recipient using the setFrom() and setRecipient() methods. We then add several headers using the setHeader() method, including the subject, from, to, CC, and BCC headers. Finally, we add the message body using the setText() method and send the message using the Transport.send() method.

You can also use the addHeader() method to add multiple headers with the same name. For example:

msg.addHeader("X-Priority", "High");
msg.addHeader("X-Priority", "Urgent");

This will add two headers with the name "X-Priority" and values "High" and "Urgent", respectively.

Note that the setHeader() method will overwrite any existing header with the same name, while the addHeader() method will add a new header with the same name.