0
@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 ?

Sandeep
  • 409
  • 4
  • 24
janu
  • 9
  • 1
  • 7
  • 2
    Possible duplicate of [How can I send an email by Java application using GMail, Yahoo, or Hotmail?](http://stackoverflow.com/questions/46663/how-can-i-send-an-email-by-java-application-using-gmail-yahoo-or-hotmail) – Val Bonn Jan 25 '17 at 12:59
  • possible duplicate of [http://stackoverflow.com/questions/386083/must-issue-a-starttls-command-first-sending-email-with-java-and-google-apps] – ANucool Mittal Jan 25 '17 at 13:08
  • @ANucoolMittal ,Val Bonn Thank u – janu Jan 27 '17 at 04:40

0 Answers0