Results 1 to 6 of 6
  1. #1
    bhaniel is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default java Mail Problem Please Help

    this is my Code:

    Java Code:
    import java.util.Properties;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMessage.RecipientType;
    
    
    public
    
    class SendMail
    {
    
    	private static String HOST = "smtp.gmail.com";
    	private static String USER = "java.web.programming";//"yourmail.com";
    	private static String PASSWORD = "javaprogramming";
    	private static String PORT = "587";
    	private static String FROM = "mail.com";
    	private static String TO;
    	private static String STARTTLS = "true";
    	private static String AUTH = "true";
    	private static String DEBUG = "true";
    	private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
    	private static String SUBJECT = "Testing JavaMail API";
    	private static String TEXT = "This is a test message from my java application. Just ignore it";
    	
    	public synchronized void send(String to)
    	{
    	
    		TO = to;
    		//Use Properties object to set environment properties
    		Properties props = new Properties();
    		props.put("mail.smtp.host", HOST);
    		props.put("mail.smtp.port", PORT);
    		props.put("mail.smtp.user", USER);
    		props.put("mail.smtp.auth", AUTH);
    		props.put("mail.smtp.starttls.enable", STARTTLS);
    		props.put("mail.smtp.debug", DEBUG);
    		props.put("mail.smtp.socketFactory.port", PORT);
    		props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
    		props.put("mail.smtp.socketFactory.fallback", "false");
    		
    		try
    		{
    		
    			//Obtain the default mail session
    			Session session = Session.getDefaultInstance(props, null);
    			session.setDebug(true);
    			//Construct the mail message
    			MimeMessage message = new MimeMessage(session);
    			message.setText(TEXT);
    			message.setSubject(SUBJECT);
    			message.setFrom(new InternetAddress(FROM));
    			message.addRecipient(RecipientType.TO, new InternetAddress(TO));
    			message.saveChanges();
    			
    			//Use Transport to deliver the message
    			Transport transport = session.getTransport("smtp");
    			transport.connect(HOST, USER, PASSWORD);
    			transport.sendMessage(message, message.getAllRecipients());
    			
    			transport.close();
    			
    		} catch (Exception e){
    		
    			e.printStackTrace();
    		
    		}
    
    	}
    	
    	public static void main(String[] args){
    		(new SendMail()).send("litvak.marina@gmail.com");
    	}
    	
    }



    and this is the problem:

    Java Code:
    DEBUG: setDebug: JavaMail version 1.4.3
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
    javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
      nested exception is:
    	java.net.ConnectException: Connection timed out: connect
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    	at javax.mail.Service.connect(Service.java:291)
    	at javax.mail.Service.connect(Service.java:172)
    	at SendMail.send(SendMail.java:59)
    	at SendMail.main(SendMail.java:73)
    Caused by: java.net.ConnectException: Connection timed out: connect
    	at java.net.PlainSocketImpl.socketConnect(Native Method)
    	at java.net.PlainSocketImpl.doConnect(Unknown Source)
    	at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
    	at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
    	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
    	... 5 more



    i have tried the two ports 465 and 587 both the same eror's what could be the problem?
    Last edited by Fubarable; 05-30-2011 at 12:55 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Could not connect to SMTP host: smtp.gmail.com, port: 587;
    Why do you think the server is listening on that port? And that it is willing to connect with your code?

  4. #4
    bhaniel is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    because it's the gmail confrmation for the host and the port.
    this code is working fine in my friend laptop but in a diffrent nwetwork i dont know why.
    maybe there is somthing that i need to install?

    and by the way i dont have any firewall.

  5. #5
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Quote Originally Posted by bhaniel View Post
    this code is working fine in my friend laptop but in a diffrent nwetwork i dont know why.
    Your ISP may be blocking SMTP traffic.
    Can you send mails through that server using a desktop email client, like
    Outlook, Thunderbird etc?

  6. #6
    bhaniel is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Thx its Helped when i Installed outlook it opended the connection and manged to send mail's.

Similar Threads

  1. Problem while sending a java mail
    By dushyant1244 in forum Advanced Java
    Replies: 15
    Last Post: 03-15-2013, 10:37 PM
  2. Java Mail Api Problem
    By ravjot28 in forum Networking
    Replies: 3
    Last Post: 06-21-2010, 05:19 PM
  3. Java Mail Multipart Problem
    By lowell108 in forum Advanced Java
    Replies: 0
    Last Post: 01-04-2010, 02:36 PM
  4. problem with sending mail usin javaX.mail api
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 11-25-2009, 05:37 AM
  5. Problem in sending mail from java
    By npoorni in forum Advanced Java
    Replies: 1
    Last Post: 06-30-2009, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •