c#向指定的邮箱发送邮件

时间:2022-10-25 07:55:25
 private bool SendEmail(string fileName)
        {
            MailMessage m_Mail = new MailMessage();
            m_Mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"]);
            string m_MailToStr = ConfigurationManager.AppSettings["MailTo"];
            string m_MailServer = ConfigurationManager.AppSettings["MailServer"];
            string[] m_MailToList = m_MailToStr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < m_MailToList.Length; i++)
            {
                m_Mail.To.Add(new MailAddress(m_MailToList[i]));
            }
            m_Mail.Subject = "Pending Fund List";
            m_Mail.Body = "Please find the attachment for the Pending Funds List while it is ID, OP & Perf ready. ";
            m_Mail.IsBodyHtml = true;
            m_Mail.BodyEncoding = System.Text.Encoding.UTF8;
            Attachment m_MailAttach = null;
            if (!string.IsNullOrEmpty(fileName))
            {
                m_MailAttach = new Attachment(fileName);
                m_Mail.Attachments.Add(m_MailAttach);
            }
            SmtpClient client = new SmtpClient(m_MailServer);
            try
            {
                client.Send(m_Mail);
                if (m_MailAttach != null)
                    m_MailAttach.Dispose();
                return true;
            }
            catch (Exception e)
            {
                //_log.Error(e.Message);
                return false;
            }
        }