Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to send email in "HTML" using SMTPMessage class ???  RSS feed

 
Sarin Kuraganti
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone tell me how to send a "HTML formatted" email using com.sun.mail.smtp.SMTPMessage?

There is a setText() method in SMTPMessage class which defaults to the content-type of "plain/text". I want to know whether we can send out emails in HTML format using SMTPMessage class. If so how? Is there a way to set the content-type to HTML?

I need this info very urgently. I would appreciate if anyone can send a quick response to me.

Thanks a million,
regards,
sbk
 
Ganesh Phapale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you just have to specify message's type as "text/HTML" in ur code..

here is the code :
=========================================================

try{
String to ="To";
String from ="From";
String cc ="CC";
String bcc ="BCC";
String subject ="Subject";
String smtp ="your SMTP server IP";
String message="<html> <title> TEST </title> <body> Hello! </body> </html>";
String result="";


Properties props = System.getProperties();

// Puts the SMTP server name to properties object
props.put("mail.smtp.host", smtp);

// Get the default Session using Properties Object
Session session1 = Session.getDefaultInstance(props, null);
// Enable the debug mode
session1.setDebug(true);
// Create a New message
MimeMessage msg = new MimeMessage(session1);
// Set the From address
msg.setFrom(new InternetAddress(from));
// Setting the "To recipients" addresses
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
// Setting the "cc recipients" addresses
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
// Setting the "Bcc recipients" addresses
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc, false));
// Sets the Subject
msg.setSubject(subject);
// set the meaasge in HTML format
msg.setContent(message,"text/html");
// Set the Date: header
msg.setSentDate(new java.util.Date());

// Send the message
Transport.send(msg);
// If here, then message is successfully sent.
// Display Success message
result = result + "Mail sent To: "+ to;
//if cced then, add html for displaying info
if (!cc.equals(""))
result = result +"cced To: "+cc;
//if Bcced then, add html for displaying info
if (!bcc.equals(""))
result = result +"Bcced To: "+bcc ;

result = result+"";
System.out.println(result);

} // END OF TRY
catch (Exception e)
{
// If here, then error in sending Mail. Display Error message.
result=result+"Unable to send your message"+e;
System.out.println(result);
}//end catch block

===================================================================
 
Ramaswamy Narayanan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ganesh

in your code, i have only changed the to,from,cc,bcc,subject.
but i got the following error message.

DEBUG: setDebug: JavaMail version 1.3.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "your SMTP server IP", port 25, isSSL fals
e
Unable to send your messagejavax.mail.MessagingException: Unknown SMTP host: you
r SMTP server IP;
nested exception is:
java.net.UnknownHostException: your SMTP server IP

What is the problem?
 
Ganesh Phapale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

In the Code that I have given to you,
you have to specify ur SMTP server's IP address so as to get it working for sending mails.
for that you can contact ur server Administrator.

that's it!!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!