Java 无法发送gmail邮件

Java 无法发送gmail邮件,java,gmail,Java,Gmail,我正在尝试编写一个java代码通过Gmail发送电子邮件,我能够获得正确的配置和代码,并在家中测试了我的代码(电子邮件在家中成功发送),当我在工作中运行相同的代码时出现问题,我能够找出问题出在网络上,因此,我要求我的NW管理员为我打开端口465和587,并开始面临一个新问题,即我无法在这两个端口上与smtp.gmail.com建立连接,我安装了telnet,并运行以下命令 telnet smtp.gmail.com 465 telnet连接的工作方式非常奇怪(命令只显示下划线(41;,没有显示

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

telnet smtp.gmail.com 465
telnet连接的工作方式非常奇怪(命令只显示下划线(41;,没有显示任何内容,请参见附图),请注意,当我在任何其他工作机器上运行相同的命令时,它运行正常

我关闭了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

顺便说一句:我在运行代码时遇到了以下异常

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)

我真的不知道下一步要做什么,请帮忙。

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

根据gmail规范,他们使用端口-465用于SSL,而端口-587用于TLS

请尝试以下任一方法:

  • gmail.com.smtpPort=587

  • gmail.com.starttlsEnable=false


  • 希望这有帮助……:)

    我尝试了你的建议,仍然没有成功,现在它说:javax.net.ssl.SSLException:无法识别的ssl消息,纯文本连接?兄弟,正如我告诉你的,我在封闭的环境中工作。我现在不能帮你排除故障。但从积极的方面来看,您现在可以连接,但无法握手,请尝试更改配置文件。。。。。。。。