无法发送Gmail邮件

时间:2017-05-23 09:09:57

标签: java gmail

我正在尝试编写一个java代码来通过Gmail发送电子邮件,我能够获得正确的配置和代码,并在家中测试了我的代码,(电子邮件是从家里成功发送的),当我出现问题时在工作中运行相同的代码,我能够找出网络上的问题,所以我让我的NW管理员为我打开端口465和587并开始面临一个新问题,我无法与smtp建立连接.gmail.com在两个端口上,我安装了telnet,并运行以下命令

telnet smtp.gmail.com 465

并且telnet连接以这种奇怪的方式运行(命令仅显示下划线(_)并且没有任何内容出现,请参阅附图),请注意,当我在任何其他计算机上运行相同的命令时,它运行正常。< / p>

我关掉了windows防火墙,反病毒。因为问题只是我的电脑,所以我仍然无法确定我需要做些什么才能使它工作。 这是我的代码:

package mail;

private SendMail() {
}

public static void main(String[] args) throws AddressException, MessagingException {
    try {       
        send("mySenderMail@gmail.com", "myPassword", "myRecieverMail@gmail.com", "test java code mail", "this is a text message string");
        System.out.println("Sent Sucessfully");

    }
        catch (AddressException exc) {
        System.out.println("exception"+ exc.getMessage());
        System.out.println("cause:"+ exc.getCause());
    }
    catch(MessagingException exc ){
        System.out.println("exception"+ exc.getMessage());
        System.out.println("cause:"+ exc.getCause());
    }
    catch(Exception exc){
        System.out.println("exception"+ exc.getMessage());
        System.out.println("cause:"+ exc.getCause());
    }
}

public static void send(final String username, final String password, String recipientEmail, String title,
        String message) throws AddressException, MessagingException,ArrayIndexOutOfBoundsException , IOException {

    SendMail.send(username, password, recipientEmail, "", title, message);
}
public static void send(final String username, final String password, String recipientEmail, String ccEmail,
        String title, String message) throws AddressException, MessagingException , ArrayIndexOutOfBoundsException, IOException {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    // Get a Properties object
    String emailHost = username.split("@")[1].toLowerCase() + ".";
    Properties prop = new Properties();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();           
    InputStream stream = loader.getResourceAsStream("mail/mailConfig.properties");
    prop.load(stream);

    String smtpHost = prop.getProperty(emailHost + "smtpHost");
    String socketFactoryClass = prop.getProperty(emailHost + "socketFactoryClass");
    String socketFactoryFallback = prop.getProperty(emailHost + "socketFactoryFallback");
    String smtpPort = prop.getProperty(emailHost + "smtpPort");
    String socketFactoryPort = prop.getProperty(emailHost + "socketFactoryPort");
    String auth = prop.getProperty(emailHost + "auth");
    String starttlsEnable = prop.getProperty(emailHost + "starttlsEnable");
    String debug = prop.getProperty(emailHost + "debug");
    String quitwait = prop.getProperty(emailHost + "quitwait");

    Properties props = System.getProperties();
    if (smtpHost != null && !smtpHost.isEmpty())
        props.setProperty("mail.smtp.host", smtpHost);
    if (socketFactoryClass != null && !socketFactoryClass.isEmpty())
        props.setProperty("mail.smtp.socketFactory.class", socketFactoryClass);

    if (socketFactoryFallback != null && !socketFactoryFallback.isEmpty())
        props.setProperty("mail.smtp.socketFactory.fallback", socketFactoryFallback);

    if (smtpPort != null && !smtpPort.isEmpty())
        props.setProperty("mail.smtp.port", smtpPort);

    if (socketFactoryPort != null && !socketFactoryPort.isEmpty())
        props.setProperty("mail.smtp.socketFactory.port", socketFactoryPort);

    if (auth != null && !auth.isEmpty())
        props.setProperty("mail.smtp.auth", auth);

    if (starttlsEnable != null && !starttlsEnable.isEmpty())
        props.setProperty("mail.smtp.starttls.enable", starttlsEnable);

    if (debug != null && !debug.isEmpty())
        props.setProperty("mail.smtp.debug", debug);


    if (quitwait != null && !quitwait.isEmpty())
        props.put("mail.smtps.quitwait", quitwait);

    Session session = Session.getInstance(props, null);

    // -- Create a new message --
    final MimeMessage msg = new MimeMessage(session);
    session.setDebug(true);
    // -- Set the FROM and TO fields --
    msg.setFrom(new InternetAddress(username));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

    if (ccEmail.length() > 0) {
        msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
    }

    msg.setSubject(title);
    msg.setText(message, "utf-8");
    msg.setSentDate(new Date());

    SMTPTransport t = (SMTPTransport) session.getTransport("smtp");

    t.connect(smtpHost, username, password);
    t.sendMessage(msg, msg.getAllRecipients());
    t.close();

}

这是我的配置文件内容:

## ----------------- Gmail -----------------## 
gmail.com.smtpHost = smtp.gmail.com
gmail.com.socketFactoryClass = javax.net.ssl.SSLSocketFactory
gmail.com.socketFactoryFallback = false
gmail.com.smtpPort = 465
gmail.com.socketFactoryPort = 465
gmail.com.auth = true
gmail.com.starttlsEnable = true
gmail.com.debug = true
gmail.com.quitwait = false

telnet image

btw:运行我的代码时出现以下异常

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at mail.SendMail.send(SendMail.java:213)
    at mail.SendMail.send(SendMail.java:111)
    at mail.SendMail.main(SendMail.java:26)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:207)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)

我真的不知道接下来要做什么,请帮助。

1 个答案:

答案 0 :(得分:0)

我还没有对此进行过测试,因为我处于一个封闭的环境中,但我想你在这里尝试做的是在使用用于SSL的端口时将连接保持为TLS。

根据gmail规范,他们使用port-465进行SSL,而使用port-587进行TLS。

尝试以下任一方式:

  1. gmail.com.smtpPort = 587

  2. gmail.com.starttlsEnable = false

  3. 希望这有助于.......:)