当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net mvc发送邮件实例讲解

asp.net mvc发送邮件实例讲解

2017年12月12日  | 移动技术网IT编程  | 我要评论

2013兰州马拉松,褪色笔,风轻扬

qq邮箱 pop3 和 smtp 服务器地址设置如下:邮箱pop3服务器(端口110)smtp服务器(端口25)qq.compop.qq.comsmtp.qq.comsmtp服务器需要身份验证。

如果是设置pop3和smtp的ssl加密方式,则端口如下:

pop3服务器(端口995)

smtp服务器(端口465或587)。

qq邮箱要注意开启下面的服务:qq会给你个授权码,在下面的代码中详细的讲了在哪用这个授权码。

控制器中代码:

using system.net.mail;//邮件发送需引用 
public actionresult bindstep3(modelcompany c)
    {
      .........
      ..........
      ...........
      mailaddress messagefrom = new mailaddress("xxxxx@qq.com"); //发件人邮箱地址 
      string messageto = company.company_email; //收件人邮箱地址 
      string messagesubject = "激活验证"; //邮件主题 
      string messagebody = "请进行邮箱验证来完成您注册的最后一步,点击下面的链接激活您的帐号:<br><a target='_blank' rel='nofollow' style='color: #0041d3; text-decoration: underline' href=''>激活</a>"; //邮件内容 (一般是一个网址链接,生成随机数加验证id参数,点击去网站验证。)
 
    if (sendmail(messagefrom, messageto, messagesubject, messagebody))
    {
      response.write("<script type='text/javascript'>alert('发送邮件失败');</script>");
    }
    else
    {
      response.write("<script type='text/javascript'>alert('发送邮件失败');</script>");
    }
      tempdata["companybind3"] = company;
      return view("bindstep3");
    }

    public bool sendmail(mailaddress messagefrom,string messageto,string messagesubject,string messagebody)  //发送验证邮件
    {
      mailmessage message = new mailmessage();
      message.to.add(messageto);
      message.from = messagefrom;
      message.subject = messagesubject;
      message.subjectencoding = system.text.encoding.utf8;
      message.body = messagebody;
      message.bodyencoding = system.text.encoding.utf8;
      message.isbodyhtml = true; //是否为html格式 
      message.priority = mailpriority.high; //发送邮件的优先等级 
      smtpclient sc = new smtpclient();
      sc.enablessl = true;//是否ssl加密
      sc.host = "smtp.qq.com"; //指定发送邮件的服务器地址或ip 
      sc.port = 587; //指定发送邮件端口 
      sc.credentials = new system.net.networkcredential("xxxxx@qq.com", "xxxxx"); //指定登录服务器的用户名和密码(注意:这里的密码是开通上面的pop3/smtp服务提供给你的授权密码,不是你的qq密码)

      try
      {
        sc.send(message); //发送邮件 
      }
      catch(exception e)
      {
        response.write(e.message);
        return false;
      }
      return true;
      
    }

 实现效果:

 参考文章:

以上就是本文的全部内容,希望对大家学习asp.net mvc发送邮件有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网