zoukankan      html  css  js  c++  java
  • C# 使用SmtpClient发送Email

    使用Winfrom写的报错信息发送邮件通知。

    以下主要代码

     1         /// <summary>
     2         /// 发送邮件核心代码
     3         /// </summary>
     4         /// <param name="oFFromMail">发送邮箱全地址</param>
     5         /// <param name="oFFromMailPwd">发送邮箱</param>
     6         /// <param name="oFDisName">与邮箱关联的关联显示名</param>
     7         /// <param name="toMail">收件人邮箱全地址</param>
     8         /// <param name="oFSubject">邮件标题</param>
     9         /// <param name="oFBody">邮件内容</param>
    10         /// <returns>返回是否发送成功</returns>
    11         private void sendMailMethod(string oFBody)
    12         {
    13             try
    14             {
    15                 string oFFromMail = config.AppSettings.Settings["Email"].Value;//123456@qq.com
    16                 string oFFromMailPwd = config.AppSettings.Settings["Psd"].Value;//QQ密码
    17                 string oFDisName = "邮件发送服务";
    18                 string toMail = config.AppSettings.Settings["ToEmail"].Value;//接受的邮件地址 123321@qq.com
    19                 string host = config.AppSettings.Settings["Host"].Value;//QQ是smtp.qq.com;其他网上百度
    20 
    21                 //以下设置服务器
    22                 MailMessage mailmsg = new MailMessage();
    23                 mailmsg.From = new MailAddress(oFFromMail, oFDisName);
    24                 mailmsg.To.Add(toMail);//"bad.huan@163.com"
    25                 mailmsg.Subject = "商品上架功能异常邮件";
    26                 mailmsg.Body = "您好啊,这是测试邮件!<br />" + oFBody;
    27                 mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
    28                 mailmsg.IsBodyHtml = true;
    29 
    30                 SmtpClient sendmsg = new SmtpClient(host);
    31                 sendmsg.Host = host;
    32                 33                 34                 
    35 
    36                 sendmsg.Credentials = new NetworkCredential(oFFromMail, oFFromMailPwd);
    37                 sendmsg.EnableSsl = true;
    38                 sendmsg.Send(mailmsg);
    39                 mailmsg.Dispose();
    40             }
    41             catch (Exception ex)
    42             {
    43                 throw new Exception(ex.Message, ex);
    44             }
    45         }

    以上就是我刚验证过的代码,有问题可向我提问哦

  • 相关阅读:
    Lua 随机数生成问题
    关于LUA中的随机数问题
    用setup.py安装第三方包packages
    问题解决 -------- 解决YUM下Loaded plugins: fastestmirror Determining fastest mirrors 的问题
    CentOS6.7上安装Mysql5.7
    Centos6.8通过yum安装mysql5.7
    Gcc ------ gcc的使用简介与命令行参数说明
    redis 常用命令
    redis学习系列
    C# Redis实战
  • 原文地址:https://www.cnblogs.com/lindajia/p/6972601.html
Copyright © 2011-2022 走看看