zoukankan      html  css  js  c++  java
  • .NET进阶学习之使用ASP.NET两种发送邮件的方法

    如果你的ASP.NET程序学得不错了,想用ASP.NET来发送邮件,在网上有好多文章,本站不管网上说是如何实现的,下面整理两种.NET发送邮件的方法给你参考一下,很简单的哦。

    第一种:

    1、配置文件(Web.config) + 少量C#代码

    configuration 节点下添加

    <system.net>
    <mailSettings>
    <smtp from="baidu@163.com">
    <network host="smtp.163.com" password="baidu" port="25" userName="baidu" defaultCredentials="false"/>
    </smtp>
    </mailSettings>
    </system.net>
    /// <summary>
    /// 发送邮件
    /// </summary>
    private void SendMail()
    {
    System.Net.Mail.MailMessage message
    = new System.Net.Mail.MailMessage();
    message.From
    = new System.Net.Mail.MailAddress("baidu@163.com");//发送人邮箱地址,与smtp节点中的from值一致
    message.To.Add(new System.Net.Mail.MailAddress("taobao@163.com"));//接收人邮箱地址
    message.Subject = "hello";
    message.Body
    = "<b>taobao</b>";
    message.IsBodyHtml
    = true;

    System.Net.Mail.SmtpClient smtpclient
    = new System.Net.Mail.SmtpClient();
    smtpclient.Send(message);
    }
    好,介绍完第一种发送邮件的方法,下面看第二种。

    /// <summary>
    /// 发送邮件
    /// </summary>
    private void SendMail()
    {
    System.Net.Mail.MailMessage message
    = new System.Net.Mail.MailMessage();
    message.From
    = new System.Net.Mail.MailAddress("baidu@163.com");//发送人邮箱地址,与smtp节点中的from值一致
    message.To.Add(new System.Net.Mail.MailAddress("taobao@163.com"));//接收人邮箱地址
    message.Subject = "hello";
    message.Body
    = "<b>taobao</b>";
    message.IsBodyHtml
    = true;

    System.Net.Mail.SmtpClient smtpclient
    = new System.Net.Mail.SmtpClient("smtp.163.com", 25);
    smtpclient.Credentials
    = new System.Net.NetworkCredential("baidu", "baidu");//参数分别是邮箱用户名和密码
    smtpclient.Send(message);
    }
    怎样,上面两种发送邮件的方法都很简单吧。

  • 相关阅读:
    JSP 中文乱码显示处理解决方案
    jsp的9大对象
    获取各种路径
    输出自绘制图片
    Emmet基本使用方法
    <input type="file" />浏览时只显示指定文件类型
    使用dockerfile文件创建image
    gunicorn 访问日志配置与项目启动
    制作符合自己需求的镜像 docker image
    linux 查看系统信息
  • 原文地址:https://www.cnblogs.com/ret00100/p/1627716.html
Copyright © 2011-2022 走看看