.net C# 使用网易163邮箱搭建smtp服务,实现发送邮件功能

发布于:2024-07-11 ⋅ 阅读:(73) ⋅ 点赞:(0)

功能描述:使用邮箱验证实现用户注册激活和找回密码。邮箱选择网易163作为smtp服务器。

真实测试情况:第一种:大部分服务器运行商的25端口默认是封禁的,可以联系运营商进行25端口解封,解封之后可以使用25端口。第二种:使用465、587加密端口,经测试587端口发送失败返回“无法从传输连接中读取数据”,所以这里只做465端口的配置。下面就是两种成功案例的代码请同学根据实际情况选择。

从网上查阅信息了解到使用阿里和qq邮箱的587端口也发送失败,所以目前知道网易、阿里、qq三大运营商的stmp服务587端口都失败。

第一步:开启smtp服务。登录网易邮箱【设置】-左侧菜单找到【POP3/SMTP/IMAP】,开启服务和获取授权密码(注意:代码中的密码使用授权密码)。如图设置

第二步:设置服务器防火墙入栈规则添加465端口。

第三步:代码。

Send25是25端口测试可以发送邮件的方法。使用的类库System.Net.Mail

public void Send25()
        {

//this.SmtpHost //指定 smtp 服务器地址

//this.SmtpPort //smtp 服务器的端口

//this.FromEmailAddress //发送邮件的邮箱地址

//this.FormEmailPassword //发送邮件的邮箱密码,这里使用授权码

//this.ToList //接收邮件的邮箱地址,可以是集合逗号分隔


            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;//可以去掉
            SmtpClient smtp = new SmtpClient();                 //实例化一个SmtpClient
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //将smtp的出站方式设为 Network
            smtp.EnableSsl = false;                             //smtp服务器是否启用SSL加密
            smtp.Host = this.SmtpHost;                          //指定 smtp 服务器地址
            smtp.Port = this.SmtpPort;                          //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
            smtp.UseDefaultCredentials = true;                  //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
            //smtp.EnableSsl = false;
            smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword);    //如果需要认证,则用下面的方式

            MailMessage mm = new MailMessage(); //实例化一个邮件类
            mm.Priority = MailPriority.Normal; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
            mm.From = new MailAddress(this.FromEmailAddress, "管理员", Encoding.GetEncoding(936));

            //收件人
            if (!string.IsNullOrEmpty(this.ToList))
                mm.To.Add(this.ToList);
            //抄送人
            if (!string.IsNullOrEmpty(this.CCList))
                mm.CC.Add(this.CCList);
            //密送人
            if (!string.IsNullOrEmpty(this.BccList))
                mm.Bcc.Add(this.BccList);

            mm.Subject = this.Subject;                      //邮件标题
            mm.SubjectEncoding = Encoding.GetEncoding(936); //这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
            mm.IsBodyHtml = this.IsBodyHtml;                //邮件正文是否是HTML格式
            mm.BodyEncoding = Encoding.GetEncoding(936);    //邮件正文的编码, 设置不正确, 接收者会收到乱码
            mm.Body = this.Body;                            //邮件正文
                                                            //邮件附件
            if (this.AttachmentList != null && this.AttachmentList.Count > 0)
            {
                foreach (Attachment attachment in this.AttachmentList)
                {
                    mm.Attachments.Add(attachment);
                }
            }
            smtp.Send(mm);

        }

Send465是465端口测试可以发送邮件的方法。使用的类库System.Web.Mail

public bool Send465()
        {
            // 邮件主题
            System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
            //邮件主题
            mmsg.Subject = "主题";
            mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
            //邮件正文
            mmsg.Body = “正文”;

//正文编码
            mmsg.BodyEncoding = Encoding.UTF8;
            //优先级
            mmsg.Priority = System.Web.Mail.MailPriority.Normal;
            //发件者邮箱地址
            mmsg.From = Email;
            //收件人收箱地址
            mmsg.To = email;

            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //用户名
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Email);
            //密码(授权码)
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Emailpwd);
            //端口
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Port);
            //是否ssl
            mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl",true);
            //Smtp服务器
            System.Web.Mail.SmtpMail.SmtpServer = Host;
            try
            {
                System.Web.Mail.SmtpMail.Send(mmsg);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }