当前位置: 移动技术网 > IT编程>开发语言>c# > C# Email邮件发送功能 找回或重置密码功能

C# Email邮件发送功能 找回或重置密码功能

2019年07月18日  | 移动技术网IT编程  | 我要评论

最近根据公司需求,写个邮件发送。这里面的传入的地址信息的参数都是经过加密的,主要是保证用户信息的安全。

using system;
using system.collections.generic;
using system.configuration;
using system.io;
using system.linq;
using system.net.mail;
using system.text;
using system.web;
 
namespace calslnum.helper
{
 /// <summary>
 ///发送邮件类
 /// </summary>
 public class mailservice
 {
  /// <summary> 
  /// 发送邮件程序调用方法 sendmail("abc@126.com", "某某人", "cba@126.com", "你好", "我测试下邮件", "邮箱登录名", "邮箱密码", "smtp.126.com", true,); 
  /// </summary> 
  /// <param name="from">发送人邮件地址</param> 
  /// <param name="fromname">发送人显示名称</param> 
  /// <param name="to">发送给谁(邮件地址)</param> 
  /// <param name="subject">标题</param> 
  /// <param name="body">内容</param> 
  /// <param name="username">邮件登录名</param> 
  /// <param name="password">邮件密码</param> 
  /// <param name="server">邮件服务器 smtp服务器地址</param> 
  /// <param name= "ishtml "> 是否是html格式的邮件 </param> 
  /// <returns>send ok</returns> 
  public static bool sendmail(string from, string fromname, string to, string subject, string body, string server, string username, string password, bool ishtml)
  {
   //邮件发送类 
   mailmessage mail = new mailmessage();
   try
   {
    //是谁发送的邮件 
    mail.from = new mailaddress(from, fromname);
    //发送给谁 
    mail.to.add(to);
    //标题 
    mail.subject = subject;
    //内容编码 
    mail.bodyencoding = encoding.default;
    //发送优先级 
    mail.priority = mailpriority.high;
    //邮件内容 
    mail.body = body;
    //是否html形式发送 
    mail.isbodyhtml = ishtml;
    //邮件服务器和端口 
    smtpclient smtp = new smtpclient(server, 25);
    smtp.usedefaultcredentials = true;
    //指定发送方式 
    smtp.deliverymethod = smtpdeliverymethod.network;
    //发件人身份验证,否则163 发不了 
    smtp.usedefaultcredentials = true;
    //指定登录名和密码 
    smtp.credentials = new system.net.networkcredential(username, password);
    //超时时间 
    smtp.enablessl = false;
    smtp.timeout = 10000;
    smtp.send(mail);
    return true;
   }
   catch (exception)
   {
    return false;
   }
   finally
   {
    mail.dispose();
   }
  }
 
  //读取指定url地址的html,用来以后发送网页用 
  public static string screenscrapehtml(string url)
  {
   //读取stream并且对于中文页面防止乱码 
   streamreader reader = new streamreader(system.net.webrequest.create(url).getresponse().getresponsestream(), system.text.encoding.utf8);
   string str = reader.readtoend();
   reader.close();
   return str;
  }
 
  //发送plaintxt 
  public static bool sendtext(string from, string fromname, string to, string subject, string body, string server, string username, string password)
  {
   return sendmail(from, fromname, to, subject, body, server, username, password, false);
  }
 
  //发送html内容 
  public static bool sendhtml(string from, string fromname, string to, string subject, string body, string server, string username, string password)
  {
   return sendmail(from, fromname, to, subject, body, server, username, password, true);
  }
 
  //发送制定网页 
  public static bool sendweburl(string from, string fromname, string to, string subject, string server, string username, string password, string url)
  {
   //发送制定网页 
   return sendhtml(from, fromname, to, subject, screenscrapehtml(url), server, username, password);
 
  }
  //默认发送格式 
  public static bool sendemaildefault(string toemail,string f_username,string f_pass,string f_times)
  {
   stringbuilder mailcontent = new stringbuilder();
   mailcontent.append("亲爱的×××会员:<br/>");
   mailcontent.append(" 您好!你于");
   mailcontent.append(datetime.now.tostring("yyyy-mm-dd hh:mm:ss"));
   mailcontent.append("通过<a href='#'>×××</a>管理中心审请找回密码。<br/>");
   mailcontent.append("   为了安全起见,请用户点击以下链接重设个人密码:<br/><br/>");
   string url = "http://www.×××.×××/signin/rest?u=" + f_username + "&s=" + f_pass + "&t=" + f_times; 114 mailcontent.append("<a href='" + url + "'>" + url + "</a><br/><br/>"); 115 mailcontent.append(" (如果无法点击该url链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可。)"); 116 return sendhtml(configurationmanager.appsettings["emailname"].tostring(), "会员管理中心", toemail, "×××找回密码", mailcontent.tostring(), configurationmanager.appsettings["emailservice"].tostring(), configurationmanager.appsettings["emailname"].tostring(), configurationmanager.appsettings["emailpass"].tostring()); //这是从webconfig中自己配置的。 117 } 118 } 119 }

webconfig配置信息

<add key="emailname" value="××××@163.com"/>
<add key="emailpass" value="××××"/>
<add key="emailservice" value="smtp.163.com"/>

//说明: 这里面的"emailservice"得与你自己设置邮箱的smtp/pop3/...服务要相同, 大部分是根据@后面的进行配置。我是用163邮箱配置的。 可以根据自己需要自己配置。 

后台调用的方法

 public actionresult sendemail(string emailname)
  {
   emailname = helper.fi_destools.desdecrypt(emailname);
   if (!regex.ismatch(emailname, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
   {
    return content("0");
   }
   string f_username = "";
   string f_pass = "";
   string f_times = helper.fi_destools.desencrypt(datetime.now.tostring("yyyy-mm-dd hh:mm:ss"));
   list<user> list = (from a in users where a.emailaddress == emailname select a).tolist();
   if (list.count > 0)
   {    
    f_username = helper.fi_destools.desencrypt(list[0].×××);
    f_pass = helper.fi_destools.desencrypt(list[0].×××);
 
    bool flag = helper.mailservice.sendemaildefault(emailname, “×××”,“×××”, “×××”); //这里面的参数根据自己需求自己定,最好进行加密
    if (flag)
    {
     return content("true");
    }
    else
    {
     return content("false");
    }
   }
   else {
    return content("false");
   }
  }

发送完邮件效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网