C#发送邮件程序 及测试邮件服务器

今天,总算是对这个邮件程序有个交待了。这个邮件程序反复修改,现在测试也差不多了。拿出来跟大家共享一下。源码附后,我在开头说个要点。首先,地球人都知道C#开发邮件发送程序有三个办法:

第一:使用sys.web.mail命名空间下的MailMessage类等的方法属性。

第二:引用CDO组件(跟第一个办法其实也是引用了CDO组件)。

第三个办法就是用socket,自己编写了,呵呵,当然对于我这个菜鸟级的技术来说,那时不可能的。所以我选择了第一种。

       那么开篇讲了那么多,还得补充点知识,那就是关于邮件发送方面的知识:smtp服务器的运作问题。首先,你在客户端向邮件服务器端请求要发送邮件,你首先是发送你的验证过去,比如你是用新浪的邮件服务器(smtp.sina.net)那么你就要把你在新浪上的账号和密码传给这台服务器,在这台服务器上对你的身份进行验证,通过的话,他就通知你(获得验证通过的票据),ok,你可以发邮件给我这个服务器了。(但是如果你弄虚作假,嘿嘿,给你个想不到的错误:不能访问CDO.Message对象。让你还误以为是你的本地程序错误^_^)在你发送邮件给发送邮件服务器(新浪)的时候,它会有一系列的安全策略来保证邮件合法性的安全性。比如如果你在很短时间内连续发送大量的邮件给发送邮件服务器,并且使用同一账号和密码,嘿嘿,这个时候,它就要采取措施了。(在这种情况下,新浪的邮件服务器只会暂时对你的锁定,不接收你的请求)。下面,在接受到你的邮件后,你的邮件自然就在它的邮件服务器队列里面等待被发送到接收邮件服务器去了。一旦发送到接收邮件服务器成功,那么收件人就能登陆邮箱,在接收邮件服务器上取信了。这个就是简略的邮件发送过程。最关键的是验证身份问题。

       在目前的多种邮件服务器当中,有需要验证的,有不需要验证的(匿名),需要验证我的我所知道的有新浪,126,支持匿名的邮163c.om, gmail.com  msn.com  hotmail.com ,更奇怪的是我公司的邮箱(9588.com)是由新浪服务器托管的,但却支持匿名发送。也许是因为企业邮箱和个人邮箱的不同。哎,新浪就是狗眼看人低啊。需要验证的(获得发送邮件服务器的票据),指的是,在整个邮件发送接收一系列过程中,如果一个环节需要验证票据(比如接收服务器和发送邮件服务器)那你就必需要获得发送邮件服务器给你的验证票据,也就是你要把你在这台发送邮件服务器上的账号和密码发送给它。

     现在,我再来简单讲讲匿名发送邮件的机制。首先,你要看看你的iis里面,点开那个总结点,有个叫作默认 SMTP 虚拟服务器,明白了吧,在iis中能做发送邮件服务器(不过是虚拟的)。右键,选择属性,就可以对一系列邮件属性进行设置,比如邮件的大小啦,等待时间啦,等等等等。匿名发送就是让本机充当发送邮件服务器,把邮件发送到接收邮件服务器上去。不过这个可是没有验证票据的,所以,如果象新浪那样的烂服务器,就会不接收,并且返回你个错误,未能访问CDO。Message对象。但是gmail,163.com都可以支持匿名发送。

当然,我还在研究能不能让iis中虚拟邮件服务器获得验证票据而可以在任何邮件服务器中流通,研究好了,我就能当黑客啦,哈哈满天下的垃圾邮件任我发,邮箱人我炸!不过先得申明,这可是开玩笑,如果有人试图利用本文来造成网络威胁,我可当不起责任,偶今天只是试验了一下,给我同学的msn邮箱里发了1000封邮件,呵呵,还是以BIllGates@Mircrosoft.com 的名义发送的,内容是:你中毒啦,赶紧杀毒吧~~~呵呵,言归正传,代码如下,不过也没什么,在网上都能搜到。

测试结果如下:

第一次:启动10个线程,每个线程发送一封邮件,每发一次线程睡眠10秒,结果是10封新浪全都收到。

第二次:启动50个线程,每发一次线程休息10秒,与第二次对比,想考察新浪服务器的安全策略是基于邮件量还是基于时间间隔的。结果却是只收到了18封。

第三次:启动18个线程,每次发送无休息间隔,结果一封信都没有收到。

第四次:18个线程,休息10秒,结果还是没收到。所以。。。新浪的邮件服务器肯定是此有自己的安全策略。应该是要禁锢我一段时间了。

第五次,大概等了1个小时,我再发,启动18个线程,每次间隔10秒,结果收到15封!!!

第六次:10个线程,休息10秒每次,收到9封!

第七次:100个线程,休息10秒每次,收到3封!

第八次:100个线程,无休息,一封都没有收到!

第九次:100个线程,休息20秒每次,没有收到一封,应该是新浪暂时把我禁锢了。

我需要再等待解锢了。那下次测试只有等到明天,已经晚上7点了。

简单代码如下(无线程和循环的代码,这个测试读者可以自己写,很容易的。):

 

一,支持的匿名的 :

public static void SendMail(string toMail,string fromMail,string subject,string emailBody,string attachment)
  {
   MailMessage mail = new MailMessage();
   mail.To = toMail;
   mail.From = fromMail;
   mail.Subject = subject;
   mail.Body = emailBody;
   mail.BodyFormat = MailFormat.Html;//设置为HTML格式 

   System.Web.Mail.SmtpMail.SmtpServer="localhost";
   if(attachment.Trim().Length>1)
    mail.Attachments.Add(new MailAttachment(attachment));
   System.Web.Mail.SmtpMail.Send(mail);

需要身份验证的:

public static void SendMail(string toMail,string password,string fromMail,string account,string subject,string emailBody,string smtpserver,string attachment)
  {
   MailMessage mail = new MailMessage();
   mail.To = toMail;
   mail.From = fromMail;
   mail.Subject = subject;
   mail.Body = emailBody;
   mail.BodyFormat = MailFormat.Html;//设置为HTML格式 
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",2);
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendemailaddress",toMail);
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpaccountname",fromMail);
   //设置为需要用户验证
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  
   //设置验证用户名(把my_username_here改为你的验证用户名)
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", account); 
   //设置验证密码(把password改为你的验证密码)
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);  

   System.Web.Mail.SmtpMail.SmtpServer=smtpserver;
   if(attachment.Trim().Length>1)
    mail.Attachments.Add(new MailAttachment(attachment));
   System.Web.Mail.SmtpMail.Send(mail);
  }

C# 开发的邮件服务器 Features supports pop3 smtp imap etc. supports ssl/tls for pop3, smtp and imap. supports multi mail vitural server, can bind multi domains. supports run as windows service, winform application with or without tray icon control. supports remote management using mailservermanager.exe supports mail recycle bin supports plugins using api interfaces Build and release download source code or use the releases i provided. if from source code you should run afterbuild.bat after build the solution successfuly then you get the debug or release version from application folder. Installation run MailServerLauncher.exe to install as windows service or just run as desktop application with or without tray icon. Configuration run MailServerManager.exe at the machine runs mailserver service or app. Connect to server press connect button from menu. type server localhost or 127.0.0.1 or leave it empty type username Administrator with case sensitive type password emtpy press ok to connect with saving or not Add virtual server type name and select storage api [your vitural server]>system>general, add dns servers for query mailto's domain mx record. [your vitural server]>system>services, enable smtp and pop3 services and set ipaddress binding with or without ssl/tls. the host name is required when set bindings. eg. bind smtp service to smtp.[your.domain] + IPAddress.Any [your vitural server]>system>service>relay, 'send email using DNS' and set at least a local ipaddress binding for email sending. the name of the binding here only a name,not mean domain. [your vitural server]>system>logging, enable logging for all services when something error you can see the details from 'Logs and Events' node [your vitural server]>domains, set email host domain, eg. if your email will be xyz@abc.com then the domain should be abc.domain, description is optional [your vitural server]>security, !!! important, add rules for your service to allow outside access like email client. eg. add a rule 'Smtp Allow All' for smtp service with ip allows between 0.0.0.0 to 255.255.255.255 to enable smtp service for outside access add 'Pop3 Allow All' and 'Rlay Allow All' like that too. [your vitural server]>filters, there are two types of filter named 'DnsBlackList' and 'VirusScan' its configurable by run it's executable from mail server install path. Domain name resolution Add smtp.[your.domain], pop3.[your.domain], imap.[your.domain] resolution to your server public ip address with A record or to your server domain with CNAME record. mx record is optional if [your.domain] has a A record.if not, you shoud add a mx record point to your server ip. Remote management to enable remote management you must add ACL to allow mail server managers connect from outside network. use MailServerAccessManager.exe to add management users or just use administrator. add rules to allow access from specific IPs or ip ranges The users here only for management cases.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值