File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes Sending Email via java servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending Email via java servlet" Watch "Sending Email via java servlet" New topic

Sending Email via java servlet

haifa tl

Joined: Apr 14, 2007
Posts: 3

I am looking for help : I developed a java code for sending an automatic email to users when they register.

My code is working with but not with yahoo !!
can any body help me to fix the cause ?

here is my code

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();

* just for debug

//String host = "";
String d_email = "",
d_password = "pwd",
d_host = "",
d_port = "465",
m_to = "",
m_subject = "Registration Confirmation ",
m_text = "You have been registered -- If you want to receive updates via email or sms please Login";

Properties props = new Properties();
props.put("mail.smtp.user", "my-email");
props.put("", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
catch (Exception mex)
private class SMTPAuthenticator extends javax.mail.Authenticator
public PasswordAuthentication getPasswordAuthentication()
return new PasswordAuthentication("", "pwd");

static public String stack2string(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
return "------\r\n" + sw.toString() + "------\r\n";
catch(Exception e2) {
return "bad stack2string";

And I am sure about mu username and password but when I run I always have this error:

Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException

please help
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Is the account you're using allowed to make inbound SMTP connections to Yahoo? I understand you have to have a paid account for that to work; free web accounts won't work.
haifa tl

Joined: Apr 14, 2007
Posts: 3

The account I am using is my yahoo mail account.
please explain more , I diden't get you
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Yes, but is it a paid account or a free account? The free acsount allows web access only, while the paid one can be accessed by SMTP as well.
I agree. Here's the link:
subject: Sending Email via java servlet
It's not a secret anymore!