星空网 > 软件开发 > ASP.net

asp.net中邮箱发送

    邮箱发送今天终于解决了,从不会到会用了3个晚上才终于解决了,有好多问题都不是代码的问题,而是邮箱的设置上的问题。下面我一一的讲解一下。

   1.邮箱发送的原理,我使用图片来解释

    asp.net中邮箱发送

左边的user_a@itcast.cn是发送的邮箱(下面我就是用a邮箱指代),右边的user_b@itheima.com是接收的邮箱(下面我就是用b邮箱指代)。

1)、邮箱a发送到他自己的smtp服务器上,如:邮箱a是outlook上注册的邮箱,那么邮箱a的邮件就发送到outlook上的smtp服务器上

2)、通过smtp服务器的通讯规则,进行传送到邮箱b的smtp服务器上,有smtp服务器再发送到存储设备上,再发送到pop3服务器上,最后发给邮箱b

注:最主要的是1),在这里主要讲解发送。

2.下面是发送邮件的类

using System;using System.Linq;using System.Net.Mail;using System.Text;namespace Micua.Infrastructure.Utility{  /// <summary>  /// 邮件发送助手类  /// </summary>  /// <remarks>  /// 2013-11-18 18:56 Created By iceStone  /// </remarks>  public static class MailHelper  {    private readonly static string SmtpServer = "smtp的服务器地址"; //smtp.wedn.net    private readonly static int SmtpServerPort = 25;    private readonly static bool SmtpEnableSsl = false;    private readonly static string SmtpUsername = "发送的邮箱";     private readonly static string SmtpDisplayName = "测试邮箱123";    private readonly static string SmtpPassword = "授权码的位置";      /// <summary>    /// 发送邮件到指定收件人    /// </summary>    /// <remarks>    /// 2013-11-18 18:55 Created By iceStone    /// </remarks>    /// <param name="to">收件人地址</param>    /// <param name="subject">主题</param>    /// <param name="mailBody">正文内容(支持HTML)</param>    /// <param name="copyTos">抄送地址列表</param>    /// <returns>是否发送成功</returns>    public static bool Send(string to, string subject, string mailBody, params string[] copyTos)    {      return Send(new[] { to }, subject, mailBody, copyTos, new string[] { }, MailPriority.Normal);    }    /// <summary>    /// 发送邮件到指定收件人    /// </summary>    /// <remarks>    /// 2013-11-18 18:55 Created By iceStone    /// </remarks>    /// <param name="tos">收件人地址列表</param>    /// <param name="subject">主题</param>    /// <param name="mailBody">正文内容(支持HTML)</param>    /// <param name="ccs">抄送地址列表</param>    /// <param name="bccs">密件抄送地址列表</param>    /// <param name="priority">此邮件的优先级</param>    /// <param name="attachments">附件列表</param>    /// <returns>是否发送成功</returns>    /// <exception cref="System.ArgumentNullException">attachments</exception>    public static bool Send(string[] tos, string subject, string mailBody, string[] ccs, string[] bccs, MailPriority priority, params Attachment[] attachments)    {      if (attachments == null) throw new ArgumentNullException("attachments");      if (tos.Length == 0) return false;      //创建Email实体      var message = new MailMessage();      message.From = new MailAddress(SmtpUsername, SmtpDisplayName);      message.Subject = subject;      message.Body = mailBody;      message.BodyEncoding = Encoding.UTF8;      message.IsBodyHtml = true;      message.Priority = priority;      //插入附件      foreach (var attachment in attachments)      {        message.Attachments.Add(attachment);      }      //插入收件人地址,抄送地址和密件抄送地址      foreach (var to in tos.Where(c => !string.IsNullOrEmpty(c)))      {        message.To.Add(new MailAddress(to));      }      foreach (var cc in ccs.Where(c => !string.IsNullOrEmpty(c)))      {        message.CC.Add(new MailAddress(cc));      }      foreach (var bcc in bccs.Where(c => !string.IsNullOrEmpty(c)))      {        message.CC.Add(new MailAddress(bcc));      }      //创建SMTP客户端      var client = new SmtpClient      {        Host = SmtpServer,        Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword),        DeliveryMethod = SmtpDeliveryMethod.Network,        EnableSsl = SmtpEnableSsl,        Port = SmtpServerPort      };      //client.SendCompleted += Client_SendCompleted;      //try      //{      //发送邮件      client.Send(message);      //client.SendAsync(message,DateTime.Now.ToString());      //client.Dispose();      //message.Dispose();      return true;      //}      //catch (Exception)      //{      //  throw;      //}    }  }}

主要是改前面的几个私有静态变量,下面我仔细讲解一下,我以网易邮箱为例,自己随便编个邮箱(ceshi@163.com,授权码:ceshi123)

private readonly static string SmtpServer = "smtp的服务器地址"; 
填写smtp的地址,如:网易的smtp服务器地址是smtp.163.comprivate readonly static int SmtpServerPort = 25;
这个不用改,这个是端口号private readonly static bool SmtpEnableSsl = false;
这个不用改,如果设置为true的话,上面的端口号就要改,改成好像是465,这个我不确定private readonly static string SmtpUsername = "发送的邮箱";
发送的邮箱,如:网易的邮箱ceshi@163.comprivate readonly static string SmtpDisplayName = "测试邮箱123";
这个地方自己可以随便设置一下,看看是什么效果private readonly static string SmtpPassword = "授权码的位置";
这个最重要,我最后花了大量的时间就在这上面,授权码就是第三方客户端登录时需要输入的,首先就需要在邮箱里进行设置,这个和代码就没有什么关系了,下面我重点讲解。

最后在调用这个类就可以了。

3.如何有邮箱的授权码

  比如网易邮箱:你可以百度一下,也可以按照我的大致思路进行设置,不同的邮箱有不同的设置,基本上就是登录网页邮箱,点击设置,找到有关于smtp服务的设置,就可以了。

注:qq邮箱的我进行设置,但是不行,我使用的是网易的可以,不知道别的行不行,大家可以试一下,有什么不懂得可以给我留言,如果大家知道如何设置qq邮箱的,可以给我说一下,让我也学习一下,谢谢大家了。希望这篇文章对你有所帮助。




原标题:asp.net中邮箱发送

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Wall Kandy:https://www.goluckyvip.com/tag/27124.html
Wallapop:https://www.goluckyvip.com/tag/27125.html
Wallyt:https://www.goluckyvip.com/tag/27126.html
Walmart Go:https://www.goluckyvip.com/tag/27128.html
Walmart Luminate:https://www.goluckyvip.com/tag/27129.html
刷单原因:https://www.goluckyvip.com/tag/2713.html
37号文今后是否会更新?一文详解关键信息 :https://www.kjdsnews.com/a/1836441.html
探讨内地人开设香港账户的可行性 :https://www.kjdsnews.com/a/1836442.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流