• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#.NET发送邮件的实例代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace MyQuery.Utils
{
    /// <summary>
    /// 封装邮件处理
    /// by 贾世义 2011-6-3
    /// </summary>
    public static class MailHelper
    {
        private static string smtpHost = null;
        private static int smptPort = 25;
        private static bool smtpIsUserCredentials = false;
        private static string smtpCredentialAccount = null;
        private static string smtpCredentialPassword = null;
        /// <summary>
        /// 设置发送邮件参数
        /// </summary>
        /// <param name="host">smtp服务器地址或名称</param>
        /// <param name="port">smtp服务端口 一般为25</param>
        /// <param name="isUserCredentials">是否需要认证</param>
        /// <param name="account">需要认证时的用户</param>
        /// <param name="password">需要认证时的用户的密码</param>
        public static void SetParameters(string host, int port, bool isUserCredentials, string account, string password)
        {
            smtpHost = host;
            smptPort = port;
            smtpIsUserCredentials = isUserCredentials;
            smtpCredentialAccount = account;
            smtpCredentialPassword = password;
        }
        /// <summary>
        /// 设置发送邮件参数 取配置
        /// </summary>
        private static void setParameters()
        {
            if (String.IsNullOrEmpty(smtpHost))
            {
                smtpHost = WebHelper.GetAppConfig("SmtpHost");
                smptPort = DataHelper.GetIntValue(WebHelper.GetAppConfig("SmptPort"), 25);
                smtpIsUserCredentials = Constants.TRUE_ID.Equals(WebHelper.GetAppConfig("SmtpIsUserCredentials"));
                smtpCredentialAccount = WebHelper.GetAppConfig("SmtpCredentialAccount");
                smtpCredentialPassword = WebHelper.GetAppConfig("SmtpCredentialPassword");
            }
        }
        /// <summary>
        /// 发送邮件 发送邮件错误不会抛出异常
        /// </summary>
        /// <param name="receivers">收件人</param>
        /// <param name="title">标题/主题</param>
        /// <param name="content">信件内容</param>
        /// <param name="sender">发件人 空则取系统配置</param>
        public static void SendMail(string receivers, string title, string content, string sender)
        {
            if (!String.IsNullOrEmpty(receivers))
            {
                //初始化参数
                setParameters();
                if (!String.IsNullOrEmpty(smtpHost))
                {
                    try
                    {
                        SmtpClient smtp = new SmtpClient(smtpHost, smptPort);
                        if (smtpIsUserCredentials)
                        {
                            smtp.UseDefaultCredentials = true;
                            smtp.Credentials = new NetworkCredential(smtpCredentialAccount, smtpCredentialPassword); ;
                        }
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        if (String.IsNullOrEmpty(sender))
                        {
                            sender = smtpCredentialAccount;
                        }
                        foreach (string receiver in DataHelper.GetStrings(receivers))
                        {
                            MailMessage msg = new MailMessage(sender, receiver, title, content);
                            msg.BodyEncoding = Encoding.UTF8;
                            msg.SubjectEncoding = Encoding.UTF8;
                            msg.IsBodyHtml = true;
                            smtp.Send(msg);
                            msg.Dispose();
                        }
                    }
                    catch { }
                }
            }
        }
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.net把图片存入数据库和读取图片的方法发布时间:2022-02-05
下一篇:
ASP.NET Datagridview自动换行的小例子发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap