Trouble sending an email from my program
Hi,
I have a program which until it seems today was working fine! It would send an email from my gmail account to another email address, perfect! All of a sudden though its stopped working and just times out with the below error message:
Quote:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at remindersystem.Email.main(Email.java:44)
at remindersystem.Main.main(Main.java:31)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at remindersystem.Email.main(Email.java:38)
... 1 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(Socke tFetcher.java:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFe tcher.java:231)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1900)
My code is as follows (obviously i've removed my real email addresses here and password!)
Code :
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Email {
public static void main(String emailtitle, String emailmsg) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myemailgmail.com","mypassword");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemail@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("sendingto@hotmail.ac.uk"));
message.setSubject(emailtitle);
message.setText(emailmsg);
Transport.send(message);
System.out.println("Done");
}
catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Thanks for any help in advance!
Re: Trouble sending an email from my program
Check GMail settings for SMTP, maybe it blocks SMTP again.
Re: Trouble sending an email from my program
umm im not really sure where i'd go to start off with! if it helps, the problem is with the
Any one got any ideas?
Re: Trouble sending an email from my program
Quote:
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
Can not connect to SMTP host. Either you are behind the firewall or security restrictions on your system or the port you are communicating to, is not helping you to do so.
Re: Trouble sending an email from my program
Hmm its a strange problem as I've disabled my firewall but its still not working, maybe I should try this on another network as I'm currently at uni..as far as anyone can see though, is the code OK?
Re: Trouble sending an email from my program
The code is OK. Sometimes GMail considers your sending action as spam activity so it blocks SMTP. That's why I recommend you to check SMTP settings in Gmail.