Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom mail headers using MIME in Java

I want to create a new custom header while sending email, but by using setHeader() and addHeader() methods I am unable to do it.

How can I create a user defined X-"" email header?

like image 715
ganga Avatar asked Sep 20 '12 11:09

ganga


1 Answers

setHeader() works for me. In below case, I set encoding options to mail header.

String mail_body = "<html><head></head><body><h1>Mail Body</h1><b>This is mail body of Test mail.</b></body></html>";
String encodingOptions = "text/html; charset=UTF-8";

MimeMessage message = new MimeMessage(session);
message.setContent("Hello", "text/plain");
message.setSubject(mail_subject);
message.setText(mail_body);
message.setHeader("Content-Type", encodingOptions);
message.setSentDate(new Date());

You can see message header in receiving email with https://support.google.com/mail/bin/answer.py?hl=en&answer=22454

Sample program. Mail.java

package com.test.mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
    private String host = "smtp.gmail.com";

    private String mail_to = "[email protected]";

    private String mail_from = "[email protected]";// using gmail server

    private String mail_subject = "Subject of this test mail";

    private String mail_body = "<html><head></head><body><h1>Mail Body</h1><b>This is mail body of Test mail.</b></body></html>";

    private String personalName = "xxx";

    private String encodingOptions = "text/html; charset=UTF-8";

    public void sendMail() throws SendFailedException {
        try {

            Properties props = new Properties();
            Authenticator auth = new Email_Autherticator();

            //props.put("mail.smtp.host", host);
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "587");
            System.out.println(props);

            Session session = Session.getDefaultInstance(props, auth);

            MimeMessage message = new MimeMessage(session);
            message.setContent("Hello", "text/plain");
            message.setSubject(mail_subject);
            message.setText(mail_body);
            message.setHeader("Content-Type", encodingOptions);
            //message.setHeader(mail_head_name, mail_head_value);
            message.setSentDate(new Date());

            Address address = new InternetAddress(mail_from, personalName);
            message.setFrom(address);

            Address toaddress = new InternetAddress(mail_to);
            message.addRecipient(Message.RecipientType.TO, toaddress);

            System.out.println(message);

            Transport.send(message);
            System.out.println("Send Mail Ok!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // return flag;
    }

    public static void main(String args[]) {
        try {
            Mail mailObj = new Mail();
            mailObj.sendMail();
        } catch (Exception e) {

        }
    }
}

Email_Autherticator.java

package com.test.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Email_Autherticator extends Authenticator {
    String username = "[email protected]";

    String password = "password";

    public Email_Autherticator() {
        super();
    }

    public Email_Autherticator(String user, String pwd) {
        super();
        username = user;
        password = pwd;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}
like image 200
swemon Avatar answered Nov 14 '22 17:11

swemon