• 邮件发送很难么?


    1.创建控制台程序:

    2.添加一个与发送邮件相关的实体类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SendEmail
    {
        /// <summary>
        /// 邮件发送实体类
        /// </summary>
        public class EmailSend
        {
            /// <summary>
            /// 邮件服务器
            /// </summary>
            public string  smtpServer { get; set; }
            /// <summary>
            /// 发件邮箱
            /// </summary>
            public string sendEmail { get; set; }
            /// <summary>
            /// 收件箱
            /// </summary>
            public string dstEmail { get; set; }
            /// <summary>
            /// 授权码
            /// </summary>
            public sbyte pwd { get; set; }
            /// <summary>
            /// 抄送邮箱
            /// </summary>
            public string ccEmail { get; set; }
            /// <summary>
            /// 密送邮箱
            /// </summary>
            public string bccEmail { get; set; }
            /// <summary>
            /// 邮件主题(标题)
            /// </summary>
            public string theme { get; set; }
            /// <summary>
            /// 邮件内容
            /// </summary>
            public string content { get; set; }
        }
    }

    3.添加一个配置文件App.config,将相关内容配置到里面:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <!--邮件服务器-->
            <add key="smtpServer" value=""/>
            <!--发件邮箱-->
            <add key="sendEmail"  value=""/>
            <!--收件邮箱-->
            <add key="dstEmail"  value=""/>
            <!--客户端授权码-->
            <add key="clientPwd"  value=""/>
            <!--抄送邮箱-->
            <add key="ccEmail"  value=""/>
            <!--密送邮箱-->
            <add key="bccEmail"  value=""/>
            <!--设置定时器间隔时间(秒数)-->
            <add key="internal"/>
        </appSettings>
    </configuration>

    4.程序入口:

    引入命名空间:

    using System.Net.Mail;
    using System.Net;

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Timers;
    using System.Net.Mail;
    using System.Net;
    
    namespace SendEmailDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建一个邮件实体对象
                EmailSend email = new EmailSend();
                //从配置文件取出相关的值并对邮件实体对象赋值
    
                //获取邮件服务器
                email.smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpserver"];
                //获取发件箱
                email.sendEmail = System.Configuration.ConfigurationManager.AppSettings["sendEmail"];
                //获取收件箱
                email.dstEmail = System.Configuration.ConfigurationManager.AppSettings["dstEmail"];
                //获取客户端授权码
                email.clientPwd = System.Configuration.ConfigurationManager.AppSettings["clientPwd"];
                //获取抄送邮箱
                email.ccEmail = System.Configuration.ConfigurationManager.AppSettings["ccEmail"];
                //获取密送邮箱
                email.bccEmail = System.Configuration.ConfigurationManager.AppSettings["bccEmail"];
    
    
                email.theme = "C#邮件发送测试";
                email.content = "邮件测试内容";
    
                string msg=SendEmail(email);
                Console.WriteLine(msg);
                Console.ReadKey();
                
    
            }
    
    
    
            public static  string SendEmail(EmailSend _mailSend)
            {
                try
                {
                    MailMessage mail = new MailMessage();
    
                    //邮件服务器客户端对象
                    SmtpClient client = new SmtpClient();
                    //设置邮件服务器
                    client.Host = _mailSend.smtpServer;
                    //设置端口
                    client.Port = 25;
                    //是否使用安全套接字层加密连接
                    client.EnableSsl = false;
                    //不使用默认凭证,注意此句必须放在 client.Credentials 的前面
                    client.UseDefaultCredentials = false;
                 
                    client.Credentials = new NetworkCredential(_mailSend.sendEmail, _mailSend.clientPwd);
    
                    //邮件通过网络直接发送到服务器
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    
                    //账号信息
                    mail.From = new MailAddress(_mailSend.sendEmail);
    
                    mail.To.Clear();
    
                    //添加收件箱
                    foreach (var item in _mailSend.dstEmail.Split(';'))
                    {
                        if (string.IsNullOrEmpty(item))
                        {
                            continue;
                        }
                        mail.To.Add(item);
                    }
    
                    //添加抄送
                    if (!string.IsNullOrEmpty(_mailSend.ccEmail))
                    {
                        foreach (var item in _mailSend.ccEmail.Split(';'))
                        {
                            if (string.IsNullOrEmpty(item))
                            {
                                continue;
                            }
                            mail.CC.Add(new MailAddress(item));
                        }
                    }
    
                    //添加密送
                    if (!string.IsNullOrEmpty(_mailSend.bccEmail))
                    {
                        foreach (var item in _mailSend.bccEmail.Split(';'))
                        {
                            if (string.IsNullOrEmpty(item))
                            {
                                continue;
                            }
                            mail.Bcc.Add(new MailAddress(item));
                        }
                    }
    
                    //设置主题
                    mail.Subject = _mailSend.theme;
                    //设置内容
                    mail.Body = _mailSend.content;
                    //设置编码
                    mail.SubjectEncoding = Encoding.UTF8;
                   
    
                    //发送邮件
                    client.Send(mail);
    
                    return "邮件发送成功!!";
                }
                catch (Exception ex)
                {
    
                    return"邮件发送异常,捕获异常:"+ex.Message;
                }
            }
    
    
        }
    }

     5.未将邮箱的客户端授权开启时,会出现如下异常:

    6.浏览器登录邮箱-->设置-->客户端授权密码-->点击开启-->输入验证码-->确定

    7.设置授权码

    8.设置成功,注:此授权码为客户端发送邮件所必须的

    9.再次发送邮件:

    10.浏览器登录邮箱查看:

    成功!!

    11.官网参考地址:https://msdn.microsoft.com/zh-cn/library/system.net.mail.mailmessage(VS.80).aspx

  • 相关阅读:
    出版文字作品报酬规定(收藏)
    关于Delphi7 的XML说明
    我做的XML验证的测试记录
    印刷常用名词
    验证XLM数据合法性(收藏)
    关于.Net操作XML相关类
    我的性格
    webpack学习笔记一
    汇编语言内存变量的地址
    Linux 汇编语言(GNU GAS汇编)开发指南
  • 原文地址:https://www.cnblogs.com/jiangxianshen/p/7495111.html
Copyright © 2020-2023  润新知