i want to generate automatic emails.
i got the following code by google.
i also added the mail.jar file into lib of web inf.
wen i execute this code i get exceptions
"Could not connect to SMTP host: smtp.gmail.com, port: 25 (javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?)"
and
"Unrecognized SSL message, plaintext connection?"
is there anything wrong in the code or should i add any resources?
should i add some coding in web.xml?
String host="smtp.gmail.com", user="uname", pass="pwd";
//host = smtp_server; //"smtp.gmail.com"; user = jsp_email; //"YourEmailId@gmail.com" // email id to send the emails
//pass = jsp_email_pw; //Your gmail password
String SSL_FACTORY ="javax.net.ssl.SSLSocketFactory";
String to = receiver;// out going email id
String from =Sender; //Email id of the recipient
String subject ="test";
String state="";
String messageText ="hi";
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg =new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText,"text/html"); // use setText if you want to send text
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
try
{
transport.sendMessage(msg, msg.getAllRecipients());
response.sendRedirect(state);
//WasEmailSent = true; // assume it was sent
}
catch
(Exception err) {
//WasEmailSent = false; // assume it's a fail
out.println(
"Error"+err.getMessage());
}
transport.close();
please help me
thanks