怎么用C#.NET实现发送邮件功能-创新互联
这篇文章主要讲解了“怎么用C#.NET实现发送邮件功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用C#.NET实现发送邮件功能”吧!
创新互联主要企业基础官网建设,电商平台建设,移动手机平台,成都微信小程序等一系列专为中小企业按需定制制作产品体系;应对中小企业在互联网运营的各种问题,为中小企业在互联网的运营中保驾护航。复制代码 代码如下:
using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using System.Net; namespace MyQuery.Utils { ////// 封装邮件处理 /// by 贾世义 2011-6-3 /// 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; ////// 设置发送邮件参数 /// /// smtp服务器地址或名称 /// smtp服务端口 一般为25 /// 是否需要认证 /// 需要认证时的用户 /// 需要认证时的用户的密码 public static void SetParameters(string host, int port, bool isUserCredentials, string account, string password) { smtpHost = host; smptPort = port; smtpIsUserCredentials = isUserCredentials; smtpCredentialAccount = account; smtpCredentialPassword = password; } ////// 设置发送邮件参数 取配置 /// 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"); } } ////// 发送邮件 发送邮件错误不会抛出异常 /// /// 收件人 /// 标题/主题 /// 信件内容 /// 发件人 空则取系统配置 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 { } } } } } }
感谢各位的阅读,以上就是“怎么用C#.NET实现发送邮件功能”的内容了,经过本文的学习后,相信大家对怎么用C#.NET实现发送邮件功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!
网站名称:怎么用C#.NET实现发送邮件功能-创新互联
转载源于:http://cdiso.cn/article/ehdhd.html