C#发送邮件

  public class SendEmailHelper
    {
        public SmtpClient _smtp = null;
        public MailMessage _mail = null;

        public void SendEmail()
        {
            _smtp.Send(_mail);
        }

        public void InitSMTP(string host, int port, string userName, string password)
        {
            _smtp = new SmtpClient
            {
                Host = host,
                Port = port,
                EnableSsl = false,
                Credentials = new System.Net.NetworkCredential(userName, password)
            };
        }

        public void InitMailMessage(string subject, string body, bool isHtmlFormat, string from, List<string> lstTo, List<string> lstCc)
        {
            _mail = new MailMessage()
            {
                From = new MailAddress(from),
                Subject = subject,
                Body = body.Replace("\r\n", "<br />"),
                IsBodyHtml = isHtmlFormat,
                BodyEncoding = Encoding.UTF8,
                Priority = MailPriority.High
            };
            if (lstTo != null)
            {
                for (int i = 0; i < lstTo.Count; i++)
                {
                    _mail.To.Add(lstTo[i]);
                }
            }
            if (lstCc != null)
            {
                for (int i = 0; i < lstCc.Count; i++)
                {
                    _mail.CC.Add(lstCc[i]);
                }
            }
        }


        public void AddAttchement(List<string> attachments)
        {
            if (attachments != null && attachments.Count > 0)
            {
                foreach (String FileName in attachments)
                {
                    if (File.Exists(FileName))
                    {
                        Attachment att = new Attachment(FileName, MediaTypeNames.Application.Octet);
                        att.NameEncoding = Encoding.UTF8;
                        _mail.Attachments.Add(att);
                    }
                }
            }
        }

        public void DeleteAttachments()
        {
            if (_mail.Attachments != null && _mail.Attachments.Count > 0)
            {
                _mail.Attachments.Dispose();
            }
        }
    }

猜你喜欢

转载自new-fighter.iteye.com/blog/2203330