【发布时间】:2016-06-05 02:39:54
【问题描述】:
我编写了一个代码,用于发送具有附件文件但收到 javax.mail.SendFailedException 异常的邮件。下面是我写的代码。谁能纠正我为什么我得到异常 以下是异常
java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at sendMail.sendMail2.execute(sendMail2.java:102)
at sendMail.sel.main(sel.java:17)
下面是邮件类
package sendMail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class sendMail2 {
public static void execute() throws Exception {
final String username = "sender@gmail.com";
final String password = "*********";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recepient@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "C:/Selenium Workspace/FMC360Automation/test-output/emailable-report.html";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
下面是基本的 Java Selenium 程序
package sendMail;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class sel {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/?gws_rd=cr,ssl&ei=sjPLVue4OI2IuATgkaWwCw");
try {
//Sendmail.execute("C:/Selenium Workspace/FMC360Automation/test-output/emailable-report.html");
sendMail2.execute();
driver.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
【问题讨论】:
-
@Berger 感谢您的评论,但我得到了同样的例外
-
JavaMail 常见问题解答有 tips for debugging connection problems。
-
我从本地系统发送
标签: java selenium-webdriver gmail jakarta-mail