Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

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!

Unable to connect to smtp server & send mail using javamail.

Sanket.BhaleraoDec 20 2013 — edited Dec 23 2013

Hi all,

i am struggling with sending email with javamail api.

i have already visited some forums but not able to resolve the issue so posting it here.

details of the situation are as follows :

1. SMTP server - to which we are able to connect with telnet and able to send emails this work even from the firewall.

2.tried to disable the IPV6 from my windows 7 machine after reading something - java tries to connect to smtp with IPV6 and it must be disable in order for javamail to work.

3.tried to execute the same code from a linux environment. from this environment we are also able to connect to smtp server with telnet and send mail

the code i am trying is below.

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

public class SendEmail

{

public static void main(String [] args){

String to = "validid@something.com";//change accordingly

String from = "valid2@something.com";//change accordingly

String host = "smtp.somthing.com";//or IP address

//Get the session object

Properties properties = System.getProperties();

properties.setProperty("mail.debug", "true");

properties.setProperty("mail.smtp.host", host);

properties.put("mail.smtp.starttls.enable", "false");

//properties.put("mail.smtp.port", "25"); //commented as its by default takes 25

Session session = Session.getDefaultInstance(properties);

////compose the message

try{

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

message.setSubject("Ping");

message.setText("Hello, this is example of sending email  ");

// Send message

Transport.send(message);

System.out.println("message sent successfully....");

}catch (MessagingException mex) {mex.printStackTrace();}

}

}

errors i am getting are

DEBUG: JavaMail version 1.4.7

DEBUG: successfully loaded resource: /META-INF/javamail.default.providers

DEBUG: Tables of loaded providers

DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}

DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}

DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG SMTP: useEhlo true, useAuth false

DEBUG SMTP: trying to connect to host "smtp.something.com", port 25, isSSL false

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.something.com, port: 25;

  nested exception is:

    java.net.ConnectException: Connection timed out: connect

    at emailer.SendMail.main(SendMail.java:64)

Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.something.com, port: 25;

  nested exception is:

    java.net.ConnectException: Connection timed out: connect

    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)

    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)

    at javax.mail.Service.connect(Service.java:295)

    at javax.mail.Service.connect(Service.java:176)

    at emailer.SendMail.main(SendMail.java:58)

Caused by: java.net.ConnectException: Connection timed out: connect

    at java.net.DualStackPlainSocketImpl.connect0(Native Method)

    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)

    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)

    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)

    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)

    at java.net.PlainSocketImpl.connect(Unknown Source)

    at java.net.SocksSocketImpl.connect(Unknown Source)

    at java.net.Socket.connect(Unknown Source)

    at java.net.Socket.connect(Unknown Source)

    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)

    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)

    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)

    ... 4 more

has anyone had some issue like this?

suggestions are welcome..

This post has been answered by Sanket.Bhalerao on Dec 23 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 20 2014
Added on Dec 20 2013
5 comments
9,534 views