• 邮件发送(C#)


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

    namespace Haitai
    {
    public class EmailClient
    {
    public EmailClient(string host, int port, bool enableSsl, string username, string password)
    {
    _host = host;
    _port = port;
    _enableSsl = enableSsl;
    _username = username;
    _password = password;
    }

    public event SendCompletedEventHandler SendCompleted;

    private string _host;
    private int _port;
    private bool _enableSsl;
    private string _username;
    private string _password;

    private SmtpClient _smtpClient;
    private SmtpClient SmtpClient
    {
    get
    {
    if (_smtpClient == null)
    {
    _smtpClient = new SmtpClient();
    _smtpClient.Host = _host;
    _smtpClient.Port = _port;
    if (_username != null && _password != null)
    {
    _smtpClient.Credentials = new NetworkCredential(_username, _password);
    }
    _smtpClient.EnableSsl = _enableSsl;
    _smtpClient.SendCompleted += (sender, e) =>
    {
    if (SendCompleted != null)
    {
    SendCompleted(this, e);
    }
    };
    }
    return _smtpClient;
    }
    }

    private MailMessage ComposeMessage(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
    {
    MailMessage message = new MailMessage();
    message.From = new MailAddress(from, fromName, Encoding.UTF8);
    foreach (string to in toList.Keys)
    {
    message.To.Add(new MailAddress(to, toList[to], Encoding.UTF8));
    }
    if (ccList != null)
    {
    foreach (string cc in ccList.Keys)
    {
    message.CC.Add(new MailAddress(cc, ccList[cc], Encoding.UTF8));
    }
    }
    message.Subject = subject;
    message.SubjectEncoding = Encoding.UTF8;
    message.Body = body;
    message.BodyEncoding = Encoding.UTF8;
    if (attachments != null)
    {
    foreach (string name in attachments.Keys)
    {
    message.Attachments.Add(new Attachment(attachments[name], name));
    }
    }
    return message;
    }

    public void Send(string from, string fromName, string to, string toName, string subject, string body)
    {
    Dictionary<string, string> toList = new Dictionary<string, string>();
    toList.Add(to, toName);
    Send(from, fromName, toList, null, subject, body, null);
    }

    public void Send(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
    {
    SmtpClient.Send(ComposeMessage(from, fromName, toList, ccList, subject, body, attachments));
    }

    public MailMessage SendAsync(string from, string fromName, string to, string toName, string subject, string body)
    {
    Dictionary<string, string> toList = new Dictionary<string, string>();
    toList.Add(to, toName);
    return SendAsync(from, fromName, toList, null, subject, body, null);
    }

    public MailMessage SendAsync(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
    {
    MailMessage message = ComposeMessage(from, fromName, toList, ccList, subject, body, attachments);
    SmtpClient.SendAsync(message, message);
    return message;
    }
    }
    }

  • 相关阅读:
    Response.Redirect、Server.Transfer、Server.Execute的区别
    js删除Array指定位置元素方法
    用Json.NET将json字符串反序列化为json匿名对象
    Asp.net中编程方式调用ashx(通过webRequest)
    Server.Transfer中传递ViewState方法
    Ext.Net中Ext.net.DirectMethods无法找到DirectMethod
    IFrame网页加载完成事件
    oracle中grant授权说明
    深度剖析Byteart Retail案例:前言
    深度剖析Byteart Retail案例:仓储(Repository)及其上下文(Repository Context)
  • 原文地址:https://www.cnblogs.com/quietwalk/p/3530961.html
Copyright © 2020-2023  润新知