欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#实现SMTP邮件发送程序实例

时间:2021-03-03 10:08:16|栏目:.NET代码|点击:

通常来说邮件发送功能在网站应用程序中经常会用到,包括大家经常看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,本文展示了一个客户端Demo,希望对有需要的朋友有所帮助。运行效果如下图所示:

核心代码如下:

复制代码 代码如下:
using System;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace HC.Email
{
    /// <summary>
    ///     整站邮件服务类
    /// </summary>
    public class EmailService
    {
        /// <summary>
        ///     发送邮件
        /// </summary>
        /// <param name="mailTo">收件人
        /// <param name="subject">主题
        /// <param name="body">内容
        /// <returns></returns>
        public static bool Send(string mailTo, string subject, string body)
        {
            return Send(new[] {mailTo}, null, subject, body, true, null);
        }
        /// <summary>
        ///     发送邮件
        /// </summary>
        /// <param name="mailTo">收件人
        /// <param name="subject">主题
        /// <param name="body">内容
        /// <returns></returns>
        public static bool Send(string[] mailTo, string subject, string body)
        {
            return Send(mailTo, null, subject, body, true, null);
        }
 
        /// <summary>
        ///     发送邮件
        /// </summary>
        /// <param name="mailTo">收件人
        /// <param name="subject">主题
        /// <param name="body">内容
        /// <param name="attachmentsPath">附件
        /// <returns></returns>
        public static bool Send(string[] mailTo, string subject, string body, string[] attachmentsPath)
        {
            return Send(mailTo, null, subject, body, true, attachmentsPath);
        }
 
        /// <summary>
        ///     发送邮件
        /// </summary>
        /// <param name="mailTo">收件人
        /// <param name="mailCcArray">抄送
        /// <param name="subject">主题
        /// <param name="body">内容
        /// <param name="isBodyHtml">是否Html
        /// <param name="attachmentsPath">附件
        /// <returns></returns>
        public static bool Send(string[] mailTo, string[] mailCcArray, string subject,string body, bool isBodyHtml,
                                string[] attachmentsPath)
        {
            try
            {
                var config = ConfigHelper.GetConfig<emailconfig>();
                if (string.IsNullOrEmpty(config.Host) ||string.IsNullOrEmpty(config.UserName) ||
                    string.IsNullOrEmpty(config.Port) ||string.IsNullOrEmpty(config.Password))
                {
                    //todo:记录日志
                    return false;
                }
                var @from = new MailAddress(config.MailFrom); //使用指定的邮件地址初始化MailAddress实例
                var message = new MailMessage(); //初始化MailMessage实例
                //向收件人地址集合添加邮件地址
                if (mailTo != null)
                {
                    foreach (string t in mailTo)
                    {
                        message.To.Add(t);
                    }
                }
 
                //向抄送收件人地址集合添加邮件地址
                if (mailCcArray != null)
                {
                    foreach (string t in mailCcArray)
                    {
                        message.CC.Add(t);
                    }
                }
                //发件人地址
                message.From = @from;
 
                //电子邮件的标题
                message.Subject = subject;
 
                //电子邮件的主题内容使用的编码
                message.SubjectEncoding = Encoding.UTF8;
 
                //电子邮件正文
                message.Body = body;
 
                //电子邮件正文的编码
                message.BodyEncoding = Encoding.Default;
                message.Priority = MailPriority.High;
                message.IsBodyHtml = isBodyHtml;
 
                //在有附件的情况下添加附件
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    foreach (string path in attachmentsPath)
                    {
                        var attachFile = new Attachment(path);
                        message.Attachments.Add(attachFile);
                    }
                }
                try
                {
                    var smtp = new SmtpClient
                        {
                            Credentials = new NetworkCredential(config.UserName, config.Password),
                            Host = config.Host,
                            Port = Convert.ToInt32(config.Port)
                        };
 
                    //将邮件发送到SMTP邮件服务器
                    smtp.Send(message);
                    //todo:记录日志
                    return true;
                }
                catch (SmtpException ex)
                {
                    //todo:记录日志
                    return false;
                }
            }
            catch (SmtpException ex)
            {
                //todo:记录日志
                return false;
            }
        }
    }
}

完整实例代码点击此处本站下载

希望本文所述对大家的C#程序设计有所帮助

上一篇:asp.net 生成静态页笔记

栏    目:.NET代码

下一篇:c# 将Datatable数据导出到Excel表格中

本文标题:C#实现SMTP邮件发送程序实例

本文地址:http://www.codeinn.net/misctech/73428.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有