zoukankan      html  css  js  c++  java
  • 邮件发送(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;
    }
    }
    }

  • 相关阅读:
    「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 下)
    「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 上)
    mocha_JavaScript单元测试框架
    Node介绍
    javascript 时间格式化
    MacOS安装MongoDB
    wordpress创建过程
    博客框架推荐
    github基本使用
    基本操作3-分支冲突
  • 原文地址:https://www.cnblogs.com/quietwalk/p/3530961.html
Copyright © 2011-2022 走看看