C# 发送邮件帮助类

更新时间:2023-12-21 下载TXT文档 下载Word文档

C#发送邮件帮助类

private string _subject;
private string _body;
private string _from;
private string _fromName;
private string _recipientName;
private string _serverHost;
private int _serverPort;
private string _username;
private string _password;
private bool _isBodyHtml;
private string _recipient;
private List<string> _attachment = new List<string> { };

#region 获取或设置邮件的主题
/// <summary>
/// 获取或设置邮件的主题
/// </summary>
public string Subject
{
    get { return this._subject; }
    set { this._subject = value; }
}
#endregion

#region 获取或设置邮件的正文内容
/// <summary>
/// 获取或设置邮件的正文内容
/// </summary>
public string Body
{
    get { return this._body; }
    set { this._body = value; }
}
#endregion

#region 获取或设置发件人的邮件地址
/// <summary>
/// 获取或设置发件人的邮件地址
/// </summary>
public string From
{
    get { return this._from; }
    set { this._from = value; }
}
#endregion

#region 获取或设置发件人的姓名
/// <summary>
/// 获取或设置发件人的姓名
/// </summary>
public string FromName
{
    get { return this._fromName; }
    set { this._fromName = value; }
}
#endregion

#region 获取或设置收件人的姓名
/// <summary>
/// 获取或设置收件人的姓名
/// </summary>
public string RecipientName
{
    get { return this._recipientName; }
    set { this._recipientName = value; }
}
#endregion

#region 获取或设置收件人的邮件地址
/// <summary>
/// 获取或设置收件人的邮件地址
/// </summary>
public string Recipient
{
    get { return this._recipient; }
    set { this._recipient = value; }
}
#endregion

#region 获取或设置邮件服务器主机地址
/// <summary>
/// 获取或设置邮件服务器主机地址
/// </summary>
public string ServerHost
{
    get { return this._serverHost; }
    set { this._serverHost = value; }
}
#endregion

#region 获取或设置邮件服务器的端口号
/// <summary>
/// 获取或设置邮件服务器的端口号
/// </summary>
public int ServerPort
{
    set { this._serverPort = value; }
    get { return this._serverPort; }
}
#endregion

#region 获取或设置SMTP认证时使用的用户名
/// <summary>
/// 获取或设置SMTP认证时使用的用户名
/// </summary>
public string Username
{
    get { return this._username; }
    set
    {
        if (value.Trim() != "")
        {
            this._username = value.Trim();
        }
        else
        {
            this._username = "";
        }
    }
}
#endregion

#region 获取或设置SMTP认证时使用的密码
/// <summary>
/// 获取或设置SMTP认证时使用的密码。
/// </summary>
public string Password
{
    set { this._password = value; }
    get { return this._password; }
}
#endregion

#region 获取或设置指示邮件正文是否为 Html 格式的值
/// <summary>
/// 获取或设置指示邮件正文是否为 Html 格式的值
/// </summary>
public bool IsBodyHtml
{
    get { return this._isBodyHtml; }
    set { this._isBodyHtml = value; }
}
#endregion

#region 获取电子邮件附件
/// <summary>
/// 获取电子邮件附件。
/// </summary>
public List<string> Attachment
{
    get { return this._attachment; }
    set { this._attachment = value; }
}
#endregion

#region 添加电子邮件附件
/// <summary>
/// 添加电子邮件附件。
/// </summary>
/// <param name="fileList">文件列表。</param>
/// <returns>是否添加成功。</returns>
public bool AddAttachment(params string[] fileList)
{
    if (fileList.Length > 0)
    {
        foreach (string file in fileList)
        {
            if (file != null)
            {
                this._attachment.Add(file);
            }
            else
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        return false;
    }
}
#endregion

#region 发送电子邮件
/// <summary>
/// 发送电子邮件。
/// </summary>
/// <returns>是否发送成功。</returns>
public bool Send()
{
    //初始化 MailMessage 对象。
    MailMessage mail = new MailMessage();
    Encoding encoding = Encoding.GetEncoding("utf-8");
    mail.From = new MailAddress(this.From, this.FromName, encoding);
    mail.To.Add(new MailAddress(this.Recipient, this.RecipientName));
    mail.Subject = this.Subject;
    mail.IsBodyHtml = this.IsBodyHtml;
    mail.Body = this.Body;
    mail.Priority = MailPriority.Normal;
    mail.BodyEncoding = encoding;
    if (this.Attachment.Count > 0)
    {
        foreach (string file in this.Attachment)
        {
            mail.Attachments.Add(new Attachment(file));
        }
    }
    //初始化 SmtpClient 对象。
    SmtpClient smtp = new SmtpClient();
    smtp.Host = this.ServerHost;
    smtp.Port = this.ServerPort;
    smtp.Credentials = new NetworkCredential(this.Username, this.Password);
    if (smtp.Port != 25)
    {
        smtp.EnableSsl = true;
    }
    try
    {
        smtp.Send(mail);
    }
    catch (SmtpException ex)
    {
        string message = ex.Message;
        return false;
    }
    return true;
}
#endregion



以上就是短码网小编为大家整理的《C# 发送邮件帮助类》相关内容,希望大家喜欢。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!

C# 发送邮件帮助类》文档下载仅供参考学习,下载后请在24小时内删除。

转载注明出处:https://www.duanma.net/article/e4ccc79d995.html

回到顶部