aspose file tools
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
Author

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.
TIA
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 http://java.sun.com/products/javamail .
The example code needs only a little bit of tweaking to get it to work for you.
Hope that helps,
Kevin
Bipul Bhattacharjee
Greenhorn

Joined: Jul 19, 2001
Posts: 4
Hi,
U have to import javax.mail.*, and javax.mail.internet.And in class path have to add mail.jar and activation.jar.you easily get the javaMail API from ttp://java.sun.com/products/javamail.
It's very simple..But before that u have to configure the SMTP server.
BBipul
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
package com.medscape.prof.etutaf.common;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEMail{
Session session;
public SendEMail(String smtpHost) throws Exception {
java.util.Properties properties = System.getProperties();
System.out.println(smtpHost);
properties.put("mail.smtp.host", 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));
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
System.out.println("before send");
try{
Transport.send(mimeMessage);
}
catch(NoSuchProviderException nse)
{

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

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

Adios
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Email from Servlet??