aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Problem in sending email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Problem in sending email" Watch "Problem in sending email" New topic
Author

Problem in sending email

Joseph Bashir
Ranch Hand

Joined: Jun 16, 2004
Posts: 41
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

Joined: Aug 16, 2005
Posts: 14347
    
  22

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joseph Bashir
Ranch Hand

Joined: Jun 16, 2004
Posts: 41
- 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

Joined: Oct 10, 2001
Posts: 214
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.


I have no java certifications. This makes me a bad programmer. Ignore my post.
litiecheng
Greenhorn

Joined: Oct 10, 2006
Posts: 1
Try the code

MyAuthenticator myauth = new MyAuthenticator("mail",
"password");
Session session = Session.getInstance(props, myauth);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in sending email