C#可以利用网易163邮箱SMTP协议的465端口发送Email,用到的发送邮件库为MailMessage,我们设置MailMessage发件箱的SMTP服务器为smtp.163.com,端口为465便可轻松发送邮件。

public static bool WithAttachment(SendInfo sendInfo, FromEmail fromEmail = null)
{
    if (sendInfo == null || sendInfo.ToUser.IsNullOrEmpty()) return false;
    if (fromEmail == null)
    {
        fromEmail = new FromEmail();
        fromEmail.Account = ConfigurationManager.AppSettings["EmailAccount"];
        fromEmail.Name = ConfigurationManager.AppSettings["EmailAccountName"];
        fromEmail.Pwd = ConfigurationManager.AppSettings["EmailPwd"];
        fromEmail.SmtpServer = ConfigurationManager.AppSettings["SmtpClient"];
        fromEmail.SmtpPort = ConfigurationManager.AppSettings["SmtpPort"];
    }
    if (fromEmail.Account.IsNullOrEmpty() || fromEmail.Pwd.IsNullOrEmpty()) return false;
    if (!string.IsNullOrEmpty(sendInfo.FromUserName)) fromEmail.Name = sendInfo.FromUserName;
    MailAddress fromAddress = new MailAddress(fromEmail.Account, fromEmail.Name);
    MailAddress toAddress = new MailAddress(sendInfo.ToUser, sendInfo.ToUserName);
    MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
    mailMessage.Subject = sendInfo.Title;

    if (!string.IsNullOrEmpty(sendInfo.HtmlPath))
    {
        string contentFilePath = (sendInfo.HtmlPath);
        StreamReader read = new StreamReader(contentFilePath, Encoding.GetEncoding("GB2312"));
        string mailBody = read.ReadToEnd();
        read.Close();
        mailMessage.Body = mailBody;
        mailMessage.IsBodyHtml = true;
    }
    else mailMessage.Body = sendInfo.Content;
    mailMessage.IsBodyHtml = sendInfo.IsHtml;
    mailMessage.SubjectEncoding = Encoding.UTF8;
    mailMessage.BodyEncoding = Encoding.GetEncoding("GB2312");
    Attachment data = null;
    if (sendInfo.FilePaths != null)
    {
        foreach (string filePath in sendInfo.FilePaths)
        {
            data = new Attachment(filePath, MediaTypeNames.Application.Octet);
            mailMessage.Attachments.Add(data);
        }
    }

    if (fromEmail.SmtpPort.IsNullOrEmpty())
    {
        if (fromEmail.Account.ToLower().EndsWith("163.com")) fromEmail.SmtpPort = "456";
        else fromEmail.SmtpPort = "587";
    }

    if (fromEmail.SmtpServer.IsNullOrEmpty()) fromEmail.SmtpServer = "smtp." + fromEmail.Account.SplistToList("@").LastOrDefault();
    SmtpClient client = new SmtpClient(fromEmail.SmtpServer);
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Port = int.Parse(fromEmail.SmtpPort);
    client.Credentials = new System.Net.NetworkCredential(fromEmail.Account, fromEmail.Pwd);
    try
    {
        client.Send(mailMessage);
        return true;
    }
    catch (SmtpException err)
    {
        Log.Write.WriteLine("邮件服务器异常.信息未发出! 错误详细信息" + err.Message);
        return false;
    }
    finally
    {
        mailMessage.Dispose();
        client.Dispose();
        if (data != null) data.Dispose();
    }
}

我们发件箱的对象(FromEmail)定义

public class FromEmail
{
    string account = string.Empty;
    string name = string.Empty;
    string pwd = string.Empty;
    string smtpServer = string.Empty;
    string smtpPort = string.Empty;
    DateTime? lastUseTime = null;
    public string Account { get => account; set => account = value; }
    public string Name { get => name; set => name = value; }
    public string Pwd { get => pwd; set => pwd = value; }
    public string SmtpServer { get => smtpServer; set => smtpServer = value; }
    public string SmtpPort { get => smtpPort; set => smtpPort = value; }
    public DateTime? LastUseTime { get => lastUseTime; set => lastUseTime = value; }
}
收件箱ToEmail定义



public class SendInfo
{
    private List<string> filePaths = new List<string>();
    private string title = null;
    private string content = null;
    private string toUser = null;
    private string toUserName = null;
    private string fromUserName = null;
    private string htmlPath = null;
    private bool isHtml = false;
    private int timeOut = 3000;

    public List<string> FilePaths
    {
        get
        {
            return filePaths;
        }

        set
        {
            filePaths = value;
        }
    }

    public string Title
    {
        get
        {
            return title;
        }

        set
        {
            title = value;
        }
    }

    public string Content
    {
        get
        {
            return content;
        }

        set
        {
            content = value;
        }
    }

    public string ToUser
    {
        get
        {
            return toUser;
        }

        set
        {
            toUser = value;
        }
    }

    public string ToUserName
    {
        get
        {
            return toUserName;
        }

        set
        {
            toUserName = value;
        }
    }

    public string HtmlPath
    {
        get
        {
            return htmlPath;
        }

        set
        {
            htmlPath = value;
        }
    }

    public bool IsHtml
    {
        get
        {
            return isHtml;
        }

        set
        {
            isHtml = value;
        }
    }

    public int TimeOut { get => timeOut; set => timeOut = value; }
    public string FromUserName { get => fromUserName; set => fromUserName = value; }
}

C#使用网易163的STMP邮箱发邮件的重点是要记住发送端口号465便可,其他代码可以在网上搜索C# SMTP发送邮件即可找到对应的代码。