@WebServlet("/send_email")
public class send_email extends HttpServlet {
private static final long serialVersionUID = 1L;`
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String to=request.getParameter("email");
out.println("Mail to be sent:"+to);
String host="localhost";
Properties props=new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.host", "smtp.gmail.com");
//props.put("mail.smtp.socketFactory.port", "587");
//props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
//props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable","true");
/*Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("<username>","<password>");
}
});
*/
Session session = Session.getDefaultInstance(props);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("<email>"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");
Transport.send(message);
System.out.println("message sent successfully");
}
catch (MessagingException e)
{
throw new RuntimeException(e);
}
}
}
error:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. n86sm977027pfb.45 - gsmtp`
Can you pls help me out? problem with SMTP I dont know the procedure.. I have googled and tried all procedures.. Can anyone explain step - step ?