This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

===================================================================
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!

 
It's a tiny ad only because the water is so cold.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic