This discussion is archived
4 Replies Latest reply: Jul 27, 2012 9:58 AM by DrClap RSS

Java Mail----getting "could not connect smtp server" error---plz help

952086 Newbie
Currently Being Moderated
{index.java

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Java Mail </title>
</head>
<body>
<form action="sendMail.jsp" method="POST">
<table border="0" align="center" cellpadding="5">
<tbody>
<thead><tr> <td colspan="3" align="center">
<b> Send Mail </b> </td> </tr> </thead>
<tr>
<td> To </td> <td> : </td>
<td> <input type="text" name="to" value="" /> </td>
</tr>
<tr>
<td> Subject </td> <td> : </td>
<td> <input type="text" name="subject" value="" /> </td>
</tr>
<tr>
<td> Message </td> <td> : </td>
<td> <textarea name="message" rows="8" cols="30">
</textarea></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="Send Mail" />

<input type="reset" value="Reset" />
<td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
}


{Mail.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package jMail;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
*
* @author samsam
*/
public class Mail {
private String to;
private String from;
private String message;
private String subject;
private String smtpServ;

/**
* @return the to
*/
public String getTo() {
return to;
}

/**
* @param to the to to set
*/
public void setTo(String to) {
this.to = to;
}

/**
* @return the from
*/
public String getFrom() {
return from;
}

/**
* @param from the from to set
*/
public void setFrom(String from) {
this.from = from;
}

/**
* @return the message
*/
public String getMessage() {
return message;
}

/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}

/**
* @return the subject
*/
public String getSubject() {
return subject;
}

/**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}

/**
* @return the smtpServ
*/
public String getSmtpServ() {
return smtpServ;
}

/**
* @param smtpServ the smtpServ to set
*/
public void setSmtpServ(String smtpServ) {
this.smtpServ = smtpServ;
}

public int sendMail(){
try
{
Properties props = new Properties();
// Debug or not true/false
props.put("mail.debug", "false");

// the email account on gmail
props.put("mail.smtp.user", to);

// gmail host details
props.put("mail.smtp.host", smtpServ);
props.put("mail.smtp.port", "993");

// enable authentication of STARTTLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", "993");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);

//-- create connection --
Transport transport = session.getTransport("smtp");
transport.connect();
System.out.println(to+" "+from);

// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(message);
// -- Set some other header information --
//msg.setHeader("MyMail", "Mr. XYZ" );
//msg.setSentDate(new Date());
// -- Send the message --

Transport.send(msg);
System.out.println("Message sent to"+to+" OK." );
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("EXCEPTION "+ex);
return -1;
}
}

// Also include an inner class that is used for authentication purposes

private class SMTPAuthenticator extends javax.mail.Authenticator {
// @Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "samaresh13hit@gmail.com"; // specify your email id here (sender's email id)
String password = "gasam13210311"; // specify your password here
return new PasswordAuthentication(username, password);
}
}

}


}

{sendMail.jsp

<%--
Document : sendMail
Created on : Jul 25, 2012, 3:37:23 PM
Author : samsam
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<jsp:useBean id="mail" scope="session" class="jMail.Mail" />
<jsp:setProperty name="mail" property="to" param="to" />
<jsp:setProperty name="mail" property="from" value="samaresh13hit@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />
<body>
<h1>Hello World!</h1>

<%
String to = mail.getTo();
int result;
result = mail.sendMail();
if(result == 0){
out.println(" Mail Successfully Sent to "+to);
}
else{
out.println(" Mail NOT Sent to "+to);
}
%>

</body>
</html>
}



Getting could not connect to smtpserver in Mail.java..plz help

Edited by: 949083 on Jul 26, 2012 11:02 PM

Edited by: 949083 on Jul 26, 2012 11:46 PM
  • 1. Re: Java Mail----getting "could not connect smtp server" error---plz help
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!

    When you post code use code tags, \
    , before and after the code to preserve formatting.
    
    Thanks for the code but you need to actually ask a question, tell us what you have tried and what error you are getting.
    
    The code you posted won't run; there is no main method so we have no way of knowing what code you are trying execute, in what order, etc.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 3. Re: Java Mail----getting "could not connect smtp server" error---plz help
    805574 Newbie
    Currently Being Moderated
    I suspect it is because smtp.gmail.com isn't listening on that port or a firewall is blocking access.

    I'd recommend you look at Stack Overflow to take a look at a simpler example to figure out what you are doing wrong.
  • 4. Re: Java Mail----getting "could not connect smtp server" error---plz help
    DrClap Expert
    Currently Being Moderated
    Yes, you've clearly grabbed something off the web, and what you have there is out of date. Have a look at the JavaMail FAQ where it explains specifically how to connect to GMail.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points