Send Email

Class library usage, coding and language questions.
Sampath
Posts: 5
Joined: Thu Aug 17, 2017 10:46 pm

Send Email

Post by Sampath » Wed Sep 20, 2017 8:02 pm

Ranorex Version: 7.1.2
- Here I'm trying to send the compressed report to an email. The code below was put inside program.cs at the end before return error;
Can somebody help me with this?
we use office365 here, so should I use smtp.office365.com or smtp.mycompany.com??
During debug too didn't find any error. everything was in place.
Thanks in advance

*BEGINNER

Code: Select all

     try
		   {
				MailMessage mail = new MailMessage();
				
				mail.From = new MailAddress("[email protected]");
				mail.To.Add("[email protected]");
				mail.Subject = "Test Results";
				
				SmtpClient SmtpServer = new SmtpClient("smtp.mycompany.com");			
				SmtpServer.Port = 25;
				SmtpServer.UseDefaultCredentials = false;
				SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
				SmtpServer.EnableSsl = true;
				SmtpServer.Timeout = 5000;
				
				String zipFileName = Ranorex.Core.Reporting.TestReport.ReportEnvironment.ReportZipFilePath;
				
				if (System.IO.File.Exists(zipFileName))
				{
					System.Net.Mail.Attachment MyReport = new System.Net.Mail.Attachment(zipFileName);
					mail.Attachments.Add (MyReport);
				}
				
				SmtpServer.Send(mail );    //--------If I use SendAsync(mail, mail), It does pass the test but doesn't send an email. 
                                                                         // This is where the error message is pointed out. 
				Report.Info("Email sent");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
				Report.Error(ex.StackTrace);
			}
Error Message:
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)

Sampath
Posts: 5
Joined: Thu Aug 17, 2017 10:46 pm

Re: Send Email

Post by Sampath » Thu Sep 21, 2017 3:14 pm

I was able to fix it.
With Ranorex v7.2.0 (Ranorex Automation Helpers) It is easy to use Sendmail, and convert to pdf