• 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 servlet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends.
I want to write email servlet using JavaMail package.i want to send this email from localhost(which is not a server)to another
computer in a LAN using SMTP.If it is possible how to do that How to do that.Give me in details.
Thanks in advance
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
There are two ways through which you can send a mail ,
1) using the smtp Client
2) using javamail package.
If u wanna make use of java mail package, the code is as follows

import javax.mail.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.internet.*;
public class SendingMail2 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
{
try
{
res.setContentType("text/html");
String from = "uremailid eg sandeep_raparia@yahoo.com ";
String to = "recipents email id eg xyz@pqr.com";
String subject = "the subject u wanna send ";
String cc="cc addresses "
String bcc="Bcc address";
String text="the matter that u wanna send ";
java.util.Properties prop = System.getProperties();
prop.put("mail.smtp.host","mail.yahoo.com");
Session ses = Session.getInstance(prop,null);
MimeMessage message = new MimeMessage(ses);
try
{
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
message.setSubject(subject);
Address[] toAddress = InternetAddress.parse(to);
Address[] cc_address=InternetAddress.parse(cc);
Address[] bcc_address=InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.TO,toAddress);
message.setRecipients(Message.RecipientType.CC,cc_address);
message.setRecipients(Message.RecipientType.BCC,bcc_address);
message.setSentDate(new java.util.Date());
message.setText(text);
Transport.send(message);
}
catch(Exception e)
{
System.out.println("Problem " + e);
}
}
catch(Exception e)
{
}
}
};


------------------
Sandeep Jain
 
Sandeep Jain
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u want to make use of smtp in the servlet

import sum.net.smtp.*;
you can write the following Code in ur service method of your servlet

SmtpClient smClient=new SmtpClient("MmailHostName");
//mailHostName can be like 'mail.webveda.com'
smClient.from("yourEmailAddress");
smClient.to("recipient email address");
PrintStream ps=smClient.startMessage();
ps.println("from "+ "yourEmailAddress");
ps.println("from "+ "recipient email address");
ps.println("write the subject that u wanna write ");
smClient.closeServer();

------------------
Sandeep Jain
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nishant,
Here is the code u asked for
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
public class MailSender extends HttpServlet
{
private final String MAIL_HOST="mail.sigmaonline.net";
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();

String from=req.getParameter("from");
String to=req.getParameter("to");
String subject=req.getParameter("subject");
String text=req.getParameter("text");
Properties prop=System.getProperties();
prop.put("mail.smtp.host",MAIL_HOST);
prop.put("mail.from",from);
Session session=Session.getDefaultInstance(prop,null);
try
{
Message message=new MimeMessage(session);
InternetAddress address[]={new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO,address);
message.setFrom(new InternetAddress(from));
message.setTo(to);
message.setSubject(subject);
message.setContent("text/plain",text);
Transport transport=session.getTransport(address[0]);
transport.addConnectionListener(new ConnectionHandler());
transport.addTransportListener(new TransportHandler());
transport.connect();
transport.sendMessage(message,address);
}
catch(Exception ex)
{
getServletContext().log(ex);
}
}
class ConnectionHandler extends ConnectionAdapter
{
public void opened(ConnectionEvent ce)
{
System.out.println("Connection Opened");
}
public void disconnected(ConnectionEvent ce)
{
System.out.println("Connection disconnected");
}
public void closed(ConnectionEvent ce)
{
System.out.println("Connection closed");
}
}
class TransportHandler extends TransportAdapter
{
public void messageTransferred(TransportEvent te)
{
System.out.println("Message Delivered");
}
public void messagePartiallyTransferred(TransportEvent te)
{
System.out.println("Message Partially Transferred");
}
public void messageNotTransferred(TransportEvent te)
{
System.out.println("Message Not Transferred");
}
}
}
Nishant, i dint tested this code, but i am sure that it works well, if there is any problem with this code please let me know it.
Loke.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lokesh,
I tried ur program to send emails using JavaMail but encountered the following exception:
javax.mail.MessagingException:could not connect to SMTP host :mail.yahoo.com, port:25,
java.net.NoRouteToHostException: Host Unreachable: no further information.
I used javawebserver2.0.
Could u pls. help me on this regard. Is anything wrong in the following mail host:
private final String MAIL_HOST="mail.yahoo.com";
kindly suggest me.
regards,
jayaraman
mailid:jayaraman_g@usa.net

Originally posted by lokesh reddy:
Hi Nishant,
Here is the code u asked for
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
public class MailSender extends HttpServlet
{
private final String MAIL_HOST="mail.sigmaonline.net";
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();

String from=req.getParameter("from");
String to=req.getParameter("to");
String subject=req.getParameter("subject");
String text=req.getParameter("text");
Properties prop=System.getProperties();
prop.put("mail.smtp.host",MAIL_HOST);
prop.put("mail.from",from);
Session session=Session.getDefaultInstance(prop,null);
try
{
Message message=new MimeMessage(session);
InternetAddress address[]={new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO,address);
message.setFrom(new InternetAddress(from));
message.setTo(to);
message.setSubject(subject);
message.setContent("text/plain",text);
Transport transport=session.getTransport(address[0]);
transport.addConnectionListener(new ConnectionHandler());
transport.addTransportListener(new TransportHandler());
transport.connect();
transport.sendMessage(message,address);
}
catch(Exception ex)
{
getServletContext().log(ex);
}
}
class ConnectionHandler extends ConnectionAdapter
{
public void opened(ConnectionEvent ce)
{
System.out.println("Connection Opened");
}
public void disconnected(ConnectionEvent ce)
{
System.out.println("Connection disconnected");
}
public void closed(ConnectionEvent ce)
{
System.out.println("Connection closed");
}
}
class TransportHandler extends TransportAdapter
{
public void messageTransferred(TransportEvent te)
{
System.out.println("Message Delivered");
}
public void messagePartiallyTransferred(TransportEvent te)
{
System.out.println("Message Partially Transferred");
}
public void messageNotTransferred(TransportEvent te)
{
System.out.println("Message Not Transferred");
}
}
}
Nishant, i dint tested this code, but i am sure that it works well, if there is any problem with this code please let me know it.
Loke.


 
jaya raman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,
I tried ur program to send emails using JavaMail but encountered the following exception:
javax.mail.MessagingException:could not connect to SMTP host :mail.yahoo.com, port:25,
java.net.NoRouteToHostException: Host Unreachable: no further information.
I used javawebserver2.0.
Could u pls. help me on this regard. Is anything wrong in the following piece of code(mail host):
prop.put("mail.smtp.host","mail.rediff.com");
kindly suggest me.
rgds,
jayaraman

Originally posted by Sandeep Jain:
Hi ,
There are two ways through which you can send a mail ,
1) using the smtp Client
2) using javamail package.
If u wanna make use of java mail package, the code is as follows

import javax.mail.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.internet.*;
public class SendingMail2 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
{
try
{
res.setContentType("text/html");
String from = "uremailid eg sandeep_raparia@yahoo.com ";
String to = "recipents email id eg xyz@pqr.com";
String subject = "the subject u wanna send ";
String cc="cc addresses "
String bcc="Bcc address";
String text="the matter that u wanna send ";
java.util.Properties prop = System.getProperties();
prop.put("mail.smtp.host","mail.yahoo.com");
Session ses = Session.getInstance(prop,null);
MimeMessage message = new MimeMessage(ses);
try
{
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
message.setSubject(subject);
Address[] toAddress = InternetAddress.parse(to);
Address[] cc_address=InternetAddress.parse(cc);
Address[] bcc_address=InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.TO,toAddress);
message.setRecipients(Message.RecipientType.CC,cc_address);
message.setRecipients(Message.RecipientType.BCC,bcc_address);
message.setSentDate(new java.util.Date());
message.setText(text);
Transport.send(message);
}
catch(Exception e)
{
System.out.println("Problem " + e);
}
}
catch(Exception e)
{
}
}
};


 
jaya raman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,
I tried ur program to send emails using JavaMail but encountered the following exception:
javax.mail.MessagingException:could not connect to SMTP host :mail.yahoo.com, port:25,
java.net.NoRouteToHostException: Host Unreachable: no further information.
I used javawebserver2.0.
Could u pls. help me on this regard. Is anything wrong in the following piece of code(mail host):
prop.put("mail.smtp.host","mail.yahoo.com");
kindly suggest me.
rgds,
jayaraman

Originally posted by Sandeep Jain:
Hi ,
There are two ways through which you can send a mail ,
1) using the smtp Client
2) using javamail package.
If u wanna make use of java mail package, the code is as follows

import javax.mail.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.internet.*;
public class SendingMail2 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
{
try
{
res.setContentType("text/html");
String from = "uremailid eg sandeep_raparia@yahoo.com ";
String to = "recipents email id eg xyz@pqr.com";
String subject = "the subject u wanna send ";
String cc="cc addresses "
String bcc="Bcc address";
String text="the matter that u wanna send ";
java.util.Properties prop = System.getProperties();
prop.put("mail.smtp.host","mail.yahoo.com");
Session ses = Session.getInstance(prop,null);
MimeMessage message = new MimeMessage(ses);
try
{
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
message.setSubject(subject);
Address[] toAddress = InternetAddress.parse(to);
Address[] cc_address=InternetAddress.parse(cc);
Address[] bcc_address=InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.TO,toAddress);
message.setRecipients(Message.RecipientType.CC,cc_address);
message.setRecipients(Message.RecipientType.BCC,bcc_address);
message.setSentDate(new java.util.Date());
message.setText(text);
Transport.send(message);
}
catch(Exception e)
{
System.out.println("Problem " + e);
}
}
catch(Exception e)
{
}
}
};


 
lokesh reddy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jayaram,
I think u r behind a firewall or a proxy server.
Any way, here i would like to provide some alternate ways to run your program with out any errors.
Try to add this code after initializing Properties object.
prop.put("http.proxyHost","YOUR PROXY SERVER NAME");
prop.put("http.proxyPort","PORT NO ON WHICH SERVER LISTENS");
Replace the Proxy Server Name and Port with appropriate strings.
If this too doesn't work, then try to change HOST_NAME.
You can try with the following HOST_NAMES.
1) mail.sigmaonline.net
2) mail.webveda.com
I hope you find this info useful.
Bye.
Loke.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lokesh, Sandeep,Jayaram.
Your explainations are very helpful. Thanx for the same.
Kindly please tell me what is the significance of session object.
Regards
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lokesh,
Thanks for providing an explanation for sending the mail from the machines behind the firewall or proxy.
I tried the same thru JSP.But the mail is not reaching the destination even though I am getting the msg as mail sent.
Any settings r required thru policy tools to enable routing the msg thru proxy settings.
If so, Pls give me the details.
Bye,
Manish
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Loke,
Your coding was really useful to me. But i have one special requirement. Normally in the coding you are pre-fixing the MAIL HOST ADDRESS (e.g. mail.yahoo.com), whereas in my application i am getting the different mail addresses from the user (like yahoo, hotmail, usa.net, rediff etc). If this be case what consideration i should take in refining your code. Kindly advise me in this regard at the earliest.
Tks a lot
Jowsaki

Originally posted by lokesh reddy:
Hi Nishant,
Here is the code u asked for
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
public class MailSender extends HttpServlet
{
private final String MAIL_HOST="mail.sigmaonline.net";
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();

String from=req.getParameter("from");
String to=req.getParameter("to");
String subject=req.getParameter("subject");
String text=req.getParameter("text");
Properties prop=System.getProperties();
prop.put("mail.smtp.host",MAIL_HOST);
prop.put("mail.from",from);
Session session=Session.getDefaultInstance(prop,null);
try
{
Message message=new MimeMessage(session);
InternetAddress address[]={new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO,address);
message.setFrom(new InternetAddress(from));
message.setTo(to);
message.setSubject(subject);
message.setContent("text/plain",text);
Transport transport=session.getTransport(address[0]);
transport.addConnectionListener(new ConnectionHandler());
transport.addTransportListener(new TransportHandler());
transport.connect();
transport.sendMessage(message,address);
}
catch(Exception ex)
{
getServletContext().log(ex);
}
}
class ConnectionHandler extends ConnectionAdapter
{
public void opened(ConnectionEvent ce)
{
System.out.println("Connection Opened");
}
public void disconnected(ConnectionEvent ce)
{
System.out.println("Connection disconnected");
}
public void closed(ConnectionEvent ce)
{
System.out.println("Connection closed");
}
}
class TransportHandler extends TransportAdapter
{
public void messageTransferred(TransportEvent te)
{
System.out.println("Message Delivered");
}
public void messagePartiallyTransferred(TransportEvent te)
{
System.out.println("Message Partially Transferred");
}
public void messageNotTransferred(TransportEvent te)
{
System.out.println("Message Not Transferred");
}
}
}
Nishant, i dint tested this code, but i am sure that it works well, if there is any problem with this code please let me know it.
Loke.


 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"sham",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Loke,
Your coding was really useful to me. But i have one special requirement. Normally in the coding you are pre-fixing the MAIL HOST ADDRESS (e.g. mail.yahoo.com), whereas in my application i am getting the different mail addresses from the user (like yahoo, hotmail, usa.net, rediff etc). If this be case what consideration i should take in refining your code. Kindly advise me in this regard at the earliest.
Tks a lot
Jowsaki

Originally posted by lokesh reddy:
Hi Jayaram,
I think u r behind a firewall or a proxy server.
Any way, here i would like to provide some alternate ways to run your program with out any errors.
Try to add this code after initializing Properties object.
prop.put("http.proxyHost","YOUR PROXY SERVER NAME");
prop.put("http.proxyPort","PORT NO ON WHICH SERVER LISTENS");
Replace the Proxy Server Name and Port with appropriate strings.
If this too doesn't work, then try to change HOST_NAME.
You can try with the following HOST_NAMES.
1) mail.sigmaonline.net
2) mail.webveda.com
I hope you find this info useful.
Bye.
Loke.


 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lokesh and Sandeep,
My mail server needs authentication to send the mail. It generates error 353. I know the valid userID and Password. How do I supply the usernama and password. Any idea.
thanks,
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a setText method in the javax.mail.message class, but no getText. Can anyone tell me why and how to get the message text of an email with the java mail api?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a getContent method (of MimeMessage) which gets you the content of the message as an Object.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic