发送 激活邮件 的代码及注意事项

来源:互联网 发布:2030中国人工智能 编辑:程序博客网 时间:2024/06/05 02:28

第一次做发送激活邮件,有些没有头绪,后来想了想,有了自己的一套思路!

第一步:你首先先在网上找一个发送邮件的代码,可以实现发送邮件;

代码如下:

命名空间;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;    /**/


    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <returns>发送是否成功</returns>
    public bool Send()
    {
        try
        {
            //获取所有的收件人地址
            //char[] ch = { ',' };
            //string[] address = m_To.Split(ch);
            MailAddressCollection allAddress = new MailAddressCollection();
            //for (int i = 0; i < address.Length; i++)
            //{
            //收件人地址
            MailAddress toAddress = new MailAddress("收件人地址");
            allAddress.Add(toAddress);

            //发件人地址
            MailAddress fromAddress = new MailAddress("发件人地址");

            //定义邮件消息
            MailMessage msg = new MailMessage(fromAddress, toAddress);
            //邮件标题
            msg.Subject = "你好,!";
            //邮件正文
            msg.Body = "当您看到这封邮件时,说明已经发送成功^︺^,www.baidu.com ";

            //设置是否以html方式发送
            msg.IsBodyHtml = true;

            //获取所有邮件附件
            //char[] cr = { ';' };
            //string[] file = m_File.Split(cr);
            //for (int n = 0; n < file.Length; n++)
            //{
            //    if (file[n] != "")
            //    {
            //        //附件对象
            //        Attachment data = new Attachment(file[n], MediaTypeNames.Application.Octet);
            //        //附件资料
            //        ContentDisposition disposition = data.ContentDisposition;
            //        disposition.CreationDate = System.IO.File.GetCreationTime(file[n]);
            //        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file[n]);
            //        disposition.ReadDate = System.IO.File.GetLastAccessTime(file[n]);
            //        //加入邮件附件
            //        msg.Attachments.Add(data);
            //    }
            //}

            //使用简单邮件传输协议来传送邮件
            SmtpClient sendMail = new SmtpClient();
            //发送邮件的服务器名或地址
            sendMail.Host = "smtp." + 发送邮箱.Substring(fromMail.IndexOf("@") + 1)";

            //验证发件人的身份
            sendMail.Credentials = new NetworkCredential(发件人的邮箱账号, 密码);
            //处理待发送邮件的方法
            sendMail.DeliveryMethod = SmtpDeliveryMethod.Network;

 

            //发送邮件
            sendMail.Send(msg);
            //}
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

发送失败的原因可能是你的邮箱smtp协议没选中,请到邮箱的设置里选中,

第二部:激活代码思路

要想做到给注册用户激活,首先在用户注册成功的一般处理程序中写上反复送邮箱的方法,发送内容中有个链接并把用户的ID加在两节后面,链接地址是你写的一个激活页面,用户在邮箱里点击链接换到你的激活页面,激活页面的后天代码要获取到链接地址的Id,根据id得到用户,并把用户的相关字段改为已激活,给用户Alert(“已激活成功”),并跳转到登录页面;

第一次写,有不懂的可以问我!我是个菜鸟,请高手指点

0 0
原创粉丝点击