//********************************************************************************
//
//文件名(File Name): SendMail.cs
//功能描述(Description): 该类实现了发送邮件功能,测试完成以后,把测试结果通过发信的方式通知给相关人员。
//作 者(Author): Cary Cao
//日 期(Create Date): 2015-10-20
//修改记录(Revision History): NO
//
//********************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
using System.Collections;
using System.IO;
using System.Data;
using System.Reflection;
namespace NetBrain.Common.Utility
{
public class SendMail
{
public static void MailSending(string testURL, string title)
{
string from = "
[email protected]";
string fromer = "QA Automation";
string Subject = title + "-" + DateTime.Today.ToShortDateString();
string Body = PrepareHtmlBody(testURL);
string SMTPHost = "10.10.10.8";
int SMTPPort = 587;
string SMTPuser = "qaauto";
string SMTPpass = "123@qwe";
sendmail(from, fromer, Subject, Body, SMTPHost, SMTPPort, SMTPuser, SMTPpass);
}
public static bool sendmail(string sfrom, string sfromer, string sSubject, string sBody,string sSMTPHost, int sSMTPPort, string sSMTPuser, string sSMTPpass)
{
创建一个MailMessage对象
MailMessage oMail = new MailMessage();
设置from地址
MailAddress from = new MailAddress(sfrom, sfromer);
oMail.From = from;
设置from地址
MailAddress to1 = new MailAddress("
[email protected]", "test Cao");
oMail.To.Add(to1);
邮件标题
oMail.Subject = sSubject;
邮件内容
oMail.Body = sBody;
邮件格式
oMail.IsBodyHtml = true;
邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
设置邮件的优先级为高
//oMail.Priority = MailPriority.High;
发送邮件
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = sSMTPHost;
client.Port = sSMTPPort;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
try
{
client.Send(oMail);
return true;
}
catch
{
return false;
throw NBException.New(ErrorCode.SendMailFailed, "Send Mail Failed");
}
finally
{
oMail.Dispose();
}
}
public static string PrepareHtmlBody(string testURL)
{
string bodystr = string.Empty;
string MachineName = Environment.GetEnvironmentVariable("ComputerName");
string TestCompleteTime = DateTime.Now.ToShortDateString();
bodystr = string.Format(MailBodyFormat(), testURL, MachineName, TestCompleteTime, TestCompleteTime, @"http://10.10.5.8:8080/");
return bodystr.ToString();
}
private static string MailBodyFormat()
{
string htmlbody = string.Empty;
Assembly currAssembly = Assembly.GetExecutingAssembly();
Stream stream = currAssembly.GetManifestResourceStream("Netbrain.Common.Utility.MailContents.html");
StreamReader reader = new StreamReader(stream);
htmlbody = reader.ReadToEnd();
return htmlbody;
}
private static int GetCount(string status, DataTable dt)
{
int count = 0;
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
try
{
if (row["Status"].ToString() == status)
{
count += 1;
}
}
catch { }
}
}
return count;
}
}
}