12

MyCode:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
    final String username = "[email protected]";
    final String password = "myemailpassword";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"+
 "\n\n No spam to my email,please!"); 
        Transport.send(message);
        System.out.println("Done");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
}

My Error:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException:
Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at Mail.SendMailTLS.main(SendMailTLS.java:56)
Caused by: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1970)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
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 Mail.SendMailTLS.main(SendMailTLS.java:51)
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 7 more
Java Result: 1

I am having the Local proxy as 172.17.0.4:8080 with username as :user1, password as: user2
Solutions pls??

3
  • 2
    What the..? I wondered why you had added both Swing and SWT tags to this. Then when checking the imports at the top of the source, I wondered why you added either. Care to clarify? Commented Apr 10, 2012 at 9:13
  • 1
    try pinging the server and check if you are able to access gmail from Proxy. Commented Apr 10, 2012 at 9:35
  • I had similar issue with pop.gmail.com - code worked, then after some time exception appeared for a few tries, then it worked again. So the fault may be not on your side :) Commented Jul 28, 2016 at 11:34

6 Answers 6

16

Try this server:

smtp.googlemail.com

Sign up to request clarification or add additional context in comments.

Comments

12

If you are under a proxy I think you can't use Java Mail.

From : oracle.com

Q: How do I configure JavaMail to work through my proxy server?

A: JavaMail does not currently support accessing mail servers through a web proxy server. One of the major reasons for using a proxy server is to allow HTTP requests from within a corporate network to pass through a corporate firewall. The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. In addition, a mail server inside the corporate network will perform a similar function for email, accepting messages via SMTP and forwarding them to their ultimate destination on the Internet, and accepting incoming messages and sending them to the appropriate internal mail server.


Update

Q: How do I configure JavaMail to work through my proxy server? [updated!]

A: Starting with JavaMail 1.6.0, JavaMail supports accessing mail servers through a web proxy server. Set the "mail.protocol.proxy.host" and "mail.protocol.proxy.port" properties for the proxy server. Proxy server BASIC authentication is supported by setting the "mail.protocol.proxy.user" and "mail.protocol.proxy.password" properties.

In addition, if your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package. Similar properties exist for the "imap" and "pop3" protocols. Authentication for SOCKS servers is supported by the JDK by setting the "java.net.socks.username" and "java.net.socks.password" System properties (and thus apply to all SOCKS connections) as describe in the JDK Networking Properties documentation.

If you're using older versions of the JDK or JavaMail, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. These are system-level properties, not JavaMail session properties. They can be set from the command line when the application is invoked, for example: java -DsocksProxyHost=myproxy .... This facility can be used to direct the SMTP, IMAP, and POP3 communication from JavaMail to the SOCKS proxy server. Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application.

When using older versions of JavaMail, and without such a SOCKS server, if you want to use JavaMail to access mail servers outside the firewall indirectly, you might be able to use a program such as connect to tunnel TCP connections through an HTTP proxy server. Configure JavaMail to use the connect instance as the SOCKS server.

Comments

3

This happens because of Gmail security... just allow access to your gmail account via apps go to under your account: https://www.google.com/settings/security/lesssecureapps and it will work .. smtp.gmail.com is correct.No need to change it.

Also don't forget to check internet connection as well.

1 Comment

Even though it's an old answer I would like to say that it is only true in some cases. In my case lesssecureapps are turned on, but still proxy is a problem. Seems that there is no good way to overgo this problem..
1

could you try with props.put("mail.smtp.host", "gmail.com"); without smtp.gmail.com

Comments

1

Just for development purposes, you can try to open tunnel via your proxy to smtp.gmail.com

proxytunnel -q -p proxy_host:proxy_port -d smtp.gmail.com:465 -a local_port

and use in your code

props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "local_port");

1 Comment

How to open the tunnel?
0

It works fine when you uprade javamail to 1.6.2

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.