I am using below piece of code to send email with PDF attachment using code module. But it is not able to locate the file and throws the error as followed by the code. Please note that I am able to send the email using same code (without attachments).
Code:
MailMessage mail = new MailMessage(); SmtpClient smtpServer = new SmtpClient("smtp.mail.yahoo.com"); mail.From = new MailAddress("From"); mail.To.Add("To"); mail.Subject = "This email is just for test"; mail.IsBodyHtml = true; mail.Body = "This Email Body is just for test"; SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com" , 587); smtp.EnableSsl = true; smtp.Credentials = new System.Net.NetworkCredential("emailIDFromEmailWouldBeSent", "Password"); string reportName = TestReport.ReportEnvironment.ReportName; string RepPath = TestReport.ReportEnvironment.ReportFileDirectory; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(RepPath +"\\" + reportName + ".pdf"); mail.Attachments.Add(attachment); smtp.Send(mail);Error:
Could not find file 'C:\..\Ranorex\RanorexStudio Projects\EmailReportTest\EmailReportTest\bin\Debug\Reports\EmailReportTest_20190905_171818.pdf'.
Stack Trace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) at System.Net.Mail.AttachmentBase..ctor(String fileName) at System.Net.Mail.Attachment..ctor(String fileName) at EmailReportTest.EmailReport.Ranorex.Core.Testing.ITestModule.Run() in c:\..\Ranorex\RanorexStudio Projects\EmailReportTest\EmailReportTest\EmailReport.cs:line 80 at Ranorex.Core.Testing.TestModuleLeaf.RunInternal(DataContext parentDataContext, Int32 iteration, Int32 iterationCount, Boolean skipIteration)
So Basically Ranorex is not able to find the PDF file using above code. Any help would be highly appriciated.