Page 1 of 1

Not able to attach a PDF report while sending an email using Code Module

Posted: Thu Sep 05, 2019 10:40 pm
by rahulcan
Hi,
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.

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 8:58 am
by odklizec
Hi,

My guess is that the path to file is simply too long and actually reached the limit for path length? Try to move the content of bin\Debug folder to C:\temp\nameoftest and run it from there. Hope this helps?

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 1:22 pm
by rahulcan
I tried doing what you suggested but, it still throws me the same error.

Error:
Could not find file 'C:\Users\..\Desktop\Reports\EmailReportTest_20190906_081720.pdf'.
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:\Users\xluser\Documents\Ranorex\RanorexStudio Projects\EmailReportTest\EmailReportTest\EmailReport.cs:line 79 at Ranorex.Core.Testing.TestModuleLeaf.RunInternal(DataContext parentDataContext, Int32 iteration, Int32 iterationCount, Boolean skipIteration)

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 1:25 pm
by odklizec
Hi,

Moving project to Desktop is not a very good idea. Please move it to C:\Temp or similar location. c:\users\... or Desktop makes the path to exe (and so reports), extremely long.

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 1:41 pm
by rahulcan
Hi,

I moved it to C:\Temp but still getting the same error. the file is getting saved properly in the same folder.

Could not find file 'C:\temp\EmailReportTest_20190906_083054.pdf'.

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 1:44 pm
by odklizec
Hi,

And are you sure the PDF file of that name is there? ;) If not, then the PDF report was most probably not created?

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 1:46 pm
by rahulcan
Hi,

Yes! It is there. At first I had the same question. So I navigated to the same path and check if the report is there. And it was there. :D

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Fri Sep 06, 2019 3:30 pm
by rahulcan
Please note that. in above case, if I am trying to attach .rxlog file, then it works fine. It throws above error only when I am trying to attach .pdf report which gets created just before email is sent.

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Mon Dec 13, 2021 10:49 am
by shravani.ancha
Hello,

Even I'm facing the same issue. not able to upload the pdf file through email.
getting error as:
Generating PDF report with following specifications:

Input file: Demo_TestSuite_20211213_151329.rxzlog
Output file: Demo_TestSuite_20211213_151329.pdf
Style: RanorexReport.xsl
Details: all

ReportToPDF: Object reference not set to an instance of an object.
Multiple TestSuiteActivites, status taken from first entry
[2021/12/13 15:14:44.426][Warn ][User]: The file 'C:\temp\Ranorex\RanorexStudio Projects\WQM\IF3AndroidAutomation\bin\Debug\Demo_TestSuite_20211213_151329.pdf' does not exist. Please make sure the path 'Ranorex.Core.Reporting.ReportEnvironment' is correct.

Re: Not able to attach a PDF report while sending an email using Code Module

Posted: Thu Dec 30, 2021 11:24 am
by Elena_v
Have you tried debugging the code? At what break point does the test stop?
1) Check pls that the RepPath and reportName do not get null() or uncorrect data.
2) Run the test suite (!) as administrator.
3) Does the reportName variable get the name of the file with or without format?
4) Does sending a rxzlog by E-Mail work?