现在的位置: 首页 > 综合 > 正文

C#发邮件

2017年11月08日 ⁄ 综合 ⁄ 共 2088字 ⁄ 字号 评论关闭

/// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="smtpServer"></param>
        /// <param name="mailAddress"></param>
        /// <param name="mailPwd"></param>
        /// <param name="receive"></param>
        /// <param name="cc"></param>
        /// <param name="attach"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static void SendMail(string smtpServer, string mailAddress
                               , string mailPwd, List<string> receive
                               , List<string> cc, List<string> attach
                               , string subject, string body)
        {
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer);
            smtp.Credentials = new System.Net.NetworkCredential(mailAddress, mailPwd);
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            try
            {
                mail.Subject = subject;
                mail.Body = body;
                mail.From = new MailAddress(mailAddress);
 
                if (receive != null && receive.Count > 0)
                {
                    foreach (string str in receive)
                    {
                        mail.To.Add(str);
                    }
                }

                if (cc != null && cc.Count > 0)
                {
                    foreach (string str in cc)
                    {
                        mail.CC.Add(str);
                    }
                }

                if (attach != null && attach.Count > 0)
                {
                    Attachment attchFile = null;
                    FileInfo fi = null;
                    foreach (string fileName in attach)
                    {
                        fi = new FileInfo(fileName);
                        attchFile = new Attachment(fileName);
                        attchFile.Name = fi.Name;
                        mail.Attachments.Add(attchFile);
                    }
                }
                smtp.Send(mail);

            }
            catch (Exception ex)
            {
            }
            finally
            {
                mail.Dispose();
            }

        }

【上篇】
【下篇】

抱歉!评论已关闭.