使用支持Pop3的邮箱给好友群发邮件. C# .Net 2.0 邮件发送 + XML 读取操作代码.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Xml;
namespace xmas
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage mailMessage = new MailMessage();
            string friendName = "";
            string friendWords = "";
            string friendEMail = "";
            string xmlFilePath = "myfriendxmas.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFilePath);
            XmlNodeList xl = doc.SelectNodes("root/friend");
            foreach (XmlNode xn in xl)
            {
                XmlNodeReader xr = new XmlNodeReader(xn);
                while (xr.Read())
                {
                    if (xr.NodeType == XmlNodeType.Element)
                    {
                        if (xr.Name == "friend")
                        {
                            try
                            {
                                friendName = xn.Attributes["name"].Value;
                                friendWords = xn.Attributes["words"].Value;
                                friendEMail = xn.Attributes["mail"].Value;
                                Console.WriteLine("准备发送邮件到:" + friendName + friendWords + friendEMail);
                                string mailBody = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\" http://www.w3.org/1999/xhtml\"><head><title>Belem's wishes to you ...</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style type=\"text/css\">td img {display: block;}</style></head><body><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"597\"><tr><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"194\" height=\"1\" border=\"0\" alt=\"\" /></td><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"403\" height=\"1\" border=\"0\" alt=\"\" /></td><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"1\" height=\"1\" border=\"0\" alt=\"\" /></td></tr><tr><td colspan=\"2\"><img src=\" http://lieb.cn/xmastemp/xmas_r1_c1.gif\ " width=\"597\" height=\"39\" border=\"0\" id=\"xmas_r1_c1\" alt=\"\" /></td><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"1\" height=\"39\" border=\"0\" alt=\"\" /></td></tr><tr><td rowspan=\"2\"><img src=\" http://lieb.cn/xmastemp/xmas_r2_c1.jpg\ " width=\"194\" height=\"339\" border=\"0\" id=\"xmas_r2_c1\" alt=\"\" /></td><td><img src=\" http://lieb.cn/xmastemp/xmas_r2_c2.gif\ " width=\"403\" height=\"174\" border=\"0\" id=\"xmas_r2_c2\" alt=\"\" /></td><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"1\" height=\"174\" border=\"0\" alt=\"\" /></td></tr><tr><td background=\" http://lieb.cn/xmastemp/xmas_r3_c2.jpg\"><div style=\"color:#fff; font-family:verdana; font-size:12px;\">Hi " + friendName + ";<br /><br />" + friendWords + "<br /><br />With kind personal regards,<br/>张敏(Belem) 2006.12.22<br/><br/><a href=\" http://gb2312.spaces.live.com\ " title=\"访问Belem的共享空间\"><span style=\"color:yellow;\">View More...</span></a></div></td><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"1\" height=\"165\" border=\"0\" alt=\"\" /></td></tr><tr><td colspan=\"2\"><img src=\" http://lieb.cn/xmastemp/xmas_r4_c1.gif\ " width=\"597\" height=\"84\" border=\"0\" id=\"xmas_r4_c1\" alt=\"\" /></td><td><img src=\" http://lieb.cn/xmastemp/spacer.gif\ " width=\"1\" height=\"84\" border=\"0\" alt=\"\" /></td></tr></table><br/><br/><div style=\"font-size:9px; font-family:verdana; color:#acacac;\">* Is this a Junk mail? No, this a wish mail created by Belem since you are Belem's Friends, Classmates, Colleague, or in Belem's messenger buddy list or talked about Windows Live Spaces issues at least once to Belem through Belem's official Mail or Messenger accounts.<br/></div></body></html>";
                                MailAddress fromMailAddress = new MailAddress(" [email protected] ", "张敏");
                                smtpClient.Host = "smtp.gmail.com";
                                smtpClient.EnableSsl = true;
                                smtpClient.Port = 25;
                                smtpClient.UseDefaultCredentials = false;
                                smtpClient.Credentials = new System.Net.NetworkCredential("
[email protected] ", "发送邮箱的密码");
                                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                                mailMessage.From = fromMailAddress;
                                mailMessage.To.Add(friendEMail);
                                //mailMessage.CC.Add("
[email protected] ");
                                mailMessage.Subject = "张敏的新年祝福 Merry Xmas and Happy New Year! - Belem ...";
                                mailMessage.IsBodyHtml = true;
                                mailMessage.Body = mailBody;
                                smtpClient.Send(mailMessage);
                                mailMessage.To.Clear();
                                Console.WriteLine("Mail is sent to {0} sucessfully.", friendName);
                                System.Threading.Thread.Sleep(600);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("晕了 {0}", ex.Message);
                                Console.ReadLine();
                            }
                        }
                        else
                        {
                            throw new Exception("晕大发
了...");
                        }
                    
                    else
                    {
                        throw new Exception("真的晕大发了...");
                    }
                }
                xr.Close();
            }
        }
    }
}

----
本例XML格式:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <friend name="狗蛋" mail="狗蛋@live.com" words="圣诞到, 天天开心!" />
  <friend name="石青" mail="石青@live.cn" words="新年到, 你好!" />
  <friend name="小峰" mail="木有@live.com" words="你到底有没有邮箱?" />
</root>

你可能感兴趣的:(.net)