首页 > 代码库 > C#发送邮件

C#发送邮件

/* 创建者:菜刀居士的博客
 * 创建日期: 2014年09月05号
 * 功能:发送邮件
 *
 */


namespace Net.String.ConsoleApplication
{
    using System;
    using System.Text;
    using System.Net.Mail;
    using System.Collections.Generic;

    /// <summary>
    /// 发送邮件
    /// </summary>
    public class SendMailHelper
    {
        //自己的邮箱
        public string fromMail = "
1232573315@qq.com";
        //自己的名字
        public string fromName = "菜单";
        //邮件的标题
        public string mailText = "";

        //如果有多个人,请一一对应
        //邮件的接收信箱
        public List<string> ltoMail = new List<string>();
        //邮件的接收人
        public List<string> ltoName = new List<string>();
        //邮件的内容
        public StringBuilder body = new StringBuilder();
        //邮件的附件, 注意是文件在服务器的路径
        public List<string> lfile = new List<string>();

        public void Send()
        {
            //发件人
            MailAddress from = new MailAddress(fromMail);
            //邮箱
            MailMessage mail = new MailMessage();
            //设置标题
            mail.Subject = mailText;
            mail.From = from;

            int c = ltoMail.Count;
            //收件人
            for (int i = 0; i < c; i++)
            {
                mail.To.Add(new MailAddress(ltoMail[i], ltoName[i]));
            }
            //设置邮件的内容
            mail.Body = body.ToString();
            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            //是否为Html格式
            mail.IsBodyHtml = true;
            //设置邮件的发送级别
            mail.Priority = MailPriority.High;

            //设置附件
            int f = lfile.Count;
            for (int j = 0; j < f; j++)
            {
                mail.Attachments.Add(new Attachment(lfile[j]));
            }

            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

            SmtpClient client = new SmtpClient();
            //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
            client.Host = "smtp.qq.com";
            //设置用于 SMTP 事务的端口,默认的是 25
            client.Port = 25;
            client.UseDefaultCredentials = false;

            //这里才是真正的邮箱登陆名和密码,比如我的邮箱地址是 hbgx@hotmail, 我的用户名为 hbgx ,我的密码是 xgbh
            client.Credentials = new System.Net.NetworkCredential("
1293452213@qq.com", "123456");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            client.Send(mail);
            System.Console.WriteLine("发送成功!");
        }

        public void SendEmail(string strSmtpServer, string strFrom, string strFromPwd, string strTo, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new MailMessage(strFrom, strTo, strSubject, strBody);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Send(message);
        }
    }
}

C#发送邮件