您的位置:首页 > 编程语言 > C#

C#发送带附件邮件

2012-04-11 18:09 483 查看
using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Text;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sendmail : System.Web.UI.Page
{

//带附件发送
public static bool SendMailsf(string fromUser, string fromUserName, string toUser, string toUserName, string cc, string subjectNm, string bodyAll, string fj)
{
bool ret = false;
try
{
//Attachment objMailAttachment;
//创建一个附件对象
//objMailAttachment = new Attachment("d:\\test.txt");//发送邮件的附件

objMailAttachment = new Attachment(fj);//发送邮件的附件

MailMessage mm = new MailMessage();
mm.From = new MailAddress(fromUser, fromUserName, Encoding.UTF8);
mm.To.Add(toUser);
string[] listc = null;
try
{
if (cc != null && cc != "")
{
listc = cc.Split(',');
for (int i = 0; i < listc.Length; i++)
{
mm.CC.Add(listc[i].ToString());
}

}
}
catch (Exception list)
{
throw list;
}

mm.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中

mm.Subject = subjectNm;
mm.SubjectEncoding = Encoding.UTF8;
mm.Body = bodyAll;
mm.BodyEncoding = Encoding.UTF8;
mm.IsBodyHtml = true;
//mm.Priority = MailPriority.High;//加急邮件!

SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("帐号", "密码");
client.Host = "主机";
client.Send(mm);
ret = true;
}
catch (Exception ex)
{
throw ex;
}
return ret;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: