Javamail无法使用gmail SMTP

时间:2017-10-01 06:56:24

标签: ssl javamail

我尝试使用javamail从我的java代码发送电子邮件时遇到异常。我正在尝试使用gmail SMTP(SSL,端口465)。 TLS有效(端口587)。

这是我正在做的事情。

mailServerProperties = System.getProperties();    
mailServerProperties.put("mail.smtp.port", "465");
mailServerProperties.put("mail.smtp.auth", "true");

mailServerProperties.put("mail.smtp.socketFactory.port", "465");
mailServerProperties.put("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory");


getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(ei.getToaddr()));
generateMailMessage.setFrom(new InternetAddress(ei.getUser()));
generateMailMessage.setSubject(ei.getSubject());
generateMailMessage.setText(ei.getMessage());

transport = getMailSession.getTransport("smtp");
transport.connect("smtp.gmail.com", "user", "password");

获得此例外......

2017-10-01 06:53:51.094 UTC SEVERE: 2542: EnhancedEmailer.generateAndSendEmail: Exception sending email
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2042)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)

是否有任何属性出错?

3 个答案:

答案 0 :(得分:1)

执行以下步骤:

  1. 在电子邮件中禁用“两因素验证”
  2. 导航至:“ https://myaccount.google.com/lesssecureapps?pli=1”并打开“访问安全性较低的应用程序”
  3. 下载JavaMail API“ https://www.oracle.com/technetwork/java/javamail/index-138643.html”并将其添加到您的库中

代码

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class email_try {


    public static void main(String ap[]) {



      String myEmail = "YOUR EMAIL";
      String password = "YOUR PASSWORD";
      String opponentEmail = "THEIR EMAIL";
      Properties pro = new Properties();
      pro.put("mail.smtp.host", "smtp.gmail.com");
      pro.put("mail.smtp.starttls.enable", "true");
      pro.put("mail.smtp.auth", "true");
      pro.put("mail.smtp.port", "587");
      Session ss = Session.getInstance(pro, new javax.mail.Authenticator() {
       @Override
       protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(myEmail, password);
       }
      });



    try {
       Message msg = new MimeMessage(ss);
       msg.setFrom(new InternetAddress(myEmail));
       msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(opponentEmail));
       msg.setSubject("Your Wish");
       msg.setText("java email app");
       Transport trans = ss.getTransport("smtp");
       Transport.send(msg);
       System.out.println("message sent");
      } catch (Exception e) {
       System.out.println(e.getMessage());
      }

 }
}

//尝试输入此代码并输入正确的电子邮件ID和密码

答案 1 :(得分:0)

检查自爆属性对我有用

props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
        props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
        props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
                props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "465"); //SMTP Port
                props.put("mail.smtp.ssl.enable", "true");

我认为您的代码中缺少以下属性

props.put("mail.smtp.ssl.enable", "true");

答案 2 :(得分:0)

请参考链接以为应用生成密码。 https://devanswers.co/create-application-specific-password-gmail/

然后尝试

// client.html (you write the UI and other stuff for the sidebar or modal or webapp page)
function calledByServer(serverOutput) {
  console.log({ serverOutput }); // or whatever you want to do with the response
}
function doServer(...argsArray) { //invoke somehow, e.g. a client-side click handler
  google.script.run
    .withSuccessHandler(calledByServer)
    .ServerSideFunctionName(argsArray);
}

// Code.gs
function ServerSideFunctionName(foo) {
  console.log(foo); // Stackdriver logging, since Logger is instance specific
  // Do something with foo
  return someValueToSendToClient;
}