Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Problem in sending email

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my application when a user registers, after a successful registration the user is sent an email containing a link for the activation of account. This application runs quite well on my local machine. But after uploading it to the server, the user completes his registration successfully, but when it comes to sending an email to the user, the following exception is thrown. Can somebody please help me and tell me whats wrong going on here.


Code:
------------------
Properties props = new Properties();
props.put(KEY1, SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port","25");


Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// set the From and To address
InternetAddress addressFrom = new InternetAddress(FROM);
msg.setFrom(addressFrom);

// gets recipients email address
InternetAddress addressTo = new InternetAddress(emailTo);

msg.setRecipient(Message.RecipientType.TO, addressTo);

// Set the Subject
msg.setSubject(subject);

msg.setSentDate(new Date());

// gets file contents as a string
String filecontents = fileContents(textfilename );

//Set the Content Type
msg.setContent(filecontents, "text/html");

// send the message
Transport.send(msg);
-------------------------------------------------------------

Exception:
------------
javax.mail.MessagingException: Could not connect to SMTP host: mail.myserver.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
at javax.mail.Service.connect(Service.java:255)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)........
..................
..................
..................


Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could not connect to SMTP host: mail.myserver.com, port: 25; nested exception is:
java.net.ConnectException: Connection timed out


The error message obviously tells you that the program failed to connect to the mailserver with the hostname mail.myserver.com

- Is mail.myserver.com really the correct hostname for your mailserver?
- Is the mailserver software running on the server?
- Is there a firewall that blocks port 25 between the computer you are running your program on and the mailserver?
 
Joseph Bashir
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- Is mail.myserver.com really the correct hostname for your mailserver? (YES)
- Is the mailserver software running on the server? (YES)
- Is there a firewall that blocks port 25 between the computer you are running your program on and the mailserver? (I am not sure about this one. How would I know this? and how to solve this problem?)
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried using telnet to that server port 25, that would be the quickest way.



Now, there's clearly a firewall or something blocking you in the way, but the question is, is it on the server where your WAR is (e.g if that server is a Linux box, then it could be running iptables and blocking all outbound on port 25, or there's a hardware firewall connected to the network where the server sits blocking port 25.
Neither question can be answered by any of us, unfortunately. You'll have to deal with the admin of your server.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the code

MyAuthenticator myauth = new MyAuthenticator("mail",
"password");
Session session = Session.getInstance(props, myauth);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic