Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用smtp发送邮件时出错_Java_Email_Gmail_Jakarta Mail - Fatal编程技术网

Java 使用smtp发送邮件时出错

Java 使用smtp发送邮件时出错,java,email,gmail,jakarta-mail,Java,Email,Gmail,Jakarta Mail,我需要从我的gmail帐户发送邮件到另一个帐户。我使用了以下代码 String fromaddress = "xxx@gmail.com"; String password = "yyy"; String hostname = "smtp.gmail.com"; String hoststring = "mail.smtp.host"; String toaddress = "yyy@gmail.com"; String e

我需要从我的gmail帐户发送邮件到另一个帐户。我使用了以下代码

               String fromaddress = "xxx@gmail.com"; 
    String password = "yyy";
    String hostname = "smtp.gmail.com";
    String hoststring = "mail.smtp.host";
    String toaddress = "yyy@gmail.com";
    String emailcontent;

    Properties properties = System.getProperties();
    properties.setProperty(hoststring, hostname);
    Session session = Session.getDefaultInstance(properties);

    try{
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromaddress));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toaddress));
        message.setSubject("hi");
        emailcontent = "hi...";
        message.setText(emailcontent);
        System.out.println(emailcontent);
        Transport.send(message);
        System.out.println("Sent....");
    }catch (MessagingException mex) 
    {
        mex.printStackTrace();
    }
但我得到的错误如下。。。 javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:25


我该如何解决这个问题。你能帮我一下吗。

我想你需要把25号端口换成587号端口

你可以从学校得到帮助

gmail设置帮助链接:


我想你需要把25号端口改成587号端口

你可以从学校得到帮助

gmail设置帮助链接:


只需对上述问题再做一些调整:

将端口更改为465以启用ssl发送

我认为上面的代码行不通,因为您也需要一个验证器对象。对于gmail,smtp也需要身份验证

您可以执行以下操作:

有一个布尔标志

boolean authEnable = true;  //True for gmail
boolean useSSL = true; //For gmail
//Getters and setters for the same

  if (isUseSSL()) {
         properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         properties.put("mail.smtp.socketFactory.port", "465");
  }

Authenticator authenticator = new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abc@gmail.com", "xyz"));
        }
    };
    if (isAuthEnable()) {
        properties.put("mail.smtp.auth", "true");
        session = Session.getDefaultInstance(properties, authenticator);
    } else {
        session = Session.getDefaultInstance(properties);
    }

只需对上述问题再做一些调整:

将端口更改为465以启用ssl发送

我认为上面的代码行不通,因为您也需要一个验证器对象。对于gmail,smtp也需要身份验证

您可以执行以下操作:

有一个布尔标志

boolean authEnable = true;  //True for gmail
boolean useSSL = true; //For gmail
//Getters and setters for the same

  if (isUseSSL()) {
         properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         properties.put("mail.smtp.socketFactory.port", "465");
  }

Authenticator authenticator = new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abc@gmail.com", "xyz"));
        }
    };
    if (isAuthEnable()) {
        properties.put("mail.smtp.auth", "true");
        session = Session.getDefaultInstance(properties, authenticator);
    } else {
        session = Session.getDefaultInstance(properties);
    }
}`

我认为这应该能奏效

}`


我认为这应该可以解决问题。

您好,我尝试过进行此配置。我将邮件主机更改为“smtp.googlemail.com”,并在thunderbird中配置了所需的端口。传出SMTP端口仅为587。但是,代码抛出以下错误。javax.mail.MessagineException:无法连接到SMTP主机:SMTP.googlemail.com,端口:25;嗨,我试着做这个配置。我将邮件主机更改为“smtp.googlemail.com”,并在thunderbird中配置了所需的端口。传出SMTP端口仅为587。但是,代码抛出以下错误。javax.mail.MessagineException:无法连接到SMTP主机:SMTP.googlemail.com,端口:25;