C#发邮件之命名空间System.Net.Mail

1.添加一个类,取名为Email

public class Email
    {
        /// <summary>
        /// 发送方发送方服务器地址
        /// </summary>
        public string strHost { get; set; }
        /// <summary>
        /// 发送方帐号
        /// </summary>
        public string strAccount { get; set; }
        /// <summary>
        /// 发送方密码
        /// </summary>
        public string strPwd { get; set; }

        /// <summary>
        /// 发送方邮件地址
        /// </summary>
        public string strFrom { get; set; }
        /// <summary>
        /// 接收方邮件地址
        /// </summary>
        public string to { get; set; }
        /// <summary>
        /// 邮件标题
        /// </summary>
        public string title { get; set; }

        /// <summary>
        /// 邮件正文内容
        /// </summary>
        public string content { get; set; }
        ///// <summary>
        ///// 附件
        ///// </summary>
        //public string sfile { get; set; }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="strHost">发送方发送方服务器地址</param>
        /// <param name="strAccount">发送方帐号</param>
        /// <param name="strPwd">发送方密码</param>
        ///  <param name="strFrom">发送方邮件地址</param>
        /// <param name="to">接收方邮件地址</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">邮件正文内容</param>
        /// <param name="sfile">邮件附件</param>
        /// <returns></returns>
        public string sendMail(Email e)//string strHost, string strAccount, string strPwd, string strFrom, string to, string title, string content
        {
            SmtpClient smtpClient = new SmtpClient();
            if (string.IsNullOrEmpty(e.strHost))
            {
                e.strHost = "smtp.sina.cn";//新浪测试
            }
            if (string.IsNullOrEmpty(e.strAccount))
            {
                e.strAccount = "xxx";
            }
            if (string.IsNullOrEmpty(e.strPwd))
            {
                e.strPwd = "xxx";
            }
            if (string.IsNullOrEmpty(e.strFrom))
            {
                e.strFrom = "xxx";
            }
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            smtpClient.Host = e.strHost; ;//指定SMTP服务器
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new System.Net.NetworkCredential(e.strAccount, e.strPwd);//用户名和密码


            MailMessage mailMessage = new MailMessage(e.strFrom, e.to);
            mailMessage.Subject = e.title;//主题
            mailMessage.Body = e.content;//内容
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
            mailMessage.IsBodyHtml = true;//设置为HTML格式
            mailMessage.Priority = MailPriority.High;//优先级
            ////// 添加附件
            //if (e.sfile != "")
            //{
            //    mailMessage.Attachments.Add(new Attachment(e.sfile));
            //}
            try
            {
                smtpClient.Send(mailMessage);
                mailMessage.Dispose();//释放资源
                return "成功";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }

2.方法调用

                    Email email = new Email();//实例化我们上面的Email类
                    email.strHost = "smtp.163.com";发送方服务器地址
                    email.strAccount = "用户名";发送方用户名 ps:邮箱地址为88888888@163.com 用户名则为:88888888
                    email.to = "收件人邮箱地址";//你要给谁发邮件
                    email.title = “title”;//这个不做解释
                    email.strPwd = "000000";//邮箱授权码,这个在下面解释
                    email.strFrom = "发送方的邮件地址";
                    email.content = @"邮件内容(字写多点,字数少发不成功,不知道为什么,有朋友知道的给我留言,谢谢)";
                    email.sendMail(email);

3.授权码获取

QQ授权码:打开QQ邮箱,点击设置

选择账户

向下拉找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务  根据需要开启

163邮箱:打开163官网  <a href=" https://mail.163.com/">163官网</a>

 

我这里是已经开启的,没开启的点击开启,后面会进行手机短信验证,验证成功后,设置你自己 的邮箱授权码

到这里就结束了,小弟才疏学浅,各路好汉应取其精华,去其糟粕,希望能够帮助到需要的人!!!

posted @ 2018-10-24 14:41  sunshuaize  阅读(379)  评论(0编辑  收藏  举报