欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# SendMail发送邮件功能实现

程序员文章站 2023-11-29 12:00:04
最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法   1、利用新浪邮箱发送   2、利用公司邮箱发送   3、利用cdo发送,这种方式要引用interop....

最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法

  1、利用新浪邮箱发送

  2、利用公司邮箱发送

  3、利用cdo发送,这种方式要引用interop.adodb.dll(http://www.nodevice.com/dll/interop_adodb_dll/item20357.html)和interop.cdo.dll()两个文件

具体代码如下:

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 = "<p style=\"font-size: 10pt\">以下内容为系统自动发送,请勿直接回复,谢谢。</p><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">";
  mailbody += "<div align=\"center\">";
  mailbody += "<tr>";
  for (int hcol = 0; hcol < dt.columns.count; hcol++)
  {
  mailbody += "<td bgcolor=\"999999\">   ";
  mailbody += dt.columns[hcol].columnname;
  mailbody += "   </td>";
  }
  mailbody += "</tr>";

  for (int row = 0; row < dt.rows.count; row++)
  {
  mailbody += "<tr>";
  for (int col = 0; col < dt.columns.count; col++)
  {
   mailbody += "<td bgcolor=\"dddddd\">   ";
   mailbody += dt.rows[row][col].tostring();
   mailbody += "   </td>";
  }
  mailbody += "</tr>";
  }
  mailbody += "</table>";
  mailbody += "</div>";
  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;
  }
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。