你的位置:首页 > 软件开发 > ASP.net > asp.net中邮箱发送

asp.net中邮箱发送

发布时间:2016-02-10 01:00:17
邮箱发送今天终于解决了,从不会到会用了3个晚上才终于解决了,有好多问题都不是代码的问题,而是邮箱的设置上的问题。下面我一一的讲解一下。 1.邮箱发送的原理,我使用图片来解释 左边的user_a@itcast.cn是发送的邮箱(下面我就是用a邮箱指代) ...

    邮箱发送今天终于解决了,从不会到会用了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 get='_blank'>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;      //}    }  }}

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

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

关键词:ASP.NET

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