javamail错误,连接被拒绝

时间:2012-02-29 19:34:33

标签: java-ee sendmail

请帮助,我正在创建一个发送使用gmail帐户作为发件人的电子邮件的应用程序。但总是返回异常,如下所示:

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 true 
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 mailPackage.mail3.send(mail3.java:52) 
at mailPackage.mail3.main(mail3.java:25) 
Caused by: java.net.ConnectException: Connection refused: connect 
at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
at java.net.PlainSocketImpl.connect(Unknown Source) 
at java.net.SocksSocketImpl.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) 
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source) 
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288) 
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:205) 
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900) 
... 8 more 

这里是我从某个论坛获得的程序

        import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 
public class mail3 { 

/** 
* @param args 
*/ 
String d_email = "email@gmail.com", 
d_password = "password", 
d_host = "smtp.gmail.com", 
d_port = "587", 
m_to = "hanars3012@yahoo.com", 
m_subject = "last Test", 
m_text = "Hey, this is the testing email."; 
public static void main(String[] args) { 
// TODO Auto-generated method stub 

mail3 test=new mail3(); 
System.out.println(test.send()); 
} 
public String send(){ 
Properties props = new Properties(); 
props.put("mail.smtp.user", d_email); 
props.put("mail.smtp.host", d_host); 
props.put("mail.smtp.port", d_port); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.debug", "true"); 
props.put("mail.smtp.ssl.enable", "true"); 
props.put("mail.smtp.socketFactory.port", d_port); 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.socketFactory.fallback", "false"); 
SecurityManager security = System.getSecurityManager(); 
try 
{ 
Authenticator auth = new SMTPAuthenticator(); 
Session session = Session.getInstance(props, auth); 
session.setDebug(true); 

MimeMessage msg = new MimeMessage(session); 
msg.setText(m_text); 
msg.setSubject(m_subject); 
msg.setFrom(new InternetAddress(d_email)); 
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)); 

Transport.send(msg); 

} 
catch (Exception mex) 
{ 

mex.printStackTrace(System.out); 
return "error"; 
} 
//System.out.println("send"); 
return "sent"; 

} 

private class SMTPAuthenticator extends javax.mail.Authenticator 
{ 
public PasswordAuthentication getPasswordAuthentication() 
{ 
return new PasswordAuthentication(d_email, d_password); 
} 
} 
} 

我试过使用端口465和amp; 587但仍然总是返回错误...

提前感谢,

1 个答案:

答案 0 :(得分:1)

如果您使用的是TLS,则可以使用端口587 - 使用端口465作为ssl。
作为参考,请检查:

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

TLS的示例代码:

package com.testEmail.common;

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 = "username@gmail.com";
        final String password = "password";

        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("from-email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to-email@gmail.com"));
            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);
        }
    }
}

SSL的示例代码:

package com.testEmail.common;

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 SendMailSSL {
    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() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username","password");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@no-spam.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("to@no-spam.com"));
            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);
        }
    }
}