邮件自动发送

邮件自动发送功能的基本实现:

引用命名空间:

using System.Net.Mail;
using System.Text;

方法实现:

初始化邮件地址:

 MailAddress from = new MailAddress("发送人邮箱地址", "显示的名称");
 MailAddress to= new MailAddress("接受人邮箱地址", "显示的名称");

初始化邮件:

MailMessage mail = new MailMessage();
        mail.Subject = "自动发送";
        mail.IsBodyHtml = true;
        mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312"); //936是简体中文的codepage值注

 //设置邮件的发送级别
        mail.Priority = MailPriority.Normal;
        mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; //自动提示

//收件人
            mail.CC.Add(new MailAddress("Crazylight@126.com", "抄送人", Encoding.UTF8)); //抄送
           // mail.Bcc.Add(new MailAddress("675676122@qq.com", "密抄人", Encoding.UTF8)); //密超收件人

  mail.Body = EmailBody();

            mail.From = from;
            mail.To.Add(to);

//添加附件
  mail.Attachments.Add(new Attachment("G:\\附件.txt"));

 初始化服务器:

//邮件发送服务器
            SmtpClient client = new SmtpClient();
            client.Host = "XX";    //发件人地址所在的服务器SMTP 如网易126邮箱的为smtp.126.com
            //设置用于 SMTP 事务的端口,默认的是 25
            client.Port = 25;

            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = false;

            smtp.Credentials = new System.Net.NetworkCredential("用户名", "密码");
            smtp.Send(mail);
            smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);

            smtp.Dispose();

  OK, 到这里,邮件自动发送功能基本实现。 

posted @ 2012-11-13 17:09  Crazylight  阅读(1309)  评论(1编辑  收藏  举报