复制代码 代码如下:
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 { }
}
}
}
}
}
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 { }
}
}
}
}
}
标签:
.NET,发送,邮件
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“C#.NET发送邮件的实例代码”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。