您的位置:首页 > 其它

.net 自动向用户发送邮件

2011-04-22 11:23 369 查看
   .net 使用System.Net.Mail命名空间实现发送邮件功能。

可以添加附件、添加抄送

 

public static bool SendSystemMail(string host, string sysEmail, string uname, string password, string toEmail, string subject, string content, string[] files, bool isBodyHtml, params string[] ccs)
{
//创建默认主机的邮件服务器
SmtpClient client = new SmtpClient(host);
client.UseDefaultCredentials = false;
//设置发送电子邮件方式
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//设置身份验证(用户名、密码)
client.Credentials = new NetworkCredential(uname, password);

//定义返回值
bool isSend = false;
try
{
//获取要发送的电子邮件
MailMessage mail = new MailMessage(sysEmail, toEmail, subject, content);
//设置电子邮件编码
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = isBodyHtml;
//发送优先级
mail.Priority = MailPriority.Normal;
//添加附件
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
mail.Attachments.Add(new Attachment(files[i]));
} //for
} //if (files != null)
//添加抄送
if (ccs != null)
{
string ccsAddress = string.Empty;
for (int i = 0; i < ccs.Length; i++)
{
ccsAddress = ccsAddress + ccs[i] + ",";
} //for
if (ccsAddress.Length > 0)
{
ccsAddress = ccsAddress.Substring(0, ccsAddress.Length - 1);
mail.CC.Add(ccsAddress);
} //if
} //if (ccs != null)
//发送邮件
client.Send(mail);
//发送完成后返回true
isSend = true;
} //try
catch (Exception e)
{
isSend = false;
} //catch
return isSend;
} //SendSystemMail
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息