如何用qq代理发送邮件

      今天我想写一篇服务器发送验证邮件的的文章,我查阅过其他博客里面写的文章,都是可以实现的,但是对于初学者来说是一个很痛苦的事情,很多代码看不懂,原因有多种,写的多,写的乱,然后就不想往下看了。我今天详细的为大家写一下该如何去实现这个功能。

     1.要准备两个qq邮箱,一个用于发送验证邮件,一个用于接收测试邮件。我先准备了两个。

     2.发送验证的qq邮箱必须开通IMAP/SMTP服务服务。

        (1).打开qq邮箱=》设置=》账户=》开启服务然后获得授权码,记住授权码。

          

     3.本人用MVC进行了简单的测试。

      

   /// <summary>
        /// 代理服务器
        /// 如qq,163等
        /// </summary>
        /// <summary>发送的邮箱</summary>
        /// <summary>授权码qq邮箱获得的</summary>
        /// <summary>接收的邮箱</summary>
        /// <summary>标题</summary>
        /// <summary>邮件</summary>
        /// <returns></returns>
        public void SendSMTPEMail(string strSmtpServer, string strFrom, 
            string strFromPass, string strto, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;

            //使用文件路径发送附件
            //AlternateView item = new AlternateView(@"D:\软件\(软电话)eyebeam1.5.rar", "application/x-rar-compressed");
            //message.AlternateViews.Add(item);
            //message.AlternateViews.Dispose();

            //使用Stream发送附件 
            //Attachment letter = new Attachment(FileUploadLetter.FileContent, FileUploadLetter.PostedFile.ContentType);
            //letter.ContentDisposition.Inline = true;
            //letter.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            ////inline.ContentId = "1";
            //letter.ContentType.MediaType = FileUploadLetter.PostedFile.ContentType;
            //letter.ContentType.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName);
            //letter.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName);
            //message.Attachments.Add(letter);
            //message.Attachments.Dispose();

            client.Send(message);
        }
    }
View Code

  这是一个发送邮件的函数,你可以将它放在一个类中或者直接调用

  4.调用函数时传参成功即可发送邮件。

  5.不理解的可以留言。

         
原文地址:https://www.cnblogs.com/bingshu/p/6192978.html