• C# 发送邮件 .net SendEmail 源码


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Mail;

    namespace EZhi.COMP
    {
    /// <summary>
    /// 发送邮件类
    /// </summary>
    public class SendEmail
    {
    /// <summary>
    /// [SMTP服务器域名或IP地址]
    /// </summary>
    public string SmtpServer = "";
    /// <summary>
    /// [发件人Email地址]
    /// </summary>
    public string From= "";
    /// <summary>
    /// [发件人Email密码]
    /// </summary>
    public string FromPass= "";
    /// <summary>
    /// [收件人Email地址]
    /// </summary>
    public string To= "";
    /// <summary>
    /// 邮件主题
    /// </summary>
    public string Subject= "";
    /// <summary>
    /// 邮件内容
    /// </summary>
    public string Body= "";

    public SendEmail()
    {
    this.SmtpServer = "smtp.163.com";
    this.From = "ezhioladmin@163.com";
    this.FromPass = "sulincn";
    }
    public SendEmail(string strTo, string strSubject, string strBody)
    {
    this.SmtpServer = "smtp.163.com";
    this.From = "ezhioladmin@163.com";
    this.FromPass = "sulincn";
    this.To = strTo;
    this.Subject = strSubject;
    this.Body = strBody;
    }

    public SendEmail(string strSmtpServer, string strFrom, string strFromPass, string strTo, string strSubject, string strBody)
    {
    this.SmtpServer = strSmtpServer;
    this.From = strFrom;
    this.FromPass = strFromPass;
    this.To = strTo;
    this.Subject = strSubject;
    this.Body = strBody;
    }


    public void Send()
    {
    SmtpClient client = new SmtpClient(SmtpServer);
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(From, FromPass);
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    MailMessage message = new MailMessage(From, To);

    //message.CC.Add(new MailAddress(To));//抄送
    //message.Bcc.Add(new MailAddress(To)); //暗抄
    message.Subject = Subject;//主题
    message.Body = Body;//内容
    message.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
    message.IsBodyHtml = true;//设置为HTML格式
    message.Priority = MailPriority.High;//优先级
    client.Send(message);
    }


    public void Send(string to,string body,string subject)
    {
    string from = From;//"123@163.com"; //发送方邮箱

    MailMessage newEmail = new MailMessage();
    newEmail.From = new MailAddress(from, from);
    newEmail.To.Add(new MailAddress(to)); //接收方邮箱一
    //newEmail.To.Add(new MailAddress("132@hotmail.com")); //接收方邮箱二
    newEmail.Subject = subject; //标题
    newEmail.Body = body; //内容
    newEmail.IsBodyHtml = true; //是否支持html
    newEmail.Priority = MailPriority.Normal; //优先级
    //发送方服务器信息
    SmtpClient smtpClient = new SmtpClient();
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Credentials = new System.Net.NetworkCredential(from, FromPass);
    smtpClient.Host = SmtpServer;//"smtp.163.com"; //主机
    smtpClient.Port = 25;
    //smtpClient.Timeout = 0;
    smtpClient.Send(newEmail); //同步发送,程序将被阻塞

    }
    }
    }

    /*
    以上内容为本人业余时间整理,部分内容来源网络,如有错误欢迎各位批评指正。


    */
  • 相关阅读:
    vue-router在两个以上子路由的情况下,跳转出错
    全局window下添加可变量
    nuxtjs 环境中添加全局axios
    nuxt.js 初始化 npm run dev 报错
    replace的回调函数。
    JS面向对象的类 实例化与继承
    DOM事件: DOM事件级别、DOM事件流、DOM事件模型、DOM事件捕获过程、自定义事件
    sync 简单实现 父子组件的双向绑定
    cube-ui 重构饿了吗Webapp的 scroll-nav域名插槽问题
    在element table中导出指定列信息
  • 原文地址:https://www.cnblogs.com/Lin267307031/p/3025859.html
Copyright © 2020-2023  润新知