2013-08-19 4 views
0

У меня есть приложение Java, которое отправляет письма. Но когда я запускаю код, я получаю следующее исключение. Я также проверяю параметры проверки подлинности и с 485 портом нет, но все еще остается.javax.mail.MessagingException: Не удалось подключиться к хосту SMTP: smtp.gmail.com, порт: 587

DEBUG: setDebug: JavaMail version 1.4.4 
DEBUG: getProvider() returning  
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, 
Inc] 
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 
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 
587; 
nested exception is: 
java.net.ConnectException: Connection refused: connect 
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) 
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.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 SimpleMailWithAttachment.<init>(SimpleMailWithAttachment.java:46) 
at SimpleMailWithAttachment.main(SimpleMailWithAttachment.java:64) 
    Caused by: java.net.ConnectException: Connection refused: connect 

Вот код, я использую, чтобы попытаться послать почту

public SimpleMailWithAttachment() { 
    Properties props = System.getProperties(); 
    props.setProperty("mail.smtp.port", "587"); 
    props.setProperty("mail.smtp.host", "smtp.gmail.com"); 
    props.setProperty("mail.smtp.starttls.enable", "true"); 
    props.setProperty("mail.smtp.auth", "true"); 

    Authenticator auth = new MyAuthenticator(); 
    Session smtpSession = Session.getInstance(props, auth); 
    smtpSession.setDebug(true); 

    MimeMessage message = new MimeMessage(smtpSession); 
    try { 
     message.setFrom(new InternetAddress(email)); 

     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     final String encoding = "UTF-8"; 

     message.setSubject(subject, encoding); 
     message.setText(text, encoding); 
     Transport.send(message); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

static class MyAuthenticator extends Authenticator { 

    public PasswordAuthentication getPasswordAuthentication() { 
     String username = "******@gmail.com"; 
     String password = "*****"; 
     return new PasswordAuthentication(username, password); 

    } 
} 

ответ

1

Я не думаю, что вы можете использовать GMail без аутентификации. Вы должны использовать порт 465 не 587.

См http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

+0

Я уже пробовал код с портом 465, но он все еще терпит неудачу с тем же исключением. Любые предложения о том, как я могу это решить? – user407269