sendmail demo

//********************************************************************************
//
//文件名(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 = "qaauto@qq.com";
            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("ctestg@qq.com", "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;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值