65

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

Michael Schmidt
  • 9,090
  • 13
  • 56
  • 80
Prerna
  • 1,235
  • 4
  • 17
  • 22

2 Answers2

121

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...

dkarp
  • 14,483
  • 6
  • 58
  • 65
  • I'm using the code in question with `simpleMessage.setContent(text, "text/html; charset=utf-8");` to set the content but when doing it, every where in content that I have a question mark `=` it is converted to `=3D` which prevents HTML properties to be set correctly, any idea why? – mohamnag Sep 21 '15 at 13:53
  • 1
    @mohamnag You'll probably notice that the resulting message has a header reading `Content-Transfer-Encoding: quoted-printable`. (This is likely because you're including non-ASCII text in your HTML part and email doesn't like non-ASCII content.) The recipient of the message will need to transfer-decode the message content to convert those `=3D`s back to `=`s. Try using `MimeMessage.getInputStream` instead of `MimeMessage.getContentStream` on the recipient side. – dkarp Sep 21 '15 at 20:59
  • @dkrap thanks for mentioning it, I was ignoring transfer encoding and resulted HTML was not showing up correctly. – mohamnag Sep 22 '15 at 14:37
0

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 = "hello@xyz.com";
        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("sender@xyz.com", "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();
        }
    }

}
Bipul Jaishwal
  • 273
  • 2
  • 15