• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Email from Servlet??

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic