代码路上『App开发专区』『.NET编程论坛』 使用System.Net.Mail 命名空间发邮件

1  /  1  页   1 跳转 查看:1355

[C#] 使用System.Net.Mail 命名空间发邮件

使用System.Net.Mail 命名空间发邮件

昨天晚上没事的时候写的一个发邮件的类... 只是能发送,没有什么经验上的可取之处,老鸟慢飞,菜鸟留步.

 using System;
 using System.Data;
 using System.Configuration;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using System.Net;
 using System.Net.Mail;
 using System.Collections;
 
 namespace SendMail
 {
     public class SendMail
     {
         public static string Title=null;
         public static string Content=null;
         public SendMail()
         {
         }
         public SendMail(string toMail)
         {
             this.toMail = toMail;
         }
         private string toMail=null;
 
         public string ToMail
         {
             get { return toMail; }
             set { toMail = value; }
         }
 
         static SmtpClient smtpClient = new SmtpClient("smtp 服务器");
         static SendMail()
         {
             smtpClient.Credentials = new System.Net.NetworkCredential("用户名", "密码");
         }
         public void Send()
         {
             try
             {
                 if (SendMail.Title == null) return;
                 if (SendMail.Content == null) return;
                 if (this.toMail == null) return;
                 MailAddress from = new MailAddress("邮件地址", "别名");
                 MailAddress to = new MailAddress(this.toMail);
                 MailMessage msg = new MailMessage(from, to);
                 msg.Subject = SendMail.Title;
                 msg.Body = SendMail.Content;
                 msg.BodyEncoding = System.Text.Encoding.Default;
                 msg.IsBodyHtml = true;
                 smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
                 smtpClient.Send(msg);
                 msg.Dispose();
             }
             catch { }
         }
 
         void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
         {
             if (e.Cancelled)
             {
                 R = -1;
             }
             else if (e.Error != null)
             {
                 R = -2;
             }
             else
             {
                 R = 0;
             }
         }
         public int R;
         
     }
     public class Mail
     {
         static Mail()
         {
             
                 SendMail.Title = "标题";
                 SendMail.Content = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/1.htm"));
         }
         private static ArrayList _mailList=new ArrayList();
         public static ArrayList mailList
         {
             get
             {
                 if (_mailList.Count==0)
                 {
                     string[] mails = System.IO.File.ReadAllLines(HttpContext.Current.Server.MapPath("~/maillist.txt"));
                     _mailList = ArrayList.Adapter(mails);
                 }
                 return _mailList;
             }
         }
         public static void ClearList()
         {
             _mailList=new ArrayList();
         }
         public static System.Collections.Generic.List<string> Start(int index, int count)
         {
             System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>();
            //从当前开始发送
             for (int i = 0; i < count; i++)
             {
                 //发送
                 if (index + i + 1 > mailList.Count)
                     break;
                 SendMail sm = new SendMail(mailList[index+i].ToString());
                 result.Add(mailList[index + i].ToString());
                 sm.Send();
             }
             return result;
         }
     }
 }
 
最后编辑杲頔 最后编辑于 2008-11-22 14:44:20
代码路上,你我同行 Codes63.Net
做人要厚道,看帖要回帖!
 

回复:使用System.Net.Mail 命名空间发邮件

恩! 看帖一定要回的
 

回复:使用System.Net.Mail 命名空间发邮件

我以前实现的发邮件的,好像直接打不开邮件。好晕的。这个我再看一下。
如果可以,那和我那个接邮件的联在一起,就天生一对拉。
代码路上,我们更专业。
 

回复:使用System.Net.Mail 命名空间发邮件

好啊  正好这两天先看看
 

回复:使用System.Net.Mail 命名空间发邮件

有能够接受查看邮件的吗?
 
1  /  1  页   1 跳转

版权所有 代码路上  CODES63.NET
特别声明:本站资料和信息全部来自互联网和网站会员上传,若侵犯作者权益请与本站联系,我们将在24小时内删除!  Sitemap

Powered by Discuz!NT 2.1.202    Copyright © 2001-2010 Comsenz Inc.
Processed in 0.0625 second(s) , 3 queries.
返顶部