`
tanglong8848
  • 浏览: 67335 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

C#发送带附件邮件

 
阅读更多
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;
    }



}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics