Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

sending Mail Exception

891682Jul 11 2012 — edited Jul 11 2012
Hi,
I am using Jdeveloper11.1.1.2.0 and OS is windows7.Below java code In Windows7 its working and got mail but same code windows server2003 I am getting error is

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
java.net.ConnectException: Connection refused: connect


package ro.gebs;
import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailThroughJava {
private static final String emailMsgTxt = "Welcome to sending mail";
private static final String emailSubjectTxt = "A test mail from Anup";
private static final String emailFromAddress = "test@gmail.com";
private static final String[] sendTo = { "abc@gmail.com" };

public static void main(String args[]) throws Exception {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SendMailThroughJava sendMailThroughJava = new SendMailThroughJava();
sendMailThroughJava.sendSSLMessage(sendTo, emailSubjectTxt,
emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully sent mail to all Users");
}

/**
* @param recipients
* @param subject
* @param message
* @param from
* @throws MessagingException
*/
public void sendSSLMessage(String recipients[], String subject,
String message, String from) throws MessagingException {
boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", 587);//
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");

props.put("mail.smtp.ssl.trust", "*");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your Gmail Id",
"Your Gmail Pwd");

}
});


session.setDebug(debug);

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
}


Note --> i have tried port number is 465/587/25 also but not working on windows server 2003.

So please help me..

Thanks
Anup

Edited by: 888679 on Jul 11, 2012 4:33 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2012
Added on Jul 11 2012
6 comments
228 views