Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to send a mail ?  RSS feed

 
lohith micheal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
i am making an application in which i want to send a mail from my Application
to any one in the world.
please help me in solving this?
try to explain me in Steps or get me few sites so that i can go through it?

Regards
Lohith M.K.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The http://faq.javaranch.com/java/JavaEnterpriseEditionFaq has links to several introductory articles and FAQs covering JavaMail, which is the Java API for sending and receiving email.

Since this seems to have nothing to do with Struts, I'll move it to the Other Java APIs forum, where JavaMail is discussed.
 
Lukasz Bajzel
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the code to send mail using Java. Make sure that you know the host/smtp-host name and other parameters.


boolean mailSent = false;
try{

Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", <your_host> ;
props.put("mail.smtp.host", <your_smtp_host> ;
Session sess = Session.getDefaultInstance(props);
Message msg = new MimeMessage(sess);

String toAddr =<comma seperated address>;

msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse(toAddr));
String rplAddr = mailProps.getProperty("reply_address");
String fromPerson = mailProps.getProperty("from_personal");

InternetAddress fromAddr = new InternetAddress(<your_reply_address> ;

msg.setFrom(fromAddr);
msg.setSubject(<subject> ;
Multipart mp = new MimeMultipart();

BodyPart pixPart = new MimeBodyPart();
pixPart.setContent(content, "text/html"); //for sending text/html mail
msg.setContent(mp);
Transport.send(msg);
mailSent = true;
}catch(Throwable t){
mailSent = false;
//handle exception
}
return mailSent;



Hope this helps!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
 
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!