zoukankan      html  css  js  c++  java
  • c#发送邮件

        /// <summary>
            /// 邮件发送
            /// </summary>
            /// <param name="strTo">收信对象邮箱</param>
            /// <param name="strSubject">邮件主题</param>
            /// <param name="strBody">邮件内容</param>
            public  bool SendEmail(string strTo, string strSubject, string strBody)
            {
                //string strSmtpServer = "smtp.163.com"; //163邮件服务器
                string strSmtpServer = "smtp.qq.com"; //qq邮件服务器
                string strFrom = "用户邮箱"; //用户邮箱
                string strFromPass = "用户密码";//用户密码

                //string strSmtpServer = "202.108.3.190"; //新浪邮件服务器
                SmtpClient client = new SmtpClient(strSmtpServer);//创建邮箱服务器对象          

                client.UseDefaultCredentials = false;//获取或设置是否使用默认凭据访问 Web 代理服务器
                client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);//创建用户对象
                client.DeliveryMethod = SmtpDeliveryMethod.Network;//投递方式

                MailMessage message = new MailMessage();    //创建邮件对象
                message.From = new MailAddress(strFrom, "施强留学网");    //发信人地址
                message.To.Add(strTo);                      //添加收信人地址
                message.Subject = strSubject;               //邮件主题
                message.Body = strBody;                     //邮件内容

                //当然,你也把上面五条语句简化为如下:
                //MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
                //添加附件,要注意的是,发送附件可能会慢点,耐心等下!
                //Attachment attachment = new Attachment("e://a.xls");   //创建附件对象,括号内参数为要添加的附件所在的地址
                // message.Attachments.Add(attachment);                      //添加到邮件

                message.BodyEncoding = System.Text.Encoding.UTF8;//获取或设置用于邮件正文的编码

                message.IsBodyHtml = true;//取得或设定值,指出电子邮件的主体是否为 HTML
                if (!string.IsNullOrEmpty(strBody))//判断邮件内容是否为空
                {
                    try
                    {
                        client.Send(message);//发送
                        return true;
                    }
                    catch (Exception ex)
                    {

                        return false;
                    }
                }
                else
                {

                    return false;
                }
            }

  • 相关阅读:
    Vue(小案例_vue+axios仿手机app)_go实现退回上一个路由
    nyoj 635 Oh, my goddess
    nyoj 587 blockhouses
    nyoj 483 Nightmare
    nyoj 592 spiral grid
    nyoj 927 The partial sum problem
    nyoj 523 亡命逃窜
    nyoj 929 密码宝盒
    nyoj 999 师傅又被妖怪抓走了
    nyoj 293 Sticks
  • 原文地址:https://www.cnblogs.com/tongdengquan/p/6090587.html
Copyright © 2011-2022 走看看