【问题标题】:Java Mail Exception Error Unknown SMTP host: smtp.gmail.com is E-mail is not sendJava Mail Exception Error Unknown SMTP host: smtp.gmail.com is E-mail is not send
【发布时间】:2022-01-14 12:23:12
【问题描述】:

我正在尝试通过 Gmail 发送电子邮件,但出现异常“未知的 SMTP 主机:smtp.gmail.com”。 我已经开启不太安全的应用访问,两步验证应该关闭。

public class MonitoringMail
{
    public void sendMail(String mailServer, String from, String[] to, String subject, String messageBody) throws MessagingException, AddressException
    {
        boolean debug = false;
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.EnableSSL.enable","true");
        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.host", mailServer); 
        props.put("mail.debug", "true");
        
         props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
         props.setProperty("mail.smtp.socketFactory.fallback", "false");   
         props.setProperty("mail.smtp.port", "465");   
         props.setProperty("mail.smtp.socketFactory.port", "465"); 

        
          Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getDefaultInstance(props, auth);

            session.setDebug(debug);
        
        try
        {
            
            
            Transport bus = session.getTransport("smtp");
            bus.connect();
            Message message = new MimeMessage(session);
        
         //X-Priority values are generally numbers like 1 (for highest priority), 3 (normal) and 5 (lowest).
            
             message.addHeader("X-Priority", "1");
             message.setFrom(new InternetAddress(from));
             InternetAddress[] addressTo = new InternetAddress[to.length];
             for (int i = 0; i < to.length; i++)
             addressTo[i] = new InternetAddress(to[i]);
             message.setRecipients(Message.RecipientType .TO, addressTo);
             message.setSubject(subject);
                  
             
             BodyPart body = new MimeBodyPart();

            // body.setText(messageBody);
            body.setContent(messageBody,"text/html");

             MimeMultipart multipart = new MimeMultipart();
             multipart.addBodyPart(body);
            // multipart.addBodyPart(attachment);
             message.setContent(multipart);
             Transport.send(message);
             System.out.println("Sucessfully Sent mail to All Users");
             bus.close();
            
        }
        catch (MessagingException mex)
        {
            mex.printStackTrace();
        }       
    } 

它早些时候工作,但一周后我得到了一个例外。 我尝试解决此异常,我花了 3 天时间,但我无法解决这些问题,请尝试解决此问题。

【问题讨论】:

    标签: java selenium email smtp


    【解决方案1】:

    SMTP 有 2 个不同的端口 SSL 端口 465 TLS/STARTTLS 的端口 587 您是否尝试过使用其他端口?

    【讨论】:

    • 是的,我尝试那个时候给出另一个例外。
    【解决方案2】:

    请尝试使用端口 25。

    props.setProperty("mail.smtp.port", "25");
    

    【讨论】:

      【解决方案3】:

      我了解到您已设置 gmail 帐户并启用了安全性较低的应用程序,但 Gmail 帐户会在一段时间不活动后自动关闭“允许安全性较低的应用程序”选项。如果此代码之前有效,请尝试再次检查您的 gmail 帐户设置。

      【讨论】:

        【解决方案4】:

        在我的情况下,我在它正常工作之后更改了端口

        【讨论】:

          猜你喜欢
          • 2012-09-11
          • 2021-12-09
          • 2017-07-12
          • 2022-12-01
          • 2022-12-01
          • 1970-01-01
          • 2010-12-05
          • 2022-11-20
          • 2013-10-06
          相关资源
          最近更新 更多