C#实现邮箱发送的多种方式

一、简单的邮件发送(不含授权码)

    这里不启动安全连接,有些邮箱不支持安全连接。

        /// 最基本的发送邮件的方法
        /// </summary>
        public static void Send163Demo()
        {
            string user = "asdf@qq.com";//替换成你的hotmail用户名
            string password = "1234";//替换成你的hotmail密码
            string host = "smtp.qq.cn";//设置邮件的服务器
            string mailAddress = "asdf@qq.com"; //替换成你的hotmail账户
            string ToAddress = "lsd@qq.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"); //这里简单修改下,发送邮件会变的很快。
            MessageBox.Show("邮件发送成功!");
        }

二、安全连接发送邮箱(含授权码方式)

  #region QQ邮箱邮件发送

            //pub.SendMail email = new pub.SendMail(); //引用此类所在的命名空间后new一个对象出来
            //string _sendServer = "smtp.qq.com";//服务器地址
            //string _sendUseremail = "123213@qq.com";//发件人邮箱
            string _sendUserGrant = "hxtl2hbicj";//授权码
            //string _sendToUser = "12321323@qq.com";//接收人
            //string _strSubject = "数字化采购系统KPI推送--PP";//主题
            //string _strBody = string.Empty;//发送内容

            //for (int i = 0; i < list_user.Count; i++)
            //{
            //    if (list_user[i].email != "")
            //        _sendToUser += "," + list_user[i].email;
            //}
            邮件内容头部
            //_strBody += "大家好! <br /><br /> 以下是PP模块的KPI汇总内容: <br /><br /> ";

            中间部分-获取表格
            //_strBody += getMailBody_PP(a);
            邮件内容尾部
            //_strBody += "<br /> 请关注未达成的内容项,望可以今日完成。<br />";

            email.SendQQMail("smtp.qq.com", "2342@qq.com", "234234", "23432@qq.com", "QQ邮箱服务器发送邮件", "用asp.net发送邮件,用QQ的smtp.qq.com服务器,测试成功");

            //email.SendQQMail(_sendServer, _sendUseremail,  _sendToUser, _strSubject, _strBody);

            #endregion
        }

三、含授权码服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SendMail.pub
{
    class SendMail
    {
        //public void SendQQMail(string strSmtpServer, string strFrom, string strto,
        //    string strSubject, string strBody)
        //{
        //    SmtpClient smtpClient = new SmtpClient();

        //    smtpClient.EnableSsl = true;

        //    smtpClient.UseDefaultCredentials = false;//先设置

        //    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指定电子邮件发送方式

        //    smtpClient.Host = strSmtpServer; //指定SMTP服务器

        //    smtpClient.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass); //用户名和授权码

        //    // 发送邮件设置

        //    MailMessage mailMessage = new MailMessage(strFrom, strto); // 发送人和收件人

        //    mailMessage.Subject = strSubject; //主题

        //    mailMessage.Body = strBody;//内容
        //    mailMessage.CC.Add("liujihui@shinbada.com");

        //    mailMessage.BodyEncoding = Encoding.UTF8; //正文编码

        //    mailMessage.IsBodyHtml = true; //设置为HTML格式

        //    mailMessage.Priority = MailPriority.Low; //优先级

        //    smtpClient.Send(mailMessage);
        //}

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SendALLMail(MailModel model)
        {
            try
            {
                MailAddress receiver = new MailAddress(model.ReceiverAddress, model.ReceiverName);
                MailAddress sender = new MailAddress(model.SenderAddress, model.SenderName);
                MailMessage message = new MailMessage();
                message.From = sender;//发件人
                message.To.Add(receiver);//收件人
                //message.CC.Add(sender);//抄送人
                message.Subject = model.Title;//标题
                message.Body = model.Content;//内容
                message.IsBodyHtml = true;//是否支持内容为HTML

                SmtpClient client = new SmtpClient();
                client.Host = "smtp.qq.com";
                client.Port = 465;
                client.EnableSsl = true;//是否启用SSL
                client.Timeout = 10000;//超时
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(model.SenderAddress, model.SenderPassword);
                client.Send(message);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

}
四、表字段

 public struct MailModel
    {
        /// <summary>
        /// 收件人地址
        /// </summary>
        public string ReceiverAddress { get; set; }
        /// <summary>
        /// 收件人姓名
        /// </summary>
        public string ReceiverName { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 发件人地址(非必填)
        /// </summary>
        public string SenderAddress { get; set; }
        /// <summary>
        /// 发件人姓名(非必填)
        /// </summary>
        public string SenderName { get; set; }
        /// <summary>
        /// 发件人密码(非必填)
        /// </summary>
        public string SenderPassword { get; set; }
        public string host { get; set; }


    }

 

转载于:https://my.oschina.net/xiaoxiezi/blog/3082712

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值