使用支持Pop3的邮箱给好友群发邮件. C# .Net 2.0 邮件发送 + XML 读取操作代码.
2011-02-27 16:02
1071 查看
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("belemcheung@gmail.com", "张敏");
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("belemcheung@gmail.com", "发送邮箱的密码");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailMessage.From = fromMailAddress;
mailMessage.To.Add(friendEMail);
//mailMessage.CC.Add("belemcheung@gmail.com");
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>
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("belemcheung@gmail.com", "张敏");
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("belemcheung@gmail.com", "发送邮箱的密码");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailMessage.From = fromMailAddress;
mailMessage.To.Add(friendEMail);
//mailMessage.CC.Add("belemcheung@gmail.com");
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>
相关文章推荐
- ASP.NET读取POP3邮件的操作(纯代码)
- MVC使用ASP.NET Identity 2.0实现用户身份安全相关功能,比如通过短信或邮件发送安全码,账户锁定等
- ASP.NET读取POP3邮件的操作
- ASP.Net 2.0 发送邮件的代码
- ASP.NET读取POP3邮件的操作
- ASP.Net 2.0 发送邮件的代码
- 在asp.NET 中使用SMTP发送邮件的实现代码
- 163邮箱发送邮件碰到:System.Net.Mail.SmtpException: 不允许使用邮箱名称。 服务器响应为:authentication is required
- ASP.Net 2.0 发送邮件的代码
- 使用C#发送邮件支持 Implicit SSL
- C#发送邮件小程序(支持群发,附件)
- C#.NET -自动读取文本文件中Email地址,指定时间后自动发送邮件
- C#发送邮箱实现代码 (转自CFEI.NET)
- ASP.NET使用SMTP协议简单的发送邮件(支持附件)
- 使用LINQ读取RSS,比传统的C#2.0方法节省了很多代码
- C#发送邮件,使用System.Net.Mail的版本
- 在asp.NET 中使用SMTP发送邮件的实现代码
- C#使用 LumiSoft.Net.dll 通过IMAP 登陆QQ邮箱读取未读邮件
- C#发送邮件小程序(支持群发,附件)winform
- C#使用MailAddress类发送html格式邮件的实例代码