发送Email 附件打包,

  MailAddress fromAddress = new MailAddress("sender@meihua.info", "明道", Encoding.GetEncoding("utf-8"));

        StringBuilder sb = new StringBuilder("");

              //发送者 

       System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(user.Email);


        MailMessage message = new MailMessage(fromAddress, toAddress);

        message.Subject = "管理员" + fromUser.Fullname + "发布的通告";

        message.Body = sb.ToString(); 

       message.IsBodyHtml = true; 

       message.BodyEncoding = Encoding.GetEncoding("utf-8");

        message.SubjectEncoding = Encoding.GetEncoding("utf-8");

        if (!string.IsNullOrEmpty(filePath))

        {

            //附件

            System.Net.Mail.Attachment mailAttachment  = new System.Net.Mail.Attachment(filePath);

            if (mailAttachment != null)

                message.Attachments.Add(mailAttachment);

            //foreach (string attachment in attachments)  //循环附件

            //{

            //    System.Net.Mail.Attachment newAttachment = new System.Net.Mail.Attachment(attachment);

            //    if (newAttachment != null)

            //        message.Attachments.Add(newAttachment);

            //}

        }

  try

      {

            MailAddress from = new MailAddress(message.From.Address, message.From.DisplayName, Encoding.GetEncoding("utf-8"));

            message.From = from;

            message.IsBodyHtml = isBodyHtml;

            message.BodyEncoding = Encoding.GetEncoding("utf-8");

            message.SubjectEncoding = Encoding.GetEncoding("utf-8");
            SmtpClient client = new SmtpClient("mail.meihua.info");

            client.Credentials = new System.Net.NetworkCredential("sender", "111111");
            client.Send(message);

        } 

       catch {}

posted @ 2011-05-25 15:23  web开发  阅读(693)  评论(0编辑  收藏  举报