Hi i am having the following simple code to send the mail but not working:
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.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sendMail extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
System.out.println("hello pankaj");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress("from@gmail.com");
toAddress = new InternetAddress("to@hotmail.com");
Transport t=mailSession.getTransport("smtps");
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject("testing subject");
simpleMessage.setText("testing body...............");
t.connect("smtp.gmail.com","username","password");
t.send(simpleMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
following is the debug
hello pankaj
1
DEBUG: JavaMail version 1.4.2
DEBUG: URL jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/imap.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/imap.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[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.mail.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.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.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.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: URL jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/smtp.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/E:/LabourApplication/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SendMail/WEB-INF/lib/smtp.jar!/META-INF/javamail.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
220 mx.google.com ESMTP k41sm12117751rvb.57
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO comp10
250-mx.google.com at your service, [123.237.11.41]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5
AUTH LOGIN
334 VXNlcm5hbWU6
cGFua2FqamFpbjE1
334 UGFzc3dvcmQ6
b21zaGFudGk=
235 2.7.0 Accepted
11
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
javax.mail.AuthenticationFailedException: failed to connect
at javax.mail.Service.connect(Service.java:322)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at sendMail.doGet(sendMail.java:62)