带附件的邮箱发送

来源:互联网 发布:淘宝消除模块间隙代码 编辑:程序博客网 时间:2024/05/01 06:00

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;
    }

 

}

原创粉丝点击