aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes JAVA mail API : HTML mails are sent with html tags. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JAVA mail API : HTML mails are sent with html tags." Watch "JAVA mail API : HTML mails are sent with html tags." New topic
Author

JAVA mail API : HTML mails are sent with html tags.

Amitesh kum
Greenhorn

Joined: Jun 06, 2011
Posts: 4
Hi All,

I am bit new to Java mail API. I was trying to send a html mail with below code snippet, But always it is sending me mail as it is(with html tags). It is not replacing html tags.

public static void main(String[] args) throws Exception{

System.out.println("Sending mail...");
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtpport");

Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setContent("<h1>Hello World</h1>", "text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("amitesh.kumar@gmail.com"));
message.setSentDate(new Date());
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}

Please suggest me something on this. Am i missing something???

Thanks
Amitesh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
What do you mean by "replacing"? JavaMail does not alter the contents of messages it sends.


Ping & DNS - my free Android networking tools app
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

You must set proper content type (in this case "text/html") to see html in email message. I belive that by default this content type is "text/plain".
Amitesh kum
Greenhorn

Joined: Jun 06, 2011
Posts: 4
Ulf Dittmer wrote:What do you mean by "replacing"? JavaMail does not alter the contents of messages it sends.



Like ,
message.setContent("<h1>Hello World</h1>", "text/html");

i am setting content as <h1>Hello World</h1>. Currently mail is cumming as it is "<h1>Hello World</h1>"
It should format the text("Hello world") in heading1 and send as mail.
Amitesh kum
Greenhorn

Joined: Jun 06, 2011
Posts: 4
Zandis Murāns wrote:You must set proper content type (in this case "text/html") to see html in email message. I belive that by default this content type is "text/plain".



Please see the code snippet :
message.setContent("<h1>Hello World</h1>", "text/html");

But still am getting mail with html tag as <h1>Hello World</h1>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Amitesh kum wrote:i am setting content as <h1>Hello World</h1>. Currently mail is cumming as it is "<h1>Hello World</h1>"
It should format the text("Hello world") in heading1 and send as mail.



Again:
JavaMail does not alter the contents of messages it sends.

Any formatting would happen in the mail client; make sure yours knows how to handle HTML mails and is set to do so.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAVA mail API : HTML mails are sent with html tags.