my dog learned polymorphism
The moose likes Servlets and the fly likes Email from Servlet?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Email from Servlet??" Watch "Email from Servlet??" New topic

Email from Servlet??

Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Hi all,
I am writing one servlet on Unix machine. It reads contents of one form and puts it in a formatted fashion in a file.
Can I send this formatted content to someone through email?
I don't know anything about sending email through Java.
Any help will be appreciated.
Kevin Wright
Ranch Hand

Joined: Jul 10, 2001
Posts: 38
It is pretty simple to send a no-frill email via an SMTP Server ( personally haven't tried receiving, etc ), you just need to get the javaMail API from .
The example code needs only a little bit of tweaking to get it to work for you.
Hope that helps,
Bipul Bhattacharjee

Joined: Jul 19, 2001
Posts: 4
U have to import javax.mail.*, and javax.mail.internet.And in class path have to add mail.jar and easily get the javaMail API from ttp://
It's very simple..But before that u have to configure the SMTP server.
Rehan Malik
Ranch Hand

Joined: Jul 09, 2001
Posts: 76
Check out the JavaMail packages that the above people mentioned.
The only issue that you MIGHT run into is that you may not have an SMTP server. There should be a few free ones out there...

Rehan Malik
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Probably this will help
import javax.mail.*;
import javax.mail.internet.*;
public class SendEMail{
Session session;
public SendEMail(String smtpHost) throws Exception {
java.util.Properties properties = System.getProperties();
properties.put("", smtpHost);
session = Session.getInstance(properties,null);
public void mailto(String from, String to, String subject, String message) throws Exception {
System.out.println("inside mailto");

MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
System.out.println("before send");
catch(NoSuchProviderException nse)

catch(Exception e) {
System.out.println("<br><br>" + e);

public static void main(String []args) throws Exception {
System.out.print("Sending mail... ");
SendEMail sm = new SendEMail("");
sm.mailto("from","to", "subject field", "message field"); System.out.println("Sent!");

Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
wow, thanks a lot everybody for all the help and also thanks for the code!!
One question though....
From this server I am already able to send emails using Unix mailx utility. So do I still need to configure?
Rehan Malik
Ranch Hand

Joined: Jul 09, 2001
Posts: 76
I doubt that you'll need to do anything with the unix mail server. As long as it gives you SMTP access, then you're pretty much set.
I agree. Here's the link:
subject: Email from Servlet??
It's not a secret anymore!