发送邮件(注册用户并激活邮箱)
注册用户 并激活邮箱 的逻辑
1 获得用户数据2 验证 非空、格式3 验证 图形验证码4 判断 用户是否存在,需要不存在5 判断 邮箱是否已激活使用,需要未激活6 向DB中插入用户,未激活(只要插入,无论激活否,先存入session,如果失败可以用到)7 发送邮件username,activecode,并存入session,设置有效日期8 返回激活码,如果激活码正确,更新DB中激活码9 返回“注册并激活成功,跳转首页”,否则返回“注册成功但激活失败,重新激活新邮箱”10 [重新激活邮箱:如果新邮箱未激活使用,则发送激活码,如果返回正确,更新当前用户邮箱并激活11 返回“激活成功,跳转首页”]
发送邮件
public class SendEmailHelper{/// <summary>/// 发送邮件/// </summary>/// <param name="receiveEmailAddress">接收方邮箱地址</param>/// <param name="subject">邮件主题</param>/// <param name="body">邮件内容</param>public static void SendMail(string receiveEmailAddress,string subject,string body){string sendEmailUserName = ConfigurationManager.AppSettings["sendEmailUserName"]; //发送方邮箱登录名string sendEmaiPassword = ConfigurationManager.AppSettings["sendEmaiPassword"]; //发送方邮箱密码string sendEmailAddress = ConfigurationManager.AppSettings["sendEmailAddress"]; //发送方邮箱地址//简单邮件传输协议类.Mail.SmtpClient client = new .Mail.SmtpClient();client.Host = "";//邮件服务器client.Port = 25;//smtp主机上的端口号,默认是25.client.DeliveryMethod = .work;//邮件发送方式:通过网络发送到SMTP服务器client.Credentials = new workCredential(sendEmailUserName, sendEmaiPassword);//凭证,发件人登录邮箱的用户名和密码//电子邮件信息类.Mail.MailAddress fromAddress = new .Mail.MailAddress(sendEmailAddress);//发件人Email,在邮箱是这样显示的,[发件人:小明<panthervic@>;].Mail.MailAddress toAddress = new .Mail.MailAddress(receiveEmailAddress);//收件人Email,在邮箱是这样显示的, [收件人:小红<43327681@>;] .Mail.MailMessage mailMessage = new .Mail.MailMessage(fromAddress, toAddress);//创建一个电子邮件类mailMessage.Subject = subject;//string filePath = HttpContext.Current.Server.MapPath("/index.shtml");//邮件的内容可以是一个html文本.//System.IO.StreamReader read = new System.IO.StreamReader(filePath, System.Text.Encoding.UTF8); //System.Text.Encoding.GetEncoding("GB2312")//string mailBody = read.ReadToEnd();//read.Close();mailMessage.Body = body;//可为html格式文本mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码mailMessage.IsBodyHtml = true;//邮件内容是否为html格式mailMessage.Priority = .Mail.MailPriority.Normal;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示).try{client.Send(mailMessage);//发送邮件//client.SendAsync(mailMessage, "ojb");异步方法发送邮件,不会阻塞线程. }catch (Exception e){throw new Exception(e.Message);}}}
posted on -10-29 15:00 AdolphYang 阅读(...) 评论(...) 编辑 收藏