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

Experiences, small talk, and other automation gossip.
rahulcan
Posts: 11
Joined: Tue Dec 18, 2018 5:04 am

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

Post by rahulcan » Thu Sep 05, 2019 10:40 pm

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.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7209
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

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

Post by odklizec » Fri Sep 06, 2019 8:58 am

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?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

rahulcan
Posts: 11
Joined: Tue Dec 18, 2018 5:04 am

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

Post by rahulcan » Fri Sep 06, 2019 1:22 pm

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)

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7209
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

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

Post by odklizec » Fri Sep 06, 2019 1:25 pm

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.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

rahulcan
Posts: 11
Joined: Tue Dec 18, 2018 5:04 am

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

Post by rahulcan » Fri Sep 06, 2019 1:41 pm

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'.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7209
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

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

Post by odklizec » Fri Sep 06, 2019 1:44 pm

Hi,

And are you sure the PDF file of that name is there? ;) If not, then the PDF report was most probably not created?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

rahulcan
Posts: 11
Joined: Tue Dec 18, 2018 5:04 am

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

Post by rahulcan » Fri Sep 06, 2019 1:46 pm

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

rahulcan
Posts: 11
Joined: Tue Dec 18, 2018 5:04 am

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

Post by rahulcan » Fri Sep 06, 2019 3:30 pm

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.

shravani.ancha
Posts: 1
Joined: Mon Dec 13, 2021 10:46 am

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

Post by shravani.ancha » Mon Dec 13, 2021 10:49 am

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.

Elena_v
Posts: 18
Joined: Tue Jun 25, 2019 4:27 pm

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

Post by Elena_v » Thu Dec 30, 2021 11:24 am

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?