简体   繁体   中英

How do I fix a javax.mail.MessagingException when using SMTP through GMail?

I cannot execute code that is publicly available for sending email through Gmail. I think it may be a network problem because I am at work although I am able to ping Gmail through the cmd prompt: ping smtp.gmail.com .

Here is the code I am using (from this page ):

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


class tester {
    public static void main(String args[]) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.stmp.user", "username of the sender");          
        //If you want you use TLS 
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.password", "password of the sender");
        // If you want to use SSL
        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 Authenticator() {
            @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    String username = "username";
                    String password = "password";
                    return new PasswordAuthentication("username","password"); 
                }
            });
        String to = "me@gmail.com";
        String from = "from@gmail.com";
        String subject = "Testing...";
        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(MimeMessage.RecipientType.TO, new    InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("JAVA is the BEST");
            Transport transport = session.getTransport("smtp");
            transport.send(msg);
            System.out.println("E-mail sent !");
        } catch(Exception exc) {
            System.out.println(exc);
        }
    }
}

I have changed the username and password fields to be correct. What are some things that could cause this? I have used my own code and many other publicly available code for this, but I get the same error. I did try running through port 587 also and received the same error.

Here is the error:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is: java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    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 GoogleMail.sendMail(GoogleMail.java:45)

I used the telnet smtp.gmail.com 465 and telnet smtp.gmail.com 587 command in the cmd prompt and both could not connect. I assume it is associated with the network I am connected to. I cannot provide any info beyond this and will update if I find out more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM