java - SendEmail 在 Selenium Webdriver 中无法工作 - 未知 SMTP 主机 : smtp. gmail.com;

标签 java email selenium-webdriver proxy smtp

如果我不在任何防火墙后面,我的代码可以完美运行。但是,当我从任何防火墙(公司网络)后面的网络执行相同的代码时,它不起作用并引发异常。

下面是我的代码-

public static void main(String[] args) {
    String emailbody = Utilities.getProperty("BASE_MESSAGE").replace("<<PROJECT_NAME>>",Utilities.getProperty("PROJECT_NAME"));
    emailbody += "\n\n Environment: ";
    emailbody += "\n\nTest Cases Executed: 7 " ;
    emailbody += "\nTest Cases Passed: 5" ;
    emailbody += "\nTest Cases Failed: 2";
    emailbody += "\nTest Cases Skipped: 0";
    emailbody += "\n\nAttached is the detailed report. Please reach out to Automation Team for more details.";
    emailbody += "\n\nRegards,\nAutomation Team";
    emailsend(emailbody);

}



/**
 * Function to Send an Emial
 * @param emailbody
 */
public static void emailsend(String emailbody) {
    final String usernameFrom = Utilities.getProperty("EMAIL_USER_NAME");
    final String password = Utilities.getProperty("EMAIL_PASSWORD");
    // Create object of Property file       
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "465");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "true");

    // This will handle the complete authentication
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
    {
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(usernameFrom, password);
        }
    });

    try {

        // Create object of MimeMessage class
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(usernameFrom));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xxxx@yy.com"));
        message.setSubject("Testing Subject");

        // Create object to add multimedia type content
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText(emailbody);
        MimeBodyPart messageBodyPart2 = new MimeBodyPart();
        String filename = "C:\\Users\\test.xlsx";

        DataSource source = new FileDataSource(filename);
        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName(filename);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart2);
        multipart.addBodyPart(messageBodyPart1);
        message.setContent(multipart);
        Transport.send(message);
        System.out.println("=====Email Sent=====");

    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }

}

异常 - javax.mail.MessagingException:未知 SMTP 主机:smtp.gmail.com; 嵌套异常是: java.net.UnknownHostException:smtp.gmail.com

最佳答案

SendEmail is not working in Selenium Webdriver

Selenium WebDriver 与发送电子邮件没有任何关系。两者都来自不同的 java 包。

Unknown SMTP host: smtp.gmail.com

这是实际问题。

My code is working perfectly if am not behind any firewall. But when i have executed same code from the network which is behind any firewall (company network) its not working and throws exceptions.

这清楚地表明您的公司网络阻止了对 smtp.gmail.com 的访问

您可以通过简单的ping命令来验证这一点。

$ ping smtp.gmail.com

可访问的网络 ping 结果:

$ ping smtp.gmail.com
PING smtp.gmail.com (74.125.24.109): 56 data bytes
64 bytes from 74.125.24.109: icmp_seq=0 ttl=42 time=80.973 ms
64 bytes from 74.125.24.109: icmp_seq=1 ttl=42 time=89.877 ms
64 bytes from 74.125.24.109: icmp_seq=2 ttl=42 time=94.582 ms

无访问网络 ping 结果:

$ ping smtp.gmail.com
PING smtp.gmail.com (74.125.24.109): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3

希望这有帮助。

关于java - SendEmail 在 Selenium Webdriver 中无法工作 - 未知 SMTP 主机 : smtp. gmail.com;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883441/

相关文章:

django - django 基于日期的自动电子邮件通知

email - 将 HTML 表单放入电子邮件是否不切实际?

java - Hamcrest describeMismatchSafely 总是在我的实现中打印 Object.toString()

java - 在 JComboBox 中显示属性并注册另一个属性

email - 在 ASP 中发送电子邮件(CDO 消息)每次挂起大约 5 到 10 秒

javascript - 我应该明确等待元素在 Protractor 测试中可见吗?

python - “phantomjs”可执行文件可能具有错误的权限

selenium - 以另一种方式检查 'checkBox' -> xPath

java - Android 与 PC 上的 Java : Different HttpResponse?

java - Stacktrace 不在 Glassfish 4.1 集群日志中打印