javax.mail.AuthenticationFailedException - Pls Help
This are my codes on my jsp page for sending out an email.
<%@ page language ="java" import="java.lang.*,
java.util.*,
java.util.Date,
java.text.DateFormat,
java.text.SimpleDateFormat,
com.bgenie.bgenieData.genieObj.loginObj,
com.bgenie.bgenieData.genieObj.registerObj,
javax.mail.*,
javax.mail.internet.*,
javax.activation.*,
com.bgenie.bgenieData.bean.loginBean" %>
<%
String host ="smtp.gmail.com";
String port ="995";
String to ="chongming@gmail.com";
String from ="chongming.koh@gmail.com";
String subject ="Test from JavaMail.";
String messageText ="Hello from JavaMail!";
String username ="xxx";
String password ="xxx";
boolean sessionDebug =true;
Properties props = System.getProperties();
//props.put("mail.host", host);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.transport.protocol","smtp");
props.put("mail.debug","true");
//props.put("mail.transport.protocol", "smtp");
//Authenticator auth = new Authenticator(username,password);
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(sessionDebug);
Message msg =new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address ={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host +".");
%>
</table>
</body>
</html>
This is the error is shown.
javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.sendMail2_jsp._jspService(org.apache.jsp.sendMail2_jsp:112)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause
javax.mail.AuthenticationFailedException
javax.mail.Service.connect(Service.java:306)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
javax.mail.Transport.send0(Transport.java:168)
javax.mail.Transport.send(Transport.java:98)
org.apache.jsp.sendMail2_jsp._jspService(org.apache.jsp.sendMail2_jsp:96)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
Can someone please guide me on how to solve this issue? thanks
# 1
>root cause
>javax.mail.AuthenticationFailedException
You need to be identified by stmp server :
// Send message with authentication!
Transport tr = session.getTransport("smtp");
tr.connect(MailHost, user, pass);
message.saveChanges(); // don't forget this
tr.sendMessage(message, message.getAllRecipients());
tr.close();
Read JavaMail Faq please!
# 2
Hi, Thanks for replying to my question.I have try to put ur solutions in my jsp code.I get this error:The method getTransport(String) is undefined for the type HttpSessionWhats your advice? Thanks..
# 3
lol... little mistake with import in Jsp : import javax.mail.Session;import javax.servlet.http.HttpSession;javax.mail.Session mailSession = javax.mail.Session.getInstance(props, null);
# 4
Hi,I still get the same error. Anymore advise?
# 5
Variable 'session' in JSP is HttpSession... like request is HttpRequest, out is PrinterWriter etc...
<%@ page language = "java" import="java.lang.*,
java.util.*,
java.util.Date,
java.text.DateFormat,
java.text.SimpleDateFormat,
javax.activation.DataHandler,
javax.activation.DataSource,
javax.activation.FileDataSource,
javax.mail.Address,
javax.mail.BodyPart,
javax.mail.Message,
javax.mail.MessagingException,
javax.mail.Multipart,
javax.mail.Session,
javax.mail.Transport,
javax.mail.internet.AddressException,
javax.mail.internet.InternetAddress,
javax.mail.internet.MimeBodyPart,
javax.mail.internet.MimeMessage,
javax.mail.internet.MimeMultipart" %>
<%
String host = "smtp.gmail.com";
String port = "25";
String to = "xx@xx.fr";
String from = "xx@xx.org";
String subject = "Test from JavaMail.";
String messageText = "Hello from JavaMail!";
String username = "xxxx.xx";
String password = "xxx";
boolean sessionDebug = true;
Properties props = System.getProperties();
// debug transport
props.put("mail.debug", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.starttls.enable","true");
javax.mail.Session mailSession = javax.mail.Session.getInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Send message with authentication!
Transport tr = mailSession.getTransport("smtp");
tr.connect(host, username, password);
msg.saveChanges(); // don't forget this
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>
</table>
</body>
</html>
# 6
Turn on session debugging and post the protocol trace.If JavaMail is actually attempting to login to the server(as verified by examining the protocol trace), then mostlikely you're giving it the wrong username or password.(See the JavaMail FAQ for debugging
# 7
I have turn on the session debugging and these are the error messages
12:19:52,515 INFO [STDOUT] DEBUG: JavaMail version 1.4ea
12:19:52,515 INFO [STDOUT] DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.5.0_05\jre\lib\javamail.pro
viders (The system cannot find the file specified)
12:19:52,515 INFO [STDOUT] DEBUG: !anyLoaded
12:19:52,515 INFO [STDOUT] DEBUG: not loading resource: /META-INF/javamail.providers
12:19:52,515 INFO [STDOUT] DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
12:19:52,515 INFO [STDOUT] DEBUG: Tables of loaded providers
12:19:52,515 INFO [STDOUT] DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provid
er[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail
.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.ma
il.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail
.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Prov
ider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STOR
E,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
12:19:52,515 INFO [STDOUT] DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.ima
p.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems,
Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.m
ail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.su
n.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport
,Sun Microsystems, Inc]}
12:19:52,531 INFO [STDOUT] DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
12:19:52,531 INFO [STDOUT] DEBUG: !anyLoaded
12:19:52,531 INFO [STDOUT] DEBUG: not loading resource: /META-INF/javamail.address.map
12:19:52,531 INFO [STDOUT] DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.5.0_05\jre\lib\javamail.add
ress.map (The system cannot find the file specified)
12:19:52,531 INFO [STDOUT] DEBUG: setDebug: JavaMail version 1.4ea
12:19:52,593 INFO [STDOUT] DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTran
sport,Sun Microsystems, Inc]
12:19:52,609 INFO [STDOUT] DEBUG SMTP: useEhlo true, useAuth true
12:19:52,609 INFO [STDOUT] DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 995, isSSL false
12:20:53,078 INFO [STDOUT] DEBUG SMTP: EOF: [EOF]
12:20:53,078 INFO [STDOUT] DEBUG SMTP: could not connect to host "smtp.gmail.com", port: 995, response: -1
12:20:53,078 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 995, response: -1
javax.servlet.ServletException: Could not connect to SMTP host: smtp.gmail.com, port: 995, response: -1
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.sendMail2_jsp._jspService(org.apache.jsp.sendMail2_jsp:122)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause
May i know whats the problem? Sorry i still new to java and this is my first time using java mail. Please do advise..
# 8
You're not connecting to the server at all. See the connection debuggingtips in the JavaMail FAQ. Most likely some firewall, on your machine or onyour network, is preventing you from connecting.
# 9
Hi, thanks
Now i got this error
16:55:40,156 INFO [STDOUT] DEBUG SMTP: useEhlo true, useAuth true
16:55:40,156 INFO [STDOUT] DEBUG SMTP: trying to connect to host "smtp.bgenie.com", port 25, isSSL
16:55:40,187 INFO [STDOUT] 220 smtp.clients.netdns.net ESMTP
16:55:40,187 INFO [STDOUT] DEBUG SMTP: connected to host "smtp.bgenie.com", port: 25
16:55:40,187 INFO [STDOUT] EHLO user-video
16:55:40,203 INFO [STDOUT] 250-smtp.clients.netdns.net
250-PIPELINING
250-8BITMIME
250-SIZE 0
250 AUTH LOGIN PLAIN CRAM-MD5
16:55:40,203 INFO [STDOUT] DEBUG SMTP: Found extension "PIPELINING", arg ""
16:55:40,203 INFO [STDOUT] DEBUG SMTP: Found extension "8BITMIME", arg ""
16:55:40,203 INFO [STDOUT] DEBUG SMTP: Found extension "SIZE", arg "0"
16:55:40,203 INFO [STDOUT] DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN CRAM-MD5"
16:55:40,203 INFO [STDOUT] DEBUG SMTP: Attempt to authenticate
16:55:40,203 INFO [STDOUT] AUTH LOGIN
16:55:40,218 INFO [STDOUT] 334 VXNlcm5hbWU6
16:55:40,218 INFO [STDOUT] YWRtaW5AYmdlbmllLmNvbQ==
16:55:40,218 INFO [STDOUT] 334 UGFzc3dvcmQ6
16:55:40,218 INFO [STDOUT] YWRtaW4=
16:55:45,234 INFO [STDOUT] 535 authentication failed (#5.7.1)
16:55:45,234 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
javax.mail.AuthenticationFailedException
I try to check the faq for error 535, but i cant find it.
Can anyone help me with this, whats does it means by error 535?
# 10
535SMTP Authentication unsuccessful/Bad username or password
# 11
Those error codes are part of the SMTP protocol and are defined by the SMTP spec.Your server believes you've supplied the wrong username or password.
# 12
Write this Code...
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
class SMTPAuthenticator extends javax.mail.Authenticator
{
private final String SMTP_AUTH_USER = "anupam@etechsupport.net";
private final String SMTP_AUTH_PWD = "supp0rt";
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
Message was edited by:
http://anupampawar.googlepages.com