Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Amitesh kum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "replacing"? JavaMail does not alter the contents of messages it sends.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic