zoukankan      html  css  js  c++  java
  • C#通过SMTP发送邮件代码示例

    640?wx_fmt=jpeg

    1、新建SMTP.cs类库文件

    public class SMTP

        {

            /// <summary>

            /// SMTP服务器

            /// </summary>

            public string smtp { get; set; }

            /// <summary>

            /// SMTP服务器端口

            /// </summary>

            public int port { get; set; }

            /// <summary>

            /// 发件人

            /// </summary>

            public string from { get; set; }

            /// <summary>

            /// 发件人密码

            /// </summary>

            public string password { get; set; }

            /// <summary>

            /// 邮件主题

            /// </summary>

            public string subject { get; set; }

            /// <summary>

            /// 邮件主题

            /// </summary>

            public string body { get; set; }

            /// <summary>

            /// 收件人邮箱

            /// </summary>

            public string strto { get; set; }

            /// <summary>

            /// 抄送邮箱

            /// </summary>

            public List<string>    strcc=new List<string>();

            /// <summary>

            /// 发送邮件

            /// </summary>

            public void SendMail()

            {

                SmtpClient client = new SmtpClient();

                client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式    

                client.Host = this.smtp;//邮件服务器

                client.UseDefaultCredentials = false;

                client.EnableSsl = true;

                client.Credentials = new System.Net.NetworkCredential(this.from, this.password);//用户名、密码

                client.Port = this.port;

                //client.EnableSsl = true;

            

                var msg = new MailMessage();

                msg.From = new MailAddress(this.from);

                msg.To.Add(strto);

                if (this.strcc!=null&& this.strcc.Count>0 )

                {

                    foreach (string OneStrcc in strcc)

                    {

                        msg.CC.Add(OneStrcc);

                    }

                }

                

                msg.Subject = subject;//邮件标题   

                msg.Body = body;//邮件内容   

                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码   

                msg.IsBodyHtml = true;//是否是HTML邮件   

                msg.Priority = MailPriority.High;//邮件优先级   


                try

                {

                    client.Send(msg);

                }

                catch (SmtpException ex)

                {

                    throw ex;

                }

            }

        }

    2、使用示例

    try

            {

                var theSMTP = new SMTP

                {

                    smtp = ConfigurationManager.AppSettings["smtp"],

                    port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]),

                    from = ConfigurationManager.AppSettings["from"],

                    password = ConfigurationManager.AppSettings["password"],

                    subject = "主题",

                    body = "内容"

                };

                theSMTP.strto ="xxx@qq.com";

                theSMTP.strcc.Add("xxx@qq.com");

                theSMTP.SendMail();

            }

            catch (Exception ex)

            {


            }


  • 相关阅读:
    背景图片拉伸显示CSS
    可序列化对象和byte[]数组之间的互转
    简单web性能测试工具——ab命令(ApacheBench)
    测试人员必须掌握的linu常用命令
    robot framework 如何处理循环条件下面的变量自增
    robot framework 的关键字Continue For Loop 用法
    robot framework 的AutoItLibrary常用关键字
    robot framework 关键字Switch Browser和Select Window的区别
    robot framework 接口测试 http协议post请求json格式
    robot framework 怎么验证搜索无记录,页面元素不存在
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351381.html
Copyright © 2011-2022 走看看