1.读txt文件
格式:[email protected]
[email protected]
[email protected].判断邮件格式,正确的发邮件代码

解决方案 »

  1.   

    txt读取,你会的,单个发送;http://www.cnblogs.com/wknight/archive/2008/12/30/1365636.html多个发送,就是循环
      

  2.   

     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
            {            ds = u_member.GetList("UserName='" + username.Text.Trim() + "'");            if (this.CheckBoxMail.Checked)
                {
                    if (this.txtEmail.Text == "")
                    {
                        Page.RegisterStartupScript("aa", "<script language= 'javascript'>alert('请输入您要接收信息的邮箱!')</script>");
                    }
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        Page.RegisterStartupScript("aa", "<script language= 'javascript'>alert('请输入正确的用户名和邮件')</script>");
                        return;
                    }
                    else if (ds.Tables[0].Rows.Count == 1)
                    {
                        UserPwd = ds.Tables[0].Rows[0]["UserPwd"].ToString();
                        EMail = this.txtEmail.Text;
                        sendemail(UserPwd);
                        Page.RegisterStartupScript("aa", "<script language= 'javascript'>alert('密码已经成功发送到您的" + EMail + "新邮箱,请确认!')</script>");
                        return;
                    }
                }
                else
                {
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        Page.RegisterStartupScript("aa", "<script language= 'javascript'>alert('请输入正确的用户名')</script>");
                        return;
                    }
                    else if (ds.Tables[0].Rows.Count == 1)
                    {
                        UserPwd = ds.Tables[0].Rows[0]["UserPwd"].ToString();
                        EMail = ds.Tables[0].Rows[0]["E_Mail"].ToString();
                        sendemail(UserPwd);
                        Page.RegisterStartupScript("aa", "<script language= 'javascript'>alert('密码已经成功发送到您的" + EMail + "注册邮箱,请确认!')</script>");
                        return;
                    }
                }
              
            }
            private void sendemail(string pwd)
            {
                string Subject = "欢迎来龙买家电子商务";
                jmail.Message Jmail = new jmail.Message();
                //读取HTML模板,即发送的页面
                string strPath = System.Web.HttpContext.Current.Server.MapPath("~/user/MailInfo.html");
                //读取文件,“System.Text.Encoding.Default”可以解决中文乱码问题
                StreamReader sr = new StreamReader(strPath, System.Text.Encoding.Default);
                StringBuilder body = new StringBuilder();
                body.Append(sr.ReadToEnd());
                //关闭文件流
                sr.Close();
                //替换指定内容,通常为需要变动的内容
                string userName = this.username.Text.Trim();
                string upwd = B2B.Utility.DESEncrypt.Decrypt(pwd);
                body = body.Replace("<%userName%>", userName);
                body = body.Replace("<%pwd%>", upwd);
                string FromEmail = "[email protected]";
                string ToEmail = string.Empty;
                //发送到新邮箱
                if (this.CheckBoxMail.Checked)
                {
                    ToEmail = this.txtEmail.Text.Trim();
                }
                //发送到默认邮箱
                else
                {
                    ds = u_member.GetList("UserName='" + username.Text.Trim() + "'");
                    EMail = ds.Tables[0].Rows[0]["E_Mail"].ToString();
                    ToEmail = EMail.ToString();            }
                //Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
                Jmail.Silent = true;
                //Jmail创建的日志,前提loging属性设置为true
                Jmail.Logging = true;
                //字符集,缺省为"US-ASCII"
                Jmail.Charset = "GB2312";
                //信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
                Jmail.ContentType = "text/html";
                //添加收件人
                Jmail.AddRecipient(ToEmail, "", "");
                Jmail.From = FromEmail;
                //发件人邮件用户名
                Jmail.MailServerUserName = "zhuwenjuan_1988";
                //发件人邮件密码
                Jmail.MailServerPassWord = "juan+526";
                //设置邮件标题
                Jmail.Subject = Subject;
                //邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
                //Jmail.AddAttachment( "c:\\test.jpg",true,null);
                //邮件内容
                Jmail.Body = body.ToString().Trim();
                //Jmail发送的方法,可以修改,此为163邮箱服务器
                Jmail.Send("smtp.163.com", false);
                //Jmail.Close();            
            }可以选择用html的形式发送
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <style type="text/css">
    #Layer1 {
    position:absolute;
    width:544px;
    height:311px;
    z-index:1;
    left: 307px;
    top: 93px;
    border-top-width: thin;
    border-right-width: thin;
    border-bottom-width: thin;
    border-left-width: thin;
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;
    }
    .font {font-size: 12px}
    table {
    font-family: "宋体";
    font-size: 12px;
    font-style: normal;
    line-height: normal;
    font-weight: lighter;
    color: #666666;
    }</style>
    </head><body>
    <div id="Layer1">
      <table width="400" height="296" border="0" align="center">
        <tr>
          <td height="37"><img src="http://www.dragonbuyer.com/html/IndexImages/jb_index_image/logo.png" width="203" height="61" /></td>
        </tr>
        <tr>
          <td height="33"><p class="font">尊敬的用户您好:</p>        </td>
        </tr>
        <tr>
          <td height="38">您使用了找回密码的功能,下面是您注册时使用的用户名和密码,请妥善保管。</td>
        </tr>
        <tr>
          <td><table border="0" width="436">
            <tbody>
              <tr>
                <td width="179" align="right"><p>您的用户名</span>:</p></td>
                <td width="247"><p><input type="text" id="lbText" value="<%userName%>" /></p></td>
              </tr>
              <tr>
                <td align="right"><p>您的密码:</p></td>
                <td><p><input type="text" id="Text1" value="<%pwd%>" /</td>
              </tr>        
            </tbody>
          </table></td>
        </tr>
        <tr>
          <td>感谢您对龙买家电子商务的全力支持。</td>
        </tr>
        <tr>
          <td align="right">龙买家电子商务</td>
        </tr>
      </table>
    </div>
    </body>
    </html>
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Net.Mail;
    using System.Text;
    using System.Text.RegularExpressions;public partial class _Default : System.Web.UI.Page
    {    protected void Page_Load(object sender, EventArgs e)
        {
            string Path = @"e:\File\test.txt";
            insertStr(Path);
        }    //读txt文件,判断邮件格式
        public void insertStr(string Path)
        {
            string strLine = "";
           //totalCnt
            int totalCnt = 0;
            //successCnt
            int successCnt = 1;
            try
            {
                StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));            strLine = sr.ReadLine();
                strLine = strLine.Replace("\t", "");
                //1 Line
                if (IsEmail(strLine))
                {
                    SendMail("smtp.163.com", "fenge", "iloveyou", "[email protected]", "", "hi", "hi", "e:\\File\\test.txt");
                }
                while (strLine != null)
                {
                    strLine = sr.ReadLine();
                    if (strLine != null)
                    {
                        //check-------->true------->sendMail
                        if (IsEmail(strLine))
                        {
                            SendMail("smtp.163.com", "wln.13", "ilovemyfamily", "[email protected]", "", "hi", "hi", "e:\\File\\test.txt");
                            successCnt++;
                        }
                    }
                    totalCnt++;
                }
              
                sr.Dispose();
                sr.Close();
            }
            catch
            {
            }
        }    public static bool IsEmail(string email)
        {
            Regex reg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            return reg.IsMatch(email);
        }
        ///   <summary> 
        ///   发送邮件 
        ///   </summary> 
        ///   <param   name= "strSmtpServer "> smtp地址 </param> 
        ///   <param   name= "UserName "> 用户名 </param> 
        ///   <param   name= "Password "> 密码 </param> 
        ///   <param   name= "strFrom "> 发信人地址 </param> 
        ///   <param   name= "strto "> 收信人地址 </param> 
        ///   <param   name= "strSubject "> 邮件标题 </param> 
        ///   <param   name= "strBody "> 邮件正文 </param> 
        public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
        {
            //生成一个   使用SMTP发送邮件的客户端对象 
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);        //表示以当前登录用户的默认凭据进行身份验证 
            client.UseDefaultCredentials = true;
            //包含用户名和密码 
            client.Credentials = new System.Net.NetworkCredential(UserName, Password);        //指定如何发送电子邮件。 
            //Network                            电子邮件通过网络发送到   SMTP   服务器。     
            //PickupDirectoryFromIis             将电子邮件复制到挑选目录,然后通过本地   Internet   信息服务   (IIS)   传送。     
            //SpecifiedPickupDirectory           将电子邮件复制到   SmtpClient.PickupDirectoryLocation   属性指定的目录,然后由外部应用程序传送。             client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;        //建立邮件对象   
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);        //定义邮件正文,主题的编码方式 
            message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            message.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");        //获取或设置一个值,该值指示电子邮件正文是否为   HTML。   
            message.IsBodyHtml = false;        //指定邮件优先级         message.Priority = System.Net.Mail.MailPriority.Normal;        //添加附件 
            //System.Web.Mail.MailAttachment   mailAttachment=new   System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");   
            if (strFileName != " " && strFileName != null)
            {
                Attachment data = new Attachment(strFileName);
                message.Attachments.Add(data);
            }
            //发件人身份验证,否则163   发不了 
            client.Credentials = new System.Net.NetworkCredential(strFrom, Password);
            //发送 
            client.Send(message);
        } }
      

  4.   

    SmtpClient smtp = new SmtpClient();//实例化一个SmtpClient
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//将smtp的出站方式设为Network
            smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
            smtp.Host ="smtp.163.com";//指定 smtp 服务器地址smtp.163.com
            
            smtp.Port = 25;//指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
            //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
            smtp.UseDefaultCredentials =true;//如果需要认证,则用下面的方式
            smtp.Credentials = new NetworkCredential("你的邮箱账号","你的邮箱密码");//登录邮件
           
            MailMessage mm = new MailMessage();//实例化一个邮件类
            mm.Priority = MailPriority.Normal;//邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
            //收件方看到的邮件来源;
            //第一个参数是发信人邮件地址
            //第二参数是发信人显示的名称
            //第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码
            //936是简体中文的codepage值
            mm.From = new MailAddress("邮箱账号", "昵称、名称", Encoding.GetEncoding(936));
            //ReplyTo 表示对方回复邮件时默认的接收地址,即:你用一个邮箱发信,但却用另一个来收信
            //上面后两个参数的意义, 同 From 的意义
            mm.ReplyTo = new MailAddress("邮箱账号","昵称、名称", Encoding.GetEncoding(936));
            // 收件人地址,可以多个,例如:([email protected],[email protected]),多个以逗号分开
            mm.To.Add("[email protected],[email protected],[email protected]");
            mm.Subject = "测试";//标题
            mm.IsBodyHtml = true;//是否以html格式发送
            mm.BodyEncoding = Encoding.GetEncoding(936);//正文的编码,防止乱码        mm.Body = "测试";//邮件的内容,即:正文
            //附件
            //mm.Attachments.Add(new Attachment(@"D:\a.doc", System.Net.Mime.MediaTypeNames.Application.Rtf));
            //多个附件
            //mm.Attachments.Add(new Attachment(@"D:\c.doc"));
            //发送邮件        smtp.Send(mm);
      

  5.   

    下个Discuz!NT吧,里面的注册验证里有完整的源码实例
      

  6.   

    最重要的是先找到可以让你群发的邮件服务商,同时也不要被Bloack掉或者spam