C#中怎么利用SendMail发送邮件
这篇文章给大家介绍C# 中怎么利用SendMail发送邮件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟空间、营销软件、网站建设、准格尔网站维护、网站推广。
具体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Data;
using CDO;
using ADODB;
namespace SendMailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
}
public static string SendMsg()
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("date");
dt.Columns.Add("area");
dt.Columns.Add("orgnizer");
dt.Columns.Add("keyword");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["name"] = "北文中心影视产权交易平台•影视项目路演季---路演项目征集" + i;
dr["date"] = "2017-06-30";
dr["area"] = "北京市 北京电影学院文创园(平房园区)" + i;
dr["orgnizer"] = "北文中心影视产权交易" + i;
dr["keyword"] = "影视" + i;
dt.Rows.Add(dr);
}
string MailBody = "以下内容为系统自动发送,请勿直接回复,谢谢。
";
MailBody += "";
MailBody += "
";
for (int hcol = 0; hcol < dt.Columns.Count; hcol++)
{
MailBody += " ";
MailBody += dt.Columns[hcol].ColumnName;
MailBody += " | ";
}
MailBody += "
";
for (int row = 0; row < dt.Rows.Count; row++)
{
MailBody += "";
for (int col = 0; col < dt.Columns.Count; col++)
{
MailBody += " ";
MailBody += dt.Rows[row][col].ToString();
MailBody += " | ";
}
MailBody += "
";
}
MailBody += "
";
MailBody += "
";
return MailBody;
}
public static void SendMail()
{
MailMessage msg = new MailMessage();
msg.To.Add("xxx@ctrchina.cn");
msg.CC.Add("xxxx@sina.com");
msg.From = new MailAddress("ffff@ctrchina.cn", "ffff", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
//msg.Body = "邮件内容";//邮件内容
msg.Body = SendMsg();
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = true;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
//client.Host = "smtp.ctrchina.cn";
client.Host = "210.77.136.200";
client.Port = 465;
//client.EnableSsl = true;//经过ssl加密
client.Credentials = new System.Net.NetworkCredential("xxx@ctrchina.cn", "password");
object userState = msg;
try
{
//client.SendAsync(msg, userState);
client.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
return;
}
}
public static void SendSinaMail()
{
MailMessage msg = new MailMessage();
msg.To.Add("xxx@ctrchina.cn");
//msg.To.Add("xxx@sina.com");
msg.CC.Add("xxx@sina.com");
msg.From = new MailAddress("xxx@sina.com", "shao_sks", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
//msg.Body = "邮件内容";//邮件内容
msg.Body = SendMsg();
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = true;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com";
client.Port = 25;
//client.EnableSsl = true;//经过ssl加密
client.Credentials = new System.Net.NetworkCredential("username", "password");
object userState = msg;
try
{
//client.SendAsync(msg, userState);
client.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
return;
}
}
public static void SenMail1()
{
try
{
CDO.Message oMsg = new CDO.Message();
Configuration MyConfig = new ConfigurationClass();
Fields MyFields = MyConfig.Fields;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "210.77.136.200";
MyFields.Update();
oMsg.Configuration = MyConfig;
oMsg.Subject = "Test SMTP2911111";
oMsg.HTMLBody = SendMsg();
oMsg.From = "shaoks@ctrchina.cn";
oMsg.To = "shaoks@ctrchina.cn";
oMsg.Send();
}
catch (Exception ex)
{
return;
}
}
}
}
关于C# 中怎么利用SendMail发送邮件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网站题目:C#中怎么利用SendMail发送邮件
浏览路径:
http://pwwzsj.com/article/joedpj.html