jsp - Error in sending mail in java/servlet -
i trying send email
using java
. face error below
javax.mail.authenticationfailedexception: failed connect, no password specified
why getting error when have passed correct email-id , password authentication?
this code
import java.io.ioexception; import java.net.passwordauthentication; import java.util.properties; import javax.mail.authenticator; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * servlet implementation class testmail */ @webservlet("/testmail") public class testmail extends httpservlet { private static final long serialversionuid = 1l; public testmail() { super(); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { todo(request,response); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { todo(request,response); } private void todo(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub request.setcharacterencoding("utf8"); response.setcharacterencoding("utf8"); properties props = new properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.host","smtp.gmail.com"); props.put("mail.smtp.post","587"); session session = session.getdefaultinstance(props, new authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication( "testing@gmail.com", "testing123"); } }); message message=new mimemessage(session); try { message.setfrom(new internetaddress("testing@gmail.com","hello")); message.setrecipients(message.recipienttype.to,internetaddress.parse("testing@gmail.com")); message.setsubject("testing email"); message.settext("hello testing mail \n \n congrets"); transport.send(message); system.out.println("mail sent successfully"); } catch (messagingexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
the javamail authenticator found in javax.mail package , different java.net class of same name. 2 don't share same authenticator javamail api works java 1.1, didn't have java.net variety.
Comments
Post a Comment