栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#发送邮箱实现代码

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#发送邮箱实现代码

之前自己从来没有做过发送邮箱的功能,前段时间项目需要,在找了很多帖子之后,终于实现了。

之后有整理了一下,写了一个类。直接给类传递信息,就可以发送了。

这里还需要说明的是,发送邮箱需要开通POP3/SMTP服务,否则QQ邮箱,网易邮箱等会报错。接收的邮箱就不用开通啦,开通方法百度一下就知道啦。

public static class EmailHelper
  {
    /// 
    /// 发送邮件
    /// 
    /// 邮件主题
    /// 邮件内容
    /// 附件地址,如果不添加附件传null或""
    /// 发送人邮箱地址
    /// 发送人邮箱密码
    /// 接收人邮箱
    public static void SendMail(string subject, string msg, string filePath, string senderEmail, string senderPwd, params string[] recipientEmail)
    {
      if (!CheckIsNotEmptyOrNull(subject, msg, senderEmail, senderPwd) || recipientEmail == null || recipientEmail.Length == 0)
      {
 throw new Exception("输入信息无效");
      }
      try
      {
 string[] sendFromUser = senderEmail.Split('@');

 //构造一个Email的Message对象
 MailMessage message = new MailMessage();

 //确定smtp服务器地址。实例化一个Smtp客户端
 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp." + sendFromUser[1]);

 //构造发件人地址对象
 message.From = new MailAddress(senderEmail, sendFromUser[0], Encoding.UTF8);

 //构造收件人地址对象
 foreach (string userName in recipientEmail)
 {
   message.To.Add(new MailAddress(userName, userName.Split('@')[0], Encoding.UTF8));
 }

 if (!string.IsNullOrEmpty(filePath))
 {
   Attachment attach = new Attachment(filePath);
   //得到文件的信息
   ContentDisposition disposition = attach.ContentDisposition;
   disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
   disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
   disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
   //向邮件添加附件
   message.Attachments.Add(attach);
 }

 //添加邮件主题和内容
 message.Subject = subject;
 message.SubjectEncoding = Encoding.UTF8;
 message.Body = msg;
 message.BodyEncoding = Encoding.UTF8;

 //设置邮件的信息
 client.DeliveryMethod = SmtpDeliveryMethod.Network;
 message.BodyEncoding = System.Text.Encoding.UTF8;
 message.IsBodyHtml = false;

 //如果服务器支持安全连接,则将安全连接设为true。
 //gmail,qq支持,163不支持
 switch (sendFromUser[1])
 {
   case "gmail.com":
   case "qq.com":
     client.EnableSsl = true;
     break;
   default:
     client.EnableSsl = false;
     break;
 }

 //设置用户名和密码。
 client.UseDefaultCredentials = false;
 //用户登陆信息
 NetworkCredential myCredentials = new NetworkCredential(senderEmail, senderPwd);
 client.Credentials = myCredentials;
 //发送邮件
 client.Send(message);
      }
      catch (Exception ex)
      {
 throw (ex);
      }
    }

    /// 
    /// 验证所有传入字符串不能为空或null
    /// 
    /// 参数列表
    /// 都不为空或null返回true,否则返回false
    public static bool CheckIsNotEmptyOrNull(params string[] ps)
    {
      if (ps != null)
      {
 foreach (string item in ps)
 {
   if (string.IsNullOrEmpty(item)) return false;
 }
 return true;
      }
      return false;
    }
  }

直接调用方法,传递需要发送的信息,就可以发送邮箱了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号