Send Mail Module

Download and share automation modules and examples.
User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Send Mail Module

Post by odklizec » Wed Jan 07, 2015 9:40 am

Hi,

As mentioned by krstcs, report files cannot be attached until the report is not fully written(finished). Therefore, you need to use the sendmail code as the very last step in program.cs. Also, it would be much easier for you if you set your tests to use the rxzlog, instead of rxlog. Rxzlog is a compressed ranorex log file, which contains all necessary report files. In fact, it's just a simple zip file with "rxzlog" extension ;)

To be able to view the rxzlog, you need to make sure there is installed ranorex report viewer on each machine where you want to view the log. It's not necessary to install whole Ranorex on these machines. As far as I remember, it's just required to copy these few files from Ranorex setup...
Ranorex.Controls.dll
Ranorex.Core.dll
Ranores.Libs.dll
Ranorex.ReportViewer.exe

Or you can simply extract the rxzlog (using any zip extractor) and simply view it in a web browser.

However, I wholeheartedly agree with krstcs regarding the continuous integration. It may require some time to learn and setup CI, but it's much more reliable way to send reports. And it's just a small fraction of all good things you will get by using CI.
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

amit_kumar01
Posts: 11
Joined: Tue Dec 23, 2014 1:15 pm

Re: Send Mail Module

Post by amit_kumar01 » Wed Jan 07, 2015 10:26 am

Dear odklizec,

I have inserted SendMail.cs at very last step in program.cs. After inserting Sendmail file, i am able to get only rxlog file as a test execution report to the mail. But whenever i tried to open this file, its displaying "missing some file (i.e. .data, .css, .xsl, and .png file).

Would you please provide me the detail how to attach the rxzlog file means what is process to get this file as an attachment?

With Regards,
Amit kumar

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

Re: Send Mail Module

Post by odklizec » Wed Jan 07, 2015 11:06 am

You need to enable "Compressed copy" option in Test Suite settings. This will instruct Ranorex to create compressed log (rxzlog) alongside uncompressed log (rxlog). Check this chapter of Ranorex user guide how to do so...
http://www.ranorex.com/support/user-gui ... html#c3024
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

amit_kumar01
Posts: 11
Joined: Tue Dec 23, 2014 1:15 pm

Re: Send Mail Module

Post by amit_kumar01 » Tue Jan 13, 2015 7:35 am

Thanks swissside, The code which you have mentioned is working. Now i am able to get rxzlog file through mail.

Is there any other method to view the test suite report(which we will get in mail after execution) without installing Ranorex on the machine?

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

Re: Send Mail Module

Post by odklizec » Tue Jan 13, 2015 7:47 am

Hi,

If you read my previous posts in this discussion, you will find a way to view the rxzlogs on PC without installing whole Ranorex. You don't even need the viewer to be installed on each PC. Just copy the necessary files onto a shared drive and run the viewer from there.
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

amit_kumar01
Posts: 11
Joined: Tue Dec 23, 2014 1:15 pm

Re: Send Mail Module

Post by amit_kumar01 » Tue Jan 13, 2015 8:37 am

Thanks odklizec, it is working now.

How can we add two receiving mail id in that code? Suppose we need to send that report to 2 or more person after the execution of test suite. The sode which swissside has mentioned is

// This is from address.
"var fromAddress = new MailAddress("[email protected]", "Amit kumar");

//This is to address. In this we need to provide mail id of the receivers.
var toAddress = new MailAddress("[email protected]", "Amit kumar");

Currently i am able to send report to one mentioned email id.

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

Re: Send Mail Module

Post by odklizec » Tue Jan 13, 2015 8:56 am

I believe you need to use something like this...
mail.To.Add(new MailAddress("[email protected]","Your name 1"));
mail.To.Add(new MailAddress("[email protected]","Your name 2"));
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

amit_kumar01
Posts: 11
Joined: Tue Dec 23, 2014 1:15 pm

Re: Send Mail Module

Post by amit_kumar01 » Tue Jan 13, 2015 9:38 am

Hi odklizec,

Its not working. Its displaying some error.

Can we modify the below mentioned code so that after each execution, it could send the report to n users instead of one.

//This is to address. In this we need to provide mail id of the receivers.
var toAddress = new MailAddress("[email protected]", "Amit kumar");

amit_kumar01
Posts: 11
Joined: Tue Dec 23, 2014 1:15 pm

Re: Send Mail Module

Post by amit_kumar01 » Wed Jan 14, 2015 6:51 am

Hi Support/swissside,

Can we modify the below mentioned code so that after each execution, it could send the report to n users instead of one.

The swisside code is working to send only one user. But here we need to send that report to n users.

public void SendMail()
{
try
{

var fromAddress = new MailAddress("[email protected]", "User1");

//Here we need to change so that it could send attached report to the users.
var toAddress = new MailAddress("[email protected]", "User2");

const string subject = "test";
const string body = "test";
string zipFileName = Ranorex.Core.Reporting.TestReport.ReportEnvironment.ReportZipFilePath;
var smtp = new SmtpClient
{
Host = "a.b.c.d",
Port = 25,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,

};

using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})

// adds the zipped report to the mail
if (System.IO.File.Exists(zipFileName))
{
System.Net.Mail.Attachment MyReport = new System.Net.Mail.Attachment(zipFileName);
message.Attachments.Add (MyReport);
smtp.Send(message);
Report.Success("Email has been sent.");
}
}
catch(Exception ex)
{
Report.Failure("Mail Error: " + ex.ToString());
}

With Regards,
Amit kumar

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

Re: Send Mail Module

Post by odklizec » Wed Jan 14, 2015 9:43 am

Hi,

It should be enough to remove this line...

Code: Select all

var toAddress = new MailAddress("[email protected]", "User2"); 
and replace this piece of code...

Code: Select all

using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
}) 
with this...

Code: Select all

              using (var message = new MailMessage()
                       {  
                        From = fromAddress,
                        To.Add(new MailAddress("[email protected]", "name_1")),
                        To.Add(new MailAddress("[email protected]", "name_2")),
                        Subject = subject,  
                        Body = body  
                       })   
From some weird reason, compilation fails for me with error The name 'To' does not exist in the current context (CS0103). But this is strange, because code completion definitely offers "To" when I start typing? Maybe a bug in SharpDevelop 3.x code used by Ranorex?

Anyway, if you get the above error too, simply use this code instead...

Code: Select all

              var message = new MailMessage();
              message.From = fromAddress;
              message.To.Add(new MailAddress("[email protected]", "name_1"));
              message.To.Add(new MailAddress("[email protected]", "name_2"));
              message.Subject = subject;
              message.Body = body;
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

amit_kumar01
Posts: 11
Joined: Tue Dec 23, 2014 1:15 pm

Re: Send Mail Module

Post by amit_kumar01 » Mon Jan 19, 2015 11:52 am

Hi odklizec,

I tried with the code which you had mentioned in the last reply. But none of them are working.

With Regards,
Amit kumar

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

Re: Send Mail Module

Post by odklizec » Mon Jan 19, 2015 12:13 pm

Hi,

Too bad. It works for me OK. It would be useful if you post your solution or errors you are getting. It's hard to help you if there is not enough information about your problem.
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

ilkan.yildirim
Posts: 6
Joined: Wed Jun 24, 2015 3:18 pm

Re: Send Mail Module

Post by ilkan.yildirim » Mon Nov 30, 2015 8:53 am

Hello,

I want to send mail RanorexPDF report as an attachment.

This code works if i give path of pdf file manually. But i want to work with thishttp://www.ranorex.com/blog/ranorex-rep ... conversion link's code.

Code: Select all

// get the zipped report file path  
String PdfFileName = Ranorex.Core.Reporting.TestReport.ReportEnvironment.ReportZipFilePath;  
  
// adds the zipped report to the mail  
if (System.IO.File.Exists(PdfFileName))  
{  
    System.Net.Mail.Attachment MyReport = new System.Net.Mail.Attachment(PdfFileName);  
    mail.Attachments.Add (MyReport);  
}  
So, I've PDF report and mail sender. I could not send pdf file as an attachment. PDF File path is dynamic. I want to call this file name into SendMail.cs code module.

Please Help.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Send Mail Module

Post by Support Team » Wed Dec 02, 2015 12:38 pm

Hello ilkan.yildirim,
“This code works if i give path of pdf file manually. But i want to work with thishttp://www.ranorex.com/blog/ranorex-report-to-pdf-conversion link's code. ”
Unfortunately, I’m not exactly sure to which code you are referring. Maybe you could add further details to your question.

Thank you in advance.

Sincerely,
Robert
.
Image

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Send Mail Module

Post by Support Team » Wed Dec 09, 2015 8:51 pm

Hello ilkan.yildirim & Everyone Else,

If you are attempting to send the .pdf file generated via the "ReportToPdf" module at the end of the test suite, please see example below:
  • 1. There is no need to create a separate class for the to handle the send mail actions. You can simply add the method below to the "ReportToPDF.cs" file. Please make sure to edit the strings "From" and "To"

Code: Select all

public void SendMail()
       	{
                string From = "FromEmail";
        	string To = "ToEmail";
        	string Subject = "TestResult";
        	string Message = "These are the test result";
        	
        	//String below "RepName" identifies the current TestSuite's report name with the extension of pdf
        	string RepName = TestReport.ReportEnvironment.ReportName + ".pdf";
        	string ServerHostname = "YourServerHostName";
        	string ServerPort = "YourServerPort";
        	
            try
            {
                MailMessage mail = new MailMessage(From, To, Subject, Message);
                SmtpClient smtp = new SmtpClient(ServerHostname, int.Parse(ServerPort));        
                
                if(System.IO.File.Exists(RepName))
                {
                	System.Net.Mail.Attachment attachment;
                	attachment = new System.Net.Mail.Attachment(RepName);
                	mail.Attachments.Add(attachment);
                	smtp.Send(mail);
                	Report.Success("Email has been sent to '" + To + "'.");
                }
                else
                {
                	Report.Info("Could not find file: " + RepName);
                }
            }
            catch(Exception ex)
            {
                Report.Failure("Mail Error: " + ex.ToString());
            }
        }
  • 2. Afterward, you can call this method from within the "void ITestModule.Run()" method. Please make sure you do this inside of the "try" section of the delegate section.

Code: Select all

		void ITestModule.Run()
		{
			//Delegate must be registered only once
			if(!registered)
			{
				//PDF will be generated at the very end of the testsuite
				TestSuite.TestSuiteCompleted += delegate {
					
					//Necessary to end the testreport in order to update the duration
					TestReport.EndTestModule();
					
					//Comment out if ConvertReportToPDF() is called directly
					try
					{
						
						Report.LogHtml(ReportLevel.Success,"PDFReport", "Successfully created PDF: <a href='" + ConvertReportToPDF(PDFName, xml, details) + "' target='_blank'>Open PDF</a>");
						
						//Call the SendMail method after the PDF has been created.
						SendMail();
					}
					catch (Exception e)
					{
						Console.BackgroundColor = ConsoleColor.Black;
						Console.ForegroundColor = ConsoleColor.Red;
						Console.WriteLine("ReportToPDF: " + e.Message);
						Console.ResetColor();
						Console.WriteLine("Press any key to continue...");
						Console.ReadKey();
					}
					
					//Delete *.rxzlog if not enabled within test suite settings
					Cleanup();
					
					
				};
				registered = true;
			}
		}
At this point you will receive your email with the PDF attachment after each TestSuite run :)

Thanks,
Support Team - Andy S.
.
Image