14

i want to send an email using gmail as smtp server.

this is my code, and i do not get it to work... after running testSettings() i get the debug output and then it just stops. no timeout, no error, nothing....

public void testSettings() {
    final String username = Settings.get("benutzername");
    final String password = Settings.get("passwort");

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtps");

        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port", Settings.get("port"));
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");

    props.put("mail.smtp.auth", "true");

    props.put("mail.smtp.host", Settings.get("server"));
    props.put("mail.smtp.port", Settings.get("port"));
    props.put("mail.smtp.timeout", "10000");

    props.put("mail.smtp.ssl.checkserveridentity", "false");
    props.put("mail.smtp.ssl.trust", "*");
    props.put("mail.smtp.connectiontimeout", "10000");

    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
    session.setDebug(true);
    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("myemail@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("myemail@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("test");
        Transport transport = session.getTransport("smtps");
        transport.send(message);
        // Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        // throw new RuntimeException(e);
    }
}


DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning     javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false

The following error occurs: http://pastie.org/private/rkoknss6ppiufjd9swqta

pila
  • 928
  • 3
  • 11
  • 28

4 Answers4

27

From reading this: http://www.oracle.com/technetwork/java/javamail/faq-135477.html#commonmistakes

The use of

props.put("mail.smtp.socketFactory.class",
        "javax.net.ssl.SSLSocketFactory");** 

and

props.put("mail.smtp.socketFactory.port", "465");

is kind of outdated. To simplify the code, use:

properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
Hibbem
  • 1,491
  • 15
  • 22
22

Instead of

props.put("mail.transport.protocol", "smtps");

Transport transport = session.getTransport("smtps");

Use

props.put("mail.transport.protocol", "smtp");

Transport transport =session.getTransport("smtp");

Use smtp, not smtps

I used JDK 8, Netbeans 8, JavaMail 1.5.2 and this example works fine:

public static void main(String[] args) {
    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() {
                            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username@gmail.com","password");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("frommail@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("tomail@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Test Mail");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

If you are not able connect with port 465, try port 587

Community
  • 1
  • 1
Anar Orujov
  • 591
  • 5
  • 18
  • yeah i believe this works. i tested this some days ago on my laptop but on my desktop i cannot get it to work... and i do not know why... – pila Oct 24 '14 at 13:25
  • i just copied your code, created a new java project, filled in my data and then tested it. and it still does not happen anything. – pila Oct 24 '14 at 13:35
  • I used java 8, netbeans 8 and JavaMail API 1.5.2 tested and worked without any problem, and send mail myself, everything ok. Check your connection to smtp.gmail com 465 port, you can use telnet for it. Latest JavaMail API: https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release – Anar Orujov Oct 24 '14 at 13:43
  • mhhh when i telnet smtp.gmail.com 465 with putty, a blank screen opens and also nothing happens : – pila Oct 24 '14 at 13:48
  • this shows that you have connection, what java version use? if java version and api version different, some problems may occur – Anar Orujov Oct 24 '14 at 13:59
  • i use the latest java jdk 1.8_0_25, but finally got it working with port 587 instead of 465. you should add this to your answer then i can mark it – pila Oct 24 '14 at 14:11
  • Thanks for your post but I was not able to connect using SSL unless smpts protocol is used. A solution that worked for me is at http://www.rgagnon.com/javadetails/java-0570.html – Bit-Man Mar 23 '15 at 08:41
  • Thanks but Gmail ask me to enable allow not secure applications. Is there any way to avoid this? – jmoran Jul 07 '18 at 04:08
1

If you using Windows and have any Antivirus or firewall disable it and try.

0

My problem was in computer name, it was contain cyrillic symbols. For understanding reason of your problem add

props.put("mail.debug", "true");

Also see work code here

Anton
  • 604
  • 2
  • 11
  • 22