This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Email from Servlet??

 
Seema Das
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
sandy gupta
Ranch Hand
Posts: 228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!");
}
*/
}
 
Seema Das
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic