Prerna
Prerna

Reputation: 1235

how to send HTML email

I have to send HTML file via email but not as attachment.

Message simpleMessage = new MimeMessage(mailSession);
try {
   fromAddress = new InternetAddress(from);
   toAddress = new InternetAddress(to);

} catch (AddressException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

try {
    simpleMessage.setFrom(fromAddress);
    simpleMessage.setRecipient(RecipientType.TO, toAddress);

    simpleMessage.setSubject(subject);
    simpleMessage.setText(text);

    Transport.send(simpleMessage);
} catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

It is sending email simply with text message. I want to send HTML content which is stored in another file but not as attachment

Upvotes: 66

Views: 77874

Answers (2)

Bipul Jaishwal
Bipul Jaishwal

Reputation: 323

Here is my sendEmail java program. It's good to use setContent method of Message class object.

message.setSubject(message_sub);
message.setContent(message_text, "text/html; charset=utf-8");

sendEmail.java

package employee;

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

import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class SendEmail {

    public static void sendEmail(String message_text, String send_to,String message_sub ) throws UnsupportedEncodingException {

        
        final String username = "[email protected]";
        final String password = "password";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "us2.smtp.mailhostbox.com"); //replace your host address.
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS
        
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]", "Name from which mail has to be sent"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse(send_to)
            );
            message.setSubject(message_sub);
            message.setContent(message_text, "text/html; charset=utf-8");
            
          

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

Upvotes: 1

dkarp
dkarp

Reputation: 14763

Don't upcast your MimeMessage to Message:

MimeMessage simpleMessage = new MimeMessage(mailSession);

Then, when you want to set the message body, either call

simpleMessage.setText(text, "utf-8", "html");

or call

simpleMessage.setContent(text, "text/html; charset=utf-8");

If you'd rather use a charset other than utf-8, substitute it in the appropriate place.

JavaMail has an extra, useless layer of abstraction that often leaves you holding classes like Multipart, Message, and Address, which all have much less functionality than the real subclasses (MimeMultipart, MimeMessage, and InternetAddress) that are actually getting constructed...

Upvotes: 123

Related Questions