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