Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble sending an email from my program

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry 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:

    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!)
    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!


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Trouble sending an email from my program

    Check GMail settings for SMTP, maybe it blocks SMTP again.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    Transport.send(message);

    Any one got any ideas?

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Trouble sending an email from my program

    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.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default 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.

Similar Threads

  1. Sending an email of the results from 1 Class to another
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 20th, 2011, 09:56 PM
  2. java email program
    By ashwidjava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2011, 05:10 AM
  3. Trouble with law of Cosine program
    By Delstateprogramer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 07:07 PM
  4. Java problem -- sending email via JSP page
    By java_beginner in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2010, 03:35 AM
  5. sending an email with pdf file everyday
    By pradeepptp in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: June 21st, 2009, 11:49 AM

Tags for this Thread