gpt4 book ai didi

java - 如何解决异常 "java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger"。我正在尝试通过 selenium webdriver 发送电子邮件

转载 作者:行者123 更新时间:2023-12-02 03:49:39 25 4
gpt4 key购买 nike

这是我发送电子邮件的代码

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.abc.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("vijaya.teke@abc.com", "********");
}

});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("vijaya.teke@abc.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("vijaya.teke@abc.com"));
message.setSubject("Testing Subject");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "D:\\Automation\\Automation workspace\\TestSelenium\\Screenshots";
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);
}

我在“Session session = Session.getDefaultInstance(props,”行)行收到错误“java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger”。我没有得到解决方案。请帮助我.

最佳答案

以下对我有用:

package Reporting;
/**
* @author Shubham Jain
*
*/
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

import Utilities.Configuration;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* Note : This Class zip the folder of reports and sent the email via gmail. Make sure files do not contains any JS files or extention banned by gmail.
* Don't use automation log in this class, It will override all before logs of the same session
* */
public class SendMailClass
{
public SendMailClass(){

}
/* static ExcelLib xl = new ExcelLib();
static String browserType = xl.getXLcellValue("Controller", 1, 0);
static String serverName = xl.getXLcellValue("Controller", 1, 1);*/

private static String OSNAMES = System.getProperty("os.name").toLowerCase();
static String OS = OSNAMES.split(" ")[0];
static Date currentDate = new Date();
static SimpleDateFormat dateFormatForFileName = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//dd/MM/yyyy
static String fileDateFormet = dateFormatForFileName.format(currentDate);
// static String testoutputfolderpath = System.getProperty("user.dir")+ File.separator +"target"+ File.separator+ "cucumber-html-reports";
// static String zipFilefolderpath = System.getProperty("user.dir")+ File.separator +"reports"+File.separator+"results"+fileDateFormet; // Zip Reports Files
static String zipLogFilefolderpath = System.getProperty("user.dir")+ File.separator +"logs"+File.separator+"logs.txt";
/* static File folder = new File(zipLogFilefolderpath);
static File zipFile = new File(zipFilefolderpath);*/
static String subject = "Shubham Automation Application Test Cucumber report";
static String PlatformName = Configuration.getConfigurationValueForProperty("Report-Upload-Platform-Name");
static String emailForsendingnotification = Configuration.getConfigurationValueForProperty("Email-Sending-Notification");
static String PasswordForsendingnotification = Configuration.getConfigurationValueForProperty("Pass-Sending-Notification");

public static void main(String[] args) throws IOException {
try {
execute();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

public static void execute() throws Exception
{

// ExcelLib xl = new ExcelLib();


String FilenameLocalPath=ZipReports.zipFilePathName();

String[] b = FilenameLocalPath.split("\\\\");
String FileName = b[b.length-1];

String text = "Hi Greetings,\n This is Final Automation Test Report.\n This Auto-generated report please do not reply.\n Find the reports on "+PlatformName+" account, The Report file name is : "+FileName+".\n Please download your file from given URL: https://www.dropbox.com/home/Apps/AutomationResults/Apps/ShubhamCucumberReports";
//////////////////////////////////////////////////////////////////////////////////
////////////////////////Create a zip file of test-output folder /////////////////
// path of file which contains report

// String path = zipFilefolderpath;
String path2 = zipLogFilefolderpath;

// No of recipients of the report, you can have many separated by comma

String mailto = "shubham.jain@abc.co.in";
String mailcc = "shubham.jain@abc.co.in";
String mailbcc = "shubham.jain@abc.co.in";


String[] to={mailto};
String[] cc={mailcc};
String[] bcc={mailbcc};


// email user name and password of sender


SendMailClass.sendMail(emailForsendingnotification,
PasswordForsendingnotification,
"smtp.gmail.com",
"465",
"true",
"true",
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
subject,
text,
path2,
"Test-Report");
}

public static boolean sendMail(String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
String socketFactoryClass,
String fallback,
String[] to,
String[] cc,
String[] bcc,
String subject,
String text,
String attachmentPath,
String attachmentName){

//Object Instantiation of a properties file.
Properties props = new Properties();

props.put("mail.smtp.user", userName);

props.put("mail.smtp.host", host);

if(!"".equals(port)){
props.put("mail.smtp.port", port);
}

if(!"".equals(starttls)){
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
}

if(!"".equals(port)){
props.put("mail.smtp.socketFactory.port", port);
}
if(!"".equals(socketFactoryClass)){
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
}
if(!"".equals(fallback)){
props.put("mail.smtp.socketFactory.fallback", fallback);
}

try{

Session session = Session.getDefaultInstance(props, null);

MimeMessage msg = new MimeMessage(session);

msg.setText(text);

msg.setSubject(subject);

Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
//////////////////////////////////
BodyPart objMessageBodyPart = new MimeBodyPart();

objMessageBodyPart.setText(text);
/////////////////////////////////
messageBodyPart.setText(text);
messageBodyPart.setContent(text, "text/html");
DataSource source = new FileDataSource(attachmentPath);

messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("Logs.txt");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(objMessageBodyPart);
/*
/////////////////////////////// Add another File ////////////////

DataSource source2 = new FileDataSource(attachmentPath2);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(text);
messageBodyPart2.setDataHandler(new DataHandler(source2));
messageBodyPart2.setFileName(attachmentPath2);
messageBodyPart2.setFileName("Reports.txt");
multipart.addBodyPart(messageBodyPart2);

/////////////////////////////////////////////////////////////////////
*/
msg.setContent(multipart);

msg.setFrom(new InternetAddress(userName));

for(int i=0;i<to.length;i++){
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to[i]));
}

for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc[i]));
}

for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new
InternetAddress(bcc[i]));
}

msg.saveChanges();

Transport transport = session.getTransport("smtp");

transport.connect(host, userName, passWord);

transport.sendMessage(msg, msg.getAllRecipients());

transport.close();
//closing .exe of chrome or phantom driver binaries
// AppDriver.clearBrowserContext(Page.driver);
return true;

} catch (Exception mex){
mex.printStackTrace();
return false;
}
}
}

删除一些与您无关的代码。例如 zip 调用函数等

关于java - 如何解决异常 "java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger"。我正在尝试通过 selenium webdriver 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787279/

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