zoukankan      html  css  js  c++  java
  • [c#] 邮件群发工具的编写(三)邮件发送的几种方式

    上一篇,邮件群发工具的编写(二)数据的保存,我们讲到了邮箱数据的保存。

    这一篇,我们讲一下邮件群发的中心大事之发送。

    首先,我们还是先介绍一下通过程序发送邮件的几种方式。

    1.Smtp方式.

    这种方式比较傻瓜化,在C#里调用起来也相对简单(其他语言像java也有类似的Pocket(包)可以直接调用,相当简单)。通常在.net里,存在两个线程的类可以使用:

    一个是System.Net.Mail,另一个就是Sytem.Web.Mail,当然还有没有其他类我也没去细究过了。然而后面那个类现在VS里已经提示说过时了,那就用前面的那个类吧。

    调用方法如下,先添加对System.Net.Mail的引用。代码如下:

    View Code
     1 /// <summary>
     2    /// 作者:凌晨的搜索者
     3    /// 网址:http://www.cnblogs.com/uu102
     4    /// </summary>
     5     public class MailSender
     6    {
     7        public SmtpClient Client { get; set; }
     8        public MailMessage Msg { get; set; }
     9        private string[] To { get; set; }
    10        private string SmtpServer { get; set; }
    11        private int Port { get; set; }
    12        public MailSender()
    13        {
    14            
    15        }
    16 
    17        public MailSender(string from, string smtpServer, string username, string password)
    18        {
    19            this.Init(from, smtpServer,username,password);
    20        }
    21        private void Init(string from, string smtpServer,string username,string password)
    22        {
    23            this.Msg = new MailMessage();
    24            this.Client = new SmtpClient(smtpServer);
    25            this.SmtpServer = smtpServer;//发送邮箱的Smtp服务器地址
    26            this.Client.UseDefaultCredentials = true;
    27            this.Client.Credentials = new NetworkCredential(username, password);//这里是必须填写的邮箱登录用户名和密码
    28            this.Msg.From = new MailAddress(from);//设置邮件发送地址
    29            this.Msg.IsBodyHtml = true;//这里最好设置为html格式的邮件正文
    30             this.Msg.BodyEncoding = Encoding.UTF8;//邮件正文编码
    31            this.Msg.SubjectEncoding = Encoding.UTF8;
    32            this.Msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;//当邮件发送失败的时候通知一下
    33            
    34        }
    35        public void Send(string subject,string body,string[] attachmentPaths)
    36        {
    37 
    38            
    39            for (int i = 0; i < attachmentPaths.Length; i++)
    40            {
    41                this.Msg.Attachments.Add(new Attachment(attachmentPaths[i]));
    42            }
    43            this.Msg.Subject = subject;
    44            this.Msg.Body = body;
    45             this.Client.Send(this.Msg);
    46             
    47        }
    48    }
    这个发送邮件的封装类没有起到封装的作用,还等待各位去补充完善,代码很简单,调用的话,请接着看下面的代码
    View Code
     1  /// <summary>
     2         /// copyright:No
     3         /// author:凌晨的搜索者
     4         /// site:http://www.cnblogs.com/uu102
     5         /// </summary>
     6         /// <param name="sender"></param>
     7         /// <param name="e"></param>
     8         private void button3_Click(object sender, EventArgs e)
     9         {
    10            try
    11              {
    12                 MailSender mailSender = new MailSender("wxp146@qq.com", "smtp.qq.com", "wxp146", "******");//Rember that it's your password
    13                 links = new Dictionary<string, LinkMan>();
    14                 for (int i = 0; i < listView1.CheckedItems.Count; i++)
    15                 {
    16                     ListViewItem listViewItem = listView1.Items[listView1.CheckedItems[i].Index];
    17                     LinkMan linkMan = new LinkMan();
    18                     string id = listViewItem.Text;
    19                     string nick = listViewItem.SubItems[1].Text;
    20                     string email = listViewItem.SubItems[2].Text;
    21                     string area = listViewItem.SubItems[3].Text;
    22                     linkMan.Nick = nick;
    23                     linkMan.Email = email;
    24                     linkMan.Area = area;
    25                     mailSender.Msg.To.Add(linkMan.Email);
    26 
    27 
    28                     links.Add(linkMan.Email, linkMan);
    29 
    30                 }
    31                
    32                 /*
    33                  * mailSender.Msg.Subject = "这是一封测试信,可以直接放垃圾箱!";
    34                 mailSender.Msg.Body = "你好:测定试一下,打扰之处请见谅!<a href=\"HTTP://www.uu102.com\">曙光营销技术论坛</a>";
    35                 mailSender.Client.Send(mailSender.Msg);
    36                  *
    37                  */
    38                 mailSender.Send("这是一封测试信,可以直接放垃圾箱!",
    39                                  "你好:测定试一下,打扰之处请见谅!<a href=\"HTTP://www.uu102.com\">曙光营销技术论坛</a>",
    40                                  new string[] { Environment.CurrentDirectory + "\\Config\\MailInfo.xml" });
    41                 MessageBox.Show("邮件发送成功");
    42              }
    43             catch (WebException e1)
    44             {
    45                 MessageBox.Show(string.Format("邮件发送失败,请检查设置{0}",e1.Message));
    46             } 
    47         }
    至于上面的代码突然出现的button3是一个点击发送的按钮。以上就是我们要介绍的第一种邮件发送方式

    2.模拟登陆邮箱管理页面,然后模拟发送。

    典型的做法有登录QQ邮箱然后发帖的,网上很多人都在研究这种方式。当然,由于时间和精力的关系,我就没直接贴出代码了,有兴趣的朋友可以联系本人,一起交流探讨一下。

    3.利用IIS的Smtp邮箱服务发送

    这种方法大概是最麻烦的了,要求在自己的机器上装这装那,还不一定会成功。当然,如果你有个人的网站服务器的话,这个也不失为一个办法。

    当然还有很多别的或这改良的方法,比如ssl安全加密发送,Socket发送(这种方法以后有时间再不上吧)。

    以上就是邮件群发的基本因素,如果要做到群发,当然做这一些还不够,要考虑的因素还很多,还望大家自己完善了。

    下一篇,我们就来讲讲群发之后的“邮件跟踪服务”,这是一个目前很多人都关心的问题,邮件发送过程就说到这了。教程每天更新,欢迎继续关注!

  • 相关阅读:
    ORA-00119: invalid specification for system parameter REMOTE_LISTENER
    Standby Redo Log 的设定原则、创建、删除、查看、归档位置
    dataguard类型转换与模式转化
    Restore Points 制定回退方案
    11g dataguard 类型、保护模式、服务
    [置顶] 设计模式系列4-抽象工厂模式
    MediaInfo源代码分析 4:Inform()函数
    Codeforces Beta Round #51 D. Beautiful numbers
    如何将ER图转换成关系模式集
    建立简单的服务器端程序
  • 原文地址:https://www.cnblogs.com/uu102/p/2667811.html
Copyright © 2011-2022 走看看