发送邮件找回密码采用outlook的 pop和smtp方式、qq邮箱smtp
一、outlook的pop方式,并指定发送人邮箱地址:
需要引入dll:Microsoft.Office.Interop.Outlook
Outlook.Application olApp = new Outlook.Application(); NameSpace ns = olApp.GetNamespace("mapi"); ns.Logon(Missing.Value, Missing.Value, false, true); Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem); mailItem.To = Email; mailItem.Subject = "密码找回"; mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; mailItem.HTMLBody = "验证码是: " + code + ",请在10分钟内进行验证。验证码提供给他人可能导致账号被盗,请勿泄露,谨防被骗。系统邮件请勿回复。"; ; Outlook.Account account = GetAccountForEmailAddress(olApp, autolooksendemail); mailItem.SendUsingAccount = account; ((Outlook._MailItem)mailItem).Send(); ns.Logoff(); mailItem = null; ns = null; olApp = null;
public Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress) { // Loop over the Accounts collection of the current Outlook session. Outlook.Accounts accounts = application.Session.Accounts; foreach (Outlook.Account account in accounts) { logger.Info("Outlook.Account:" + account.SmtpAddress); // When the e-mail address matches, return the account. if (account.SmtpAddress == smtpAddress) { return account; } } throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); }
其中指定的邮箱autolooksendemail应该包含在内。若不在,则添加,步骤如下:
1、打开客户端
2、添加账户
不推荐此方式,因为用exchange方式不安全。
二、outlook的smtp方式:
需要引用System.Net.Mail.dll
SmtpClient smtpClient = new SmtpClient() { UseDefaultCredentials = false, DeliveryMethod = SmtpDeliveryMethod.Network, }; smtpClient.Credentials = new NetworkCredential(sendeaccount, autolooksendemailPwd); var message = new MailMessage { From = new MailAddress(autolooksendemail), Sender = new MailAddress(autolooksendemail), Subject = "密码找回", IsBodyHtml = false }; message.To.Add(Email); message.Body = "验证码是: " + code + ",请在10分钟内进行验证。验证码提供给他人可能导致账号被盗,请勿泄露,谨防被骗。系统邮件请勿回复。"; smtpClient.Send(message); message.Dispose(); smtpClient.Dispose();
三、qq邮箱的smtp方式:
需要引入System.Net.Mail.dll
SmtpClient client = new SmtpClient("smtp.qq.com"); //实例化一个SmtpClient类 client.UseDefaultCredentials = true; MailMessage mail = new MailMessage(); //实例化一个邮件类 mail.From = new MailAddress("发件人邮箱地址"); //发件人邮箱地址 mail.To.Add(new MailAddress("收件人邮箱地址")); //收件人邮箱地址 mail.Subject = "密码找回"; //邮件标题 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "密码找回内容"; //邮件内容 mail.BodyEncoding = System.Text.Encoding.UTF8; client.EnableSsl = true; //使用安全加密连接 client.Credentials = new NetworkCredential(sendEmail, AuthorizationCode);//验证发件人身份(发件人邮箱,邮箱授权码); client.Send(mail); //发送邮件 mail.Dispose(); mail = null; client.Dispose(); client = null;
四、生成随机码:
public static string CreateRandomCode(int length) //生成由数字和大小写字母组成的验证码 { string list = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; //list中存放着验证码的元素 Random random = new Random(); string code = ""; //验证码 for (int i = 0; i < length; i++) //循环6次得到一个伪随机的六位数验证码 { code += list[random.Next(0, list.Length - 1)]; } return code; }
随机码一定时间内有效,我采用的是redis。
五、1分钟后可再次发送邮箱随机码
var timer1 = 60; //倒计时 function TimeDown() { $("#timer1").html(timer1); if (timer1 > 0) { setTimeout(function () { TimeDown(); }, 1000); } else if (timer1 <= 0) { timer1 = 61; $("倒计时显示部分").css("display", "block"); $("发送按钮显示部分").css("display", "none"); } --timer1; };