Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending Email from Servlet.

 
inhaer Mc
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am quite new with Servlet/JSP. How can i send an email from an servlet? Thank you for your help !
 
Jiang Xiaofeng
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by inhaer Mc:
I am quite new with Servlet/JSP. How can i send an email from an servlet? Thank you for your help !



Use JavaMail: http://java.sun.com/products/javamail/
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"inhaer Mc",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Other APis forum.
 
Seetesh Hindlekar
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Include pop3.jar, activation.jar, Jmail.jar, mail.jar, mailapi.jar files in the classpath.

Include part of the code snippet in ur servlet.

try
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
//props.put("mail.smtp.host", "smtp.jcom.net");
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");


// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
Session mailSession = Session.getDefaultInstance(props, null);

session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.addHeader("MyHeaderName", "myHeaderValue");
msg.setHeader("X-Mailer", "JavaMail API");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
msg.setSentDate(new Date());
Transport.send(msg,addressTo);
}
catch (MessagingException mex) {}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic