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

Experiences, small talk, and other automation gossip.
rahulcan
Posts: 6
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: 5068
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: 6
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: 5068
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: 6
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: 5068
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: 6
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: 6
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.