温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用C#发送邮箱

发布时间:2021-06-21 18:20:46 来源:亿速云 阅读:108 作者:Leah 栏目:大数据

这篇文章将为大家详细讲解有关如何使用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; }


    }

关于如何使用C#发送邮箱就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI