Send Mail through Jsp page.
- by sourabhtaletiya
hi friends ,i have tried alot to send mail via jsp page but i am not succeded.
A error is coming
javax.servlet.ServletException: 530
5.7.0 Must issue a STARTTLS command first. x1sm5029316wbx.19
<html>
<head>
<title>JSP JavaMail Example </title>
</head>
<body>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.starttls.required","true");
String host = "smtp.gmail.com";
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
boolean sessionDebug = false;
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port","25");
props.put("mail.smtp.starttls.enable","true");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
props.put("mail.smtp.starttls.enable","true");
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);
props.put("mail.smtp.starttls.enable","true");
Transport tr = mailSession.getTransport("smtp");
tr.connect(host, "sourabh.web7", "june251989");
msg.saveChanges(); // don't forget this
props.put("mail.smtp.starttls.enable","true");
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
// Transport.send(msg);
/*
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");*/
%>
</table>
</body>
</html>