C#调用Mail发送QQ邮件

需要用到:

1.System.Net.Mail;

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

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

1.打开qq邮箱:


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



代码:

 /// 
        /// 发送邮件,成功返回true,否则false
        /// 
        /// 收件人
        /// 内容
        /// 标题
        /// 是否join
        /// 附件
        /// 姓名
        /// 结果
        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("收件人", "内容, "标题", "抄送", "附件(附件方法我移除了)", "你的姓名");





你可能感兴趣的:(C#,c#,邮箱)