幽灵谷

追求永无止境......
posts - 57, comments - 1, trackbacks - 0, articles - 0
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c# 发邮件的代码

Posted on 2010-04-08 17:47 zhb6022 阅读(...) 评论(...) 编辑 收藏

       /// <summary>

        /// 发送邮件

        /// </summary>

        /// <param></param>

        /// <param></param>

        private void sendMail(string title, string body)

        {

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

            SystemConfig config = new SystemConfig();

            doc.Load(Application.StartupPath + Constants.ConfigFileName);

            for (int i = 0; i < SystemConfig.ConfigItems.GetLength(0); i++)

            {

                config[SystemConfig.ConfigItems[i, 1]] = doc.SelectSingleNode(SystemConfig.ConfigItems[i, 0]).InnerText;

            }

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress(config["from"], config["from"]);

            mail.To.Add(config["to"]);

            mail.CC.Add(config["cc"]);

            mail.Subject = title;

            mail.Body = body;

 

            //邮件服务器和端口

            SmtpClient smtp = new SmtpClient(config["host"], 25);

            smtp.UseDefaultCredentials = true;

            //指定发送方式

            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            //指定登录名和密码

            smtp.Credentials = new System.Net.NetworkCredential(config["SMTPUSERNAME"], config["SMTPPASSWORD"]);

            smtp.Send(mail);

        }