gpt4 book ai didi

java - Selenium Java 邮件发送错误

转载 作者:行者123 更新时间:2023-12-02 04:29:34 25 4
gpt4 key购买 nike

我正在使用以下代码通过 selenium 发送邮件。

 Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", "****@gmail.com");
props.put("mail.smtp.password", "*******");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.EnableSSL.enable","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", "587");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
//Set from address
message.setFrom(new InternetAddress("mailchecker"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("******@gmail.com"));
//Set subject
message.setSubject("Test Execution Status");
message.setText("\n"+passeddata+"\n"+faildata);
BodyPart objMessageBodyPart = new MimeBodyPart();
objMessageBodyPart.setText("Please Find The Attached Report File!");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(objMessageBodyPart);
objMessageBodyPart = new MimeBodyPart();
//Set path to the pdf report file
String filename = System.getProperty("C:\\ITextTest3.pdf");
//Create data source to attach the file in mail
DataSource source = new FileDataSource(filename);
objMessageBodyPart.setDataHandler(new DataHandler(source));
objMessageBodyPart.setFileName(filename);
multipart.addBodyPart(objMessageBodyPart);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, "*****@gmail.com", "*****");
transport.sendMessage(message, message.getAllRecipients());
transport.close();}
finally{}

但是我收到以下错误。预期是缺少一些文件 javamail.address.map。但邮件调试信息表明文件已成功加载,如下面的代码片段所示。

DEBUG: URL jar:file:/C:/Users/191/Desktop/Datacede/javamail-smtp-1.4.2.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/C:/Users/191/Desktop/Datacede/javamail-smtp-1.4.2.jar!/META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre7\lib\javamail.address.map (The system cannot find the file specified)

[TestNG] Reporter com.sel.package classname@1bf3519 failed

请给出解决方案。

最佳答案

确保您已将以下 JAR 添加到您的项目中:

1 - Activation.jar

2 - 附加.jar

3 - java-mail-1.4.4

4 - javamail-connector-4.0

5 - mail.jar

6 - pop3.jar

7 - smtp-1.4.2.jar

以下代码对我有用:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail{

public static void main(String[] args) {

final String username = "username@gmail.com";
final String password = "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", "587");

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("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");

MimeBodyPart messageBodyPart2 = new MimeBodyPart();

String filename = "Your attachment file path"
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);



Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);

message.setContent(multipart );


Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

我遇到了和你一样的错误,所以我添加了丢失的 JAR,它可以工作。首先添加缺少的 JAR,然后尝试发送邮件。

关于java - Selenium Java 邮件发送错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com