This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in sending email

 
Joseph Bashir
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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?)
 
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • 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.
 
litiecheng
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the code

MyAuthenticator myauth = new MyAuthenticator("mail",
"password");
Session session = Session.getInstance(props, myauth);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic