【问题标题】:Sending an email in java [duplicate]用java发送电子邮件[重复]
【发布时间】:2018-10-05 19:18:16
【问题描述】:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;
public class EmailSend {

    public static void main(String args[]){
        try{
            String host ="smtp.gmail.com" ;
            final String user = "abc@gmail.com";
            final String pass = "password";
            String to = "xyz@gmail.com";
            String from = "abc@gmail.com";
            String subject = "Trial";
            String messageText = "Your Is Test Email :";
            boolean sessionDebug = false;
                    Properties props = System.getProperties();

            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.required", "true");

            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(sessionDebug);
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject); msg.setSentDate(new Date());
            msg.setText(messageText);

           Transport transport=mailSession.getTransport("smtp");
           transport.connect(host, user, pass);
           transport.sendMessage(msg, msg.getAllRecipients());
           transport.close();
           System.out.println("message send successfully");
       }catch(Exception ex)
        {
            System.out.println(ex);
        }

    }
}

我添加了 activation-1.1.1.jar 和 mail-1.4.jar 并且我还将电子邮件 ID 设置更改为 * 允许不太安全的应用程序:开

但我得到以下异常

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我正在使用 Eclipse IDE(氧气 3A)

【问题讨论】:

  • 我不知道将其标记为副本是否正确,因为它鼓励 OP 绕过证书,假设他们已经尝试导入证书,而实际上他们可能只是不知道他们应该首先导入它

标签: java email servlets


【解决方案1】:

您面临的错误与 Java 邮件无关,而是与 SSL 证书有关。要解决此错误,您需要将 SMTP 主机的证书导入您的密钥库。

Google 提供以下机制来下载 smtp 证书:

openssl s_client -starttls smtp -connect [hostname]:25 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'

请参考“https://support.google.com/a/answer/6180220?hl=en”上的其他证书下载方法

获得证书后,您可以使用以下命令将证书导入您的 java 密钥库:

keytool -import -file smtpgooglecert.cer -alias smtpgooglecert -keystore keystore.jks

希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 2015-10-09
    • 2020-04-24
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多