C#发送邮件源码

Posted 凌风95

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#发送邮件源码相关的知识,希望对你有一定的参考价值。

介绍

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。
SMTP服务器则是遵循SMTP协议的发送邮件服务器,用来发送或中转发出的电子邮件。

发送邮件的基本步骤

第一:需要指明邮件SMTP服务器地址:smtp.163.com
第二:开启SSL安全连接。
第三:用户凭证。

SmtpClient是什么

Socket → TcpListener → SmtpClient

简单的邮件发送

        /// <summary>
        /// 最基本的发送邮件的方法
        /// </summary>
        public static void Send163Demo()
        {
            string user = "*****@163.com";//替换成你的hotmail用户名
            string password = "******";//替换成你的hotmail密码
            string host = "smtp.163.com";//设置邮件的服务器
            string mailAddress = "*****@163.com"; //替换成你的hotmail账户
            string ToAddress = "*******@163.com";//目标邮件地址。

            SmtpClient smtp = new SmtpClient(host);
            smtp.EnableSsl = true; //开启安全连接。
            smtp.Credentials = new NetworkCredential(user, password); //创建用户凭证
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //使用网络传送
            MailMessage message = new MailMessage(mailAddress, ToAddress, "标题", "发送内容");  //创建邮件
            smtp.Send(message); //发送邮件   异步发送邮件 smtp.SendAsync(message, "huayingjie"); //这里简单修改下,发送邮件会变的很快。
            Console.WriteLine("邮件发送成功!");
        }

下面是对代码的封装,进行对hotmail、163、QQ发送邮件

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

namespace SendMail
{
    public class MailModel
    {
        /// <summary>
        /// 用户名,根据需要看是否需要带@hotmail.com
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 服务器
        /// </summary>
        public string Host { get; set; }

        /// <summary>
        /// 邮箱账户
        /// </summary>
        public string FromMailAddress { get; set; }

        /// <summary>
        /// 目标账户
        /// </summary>
        public IList<string> ToAddress { get; set; }

        /// <summary>
        /// 是否开启安全连接
        /// </summary>
        public bool EnableSsl { get; set; }

        /// <summary>
        /// 传输协议
        /// </summary>
        public SmtpDeliveryMethod DeliveryMethod { get; set; }

        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Body { get; set; }

        public MailModel() {
            ToAddress = new List<string>();
        }

        public MailModel(
            string userName,
            string password,
            string host,
            string mailAddress,
            IList<string> toAddress,
            bool enableSsl,
            SmtpDeliveryMethod deliveryMethod,
            int port,
            string title,
            string content
            )
        {
            UserName = userName;
            Password = password;
            Host = host;
            FromMailAddress = mailAddress;
            ToAddress = toAddress;
            EnableSsl = enableSsl;
            DeliveryMethod = deliveryMethod;
            Port = port;
            Subject = title;
            Body = content;
        }
    }
}
using SendMail;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;

namespace SendMailDemo
{
    class SendMail
    {
        public SendMail() { }

        /// <summary>
        /// 最基本的发送邮件的方法
        /// </summary>
        public static void Send163Demo()
        {
            string user = "*****@163.com";//替换成你的hotmail用户名
            string password = "******";//替换成你的hotmail密码
            string host = "smtp.163.com";//设置邮件的服务器
            string mailAddress = "*****@163.com"; //替换成你的hotmail账户
            string ToAddress = "*******@163.com";//目标邮件地址。

            SmtpClient smtp = new SmtpClient(host);
            smtp.EnableSsl = true; //开启安全连接。
            smtp.Credentials = new NetworkCredential(user, password); //创建用户凭证
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //使用网络传送
            MailMessage message = new MailMessage(mailAddress, ToAddress, "标题", "发送内容");  //创建邮件
            smtp.Send(message); //发送邮件
            Console.WriteLine("邮件发送成功!");
        }

        //可以
        public static void SendHotmail()
        {
            string user = "*****@hotmail.com";
            string password = "*****";
            string host = "smtp.live.com";
            string mailAddress = "*****@hotmail.com";
            IList<string> toAddress = new List<string>();
            toAddress.Add("*****@163.com");
            bool enableSsl = true;
            SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
            int post = 465;
            string title = "标题";
            string content = "发送内容";
            MailModel mailModel = new MailModel(user, password, host, mailAddress, toAddress,
                enableSsl, deliveryMethod, post, title, content);

            SennMail(mailModel);
        }

        //可以
        public static void Send163()
        {
            string user = "*****@163.com";
            string password = "*****";
            string host = "smtp.163.com";
            string mailAddress = "*****@163.com";
            IList<string> toAddress = new List<string>();
            toAddress.Add("*****@163.com");
            bool enableSsl = true;
            SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
            int post = 465;
            string title = "标题";
            string content = "发送内容";
            MailModel mailModel = new MailModel(user, password, host, mailAddress, toAddress,
                enableSsl, deliveryMethod, post, title, content);

            SennMail(mailModel);
        }

        public static void SendQQ()
        {
            string user = "*****@qq.com";
            string password = "*****";
            string host = "smtp.qq.com";
            string mailAddress = "*****@qq.com";
            IList<string> toAddress = new List<string>();
            toAddress.Add("*****@qq.com");
            bool enableSsl = true;
            SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
            int post = 465;
            string title = "标题";
            string content = "发送内容";
            MailModel mailModel = new MailModel(user, password, host, mailAddress, toAddress,
                enableSsl, deliveryMethod, post, title, content);

            SennMail(mailModel); ;
        }

        public static void SennMail(MailModel mailModel)
        {
            SmtpClient smtp = new SmtpClient(mailModel.Host);
            smtp.EnableSsl = mailModel.EnableSsl;
            smtp.Credentials = new NetworkCredential(mailModel.UserName, mailModel.Password);
            smtp.DeliveryMethod = mailModel.DeliveryMethod;
            //smtp.Port = mailModel.Port;//有些时候设置端口会出现很奇怪的问题,这个与服务器的设置有关,建议不要设置端口。
            MailAddress mailAddress = new MailAddress(mailModel.FromMailAddress);

            MailMessage message = new MailMessage();
            message.From = mailAddress;
            foreach (string toAddress in mailModel.ToAddress)
            {
                message.To.Add(toAddress);
            }
            message.Subject = mailModel.Subject;
            message.Body = mailModel.Body;
            //message.IsBodyhtml = true;
            smtp.Send(message);
            Console.WriteLine("邮件发送成功!");
        }
    }
}
using System;

namespace SendMailDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            SendMail.Send163Demo();
            //SendMail.Send163();
            //SendMail.SendQQ();
            //SendMail.SendHotmailDemo();
            Console.ReadLine();
        }
    }
}

 

以上是关于C#发送邮件源码的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何使用 Outlook 发送代码片段?

C# 如何配置公司邮箱发送邮件配置

C# 获得目录创建时间的源码片段

C#中发送邮件,包含Html代码

从c#代码发送电子邮件[重复]

c#代码怎么通过outlook发邮件