C#调用Mail发送QQ邮件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26744901/article/details/50069669

需要用到:

1.System.Net.Mail;

2.QQ邮箱的POP3/SMTP服务码

QQ邮箱的POP3/SMTP服务码获取方法:

1.打开qq邮箱:


2.进入设置页面--》账户:(往下翻)开启服务POP3/SMTP服务,QQ邮箱会给一个POP3/SMTP服务码保存下来



代码:

 /// <summary>
        /// 发送邮件,成功返回true,否则false
        /// </summary>
        /// <param name="to">收件人</param>
        /// <param name="body">内容</param>
        /// <param name="title">标题</param>
        /// <param name="whichEmail">是否join</param>
        /// <param name="path">附件</param>
        /// <param name="Fname">姓名</param>
        /// <returns>结果</returns>
        public static bool SentMailHXD(string to, string body, string title, string whichEmail, string path, string Fname)
        {
            bool retrunBool = false;
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            string strFromEmail = "";//你的邮箱
            string strEmailPassword = "";//QQPOP3/SMTP服务码
            try
            {
                mail.From = new MailAddress("" + Fname + "<" + strFromEmail + ">");
                mail.To.Add(new MailAddress(to));
                mail.BodyEncoding = Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Priority = MailPriority.Normal;
                mail.Body = body;
                mail.Subject = title;
                smtp.Host = "smtp.qq.com";
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = new System.Net.NetworkCredential(strFromEmail, strEmailPassword);
                //发送邮件
                smtp.Send(mail);   //同步发送
                retrunBool = true;
            }
            catch (Exception ex)
            {
                retrunBool = false;
            }
            // smtp.SendAsync(mail, mail.To); //异步发送 (异步发送时页面上要加上Async="true" )
            return retrunBool;
        }

调用方法即可:

   SentMailHXD("收件人", "内容, "标题", "抄送", "附件(附件方法我移除了)", "你的姓名");</span>





阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页