【问题标题】:SMTP gmail on windows applicationWindows应用程序上的SMTP gmail
【发布时间】:2020-05-10 15:42:08
【问题描述】:

我在尝试发送电子邮件 (gmail) 时遇到问题,如果我没有打开“允许不太安全的应用程序”,它将不允许我发送电子邮件。我做错了什么?

try

        {

            smtpClient.Host = mServer;
            smtpClient.Port = mPort;
            smtpClient.EnableSsl = isenableSsl;                            

            //Input new time out
            if (mTimeout > 0)
            {
                smtpClient.Timeout = mTimeout;
            }
            //Check Authentication Mode
            if (isAuthentication)
            {
                //Create Network Credentail if SMTP Server Turn On Authentication Mode
                NetworkCredential credentials = new NetworkCredential();
                credentials.UserName = mUserName;
                credentials.Password = mPassword;
                smtpClient.Credentials = credentials;
                smtpClient.UseDefaultCredentials = false;
            }
            else
            {
                smtpClient.UseDefaultCredentials = true;
            }


            //Configuration Mail Information
            if (string.IsNullOrEmpty(mDisplay)) mailMessage.From = new MailAddress(mFrom);
            else mailMessage.From = new MailAddress(mFrom, mDisplay);
            mailMessage.Sender = new MailAddress(mFrom);
            mailMessage.ReplyTo = new MailAddress(mFrom);

            //Set To Email Information
            if (ToEmails.Count != 0)
            {
                mailMessage.To.Clear();
                foreach (string mail in ToEmails)
                {
                    mailMessage.To.Add(mail);
                }
            }

            //Set Cc Email Information
            mailMessage.CC.Clear();
            foreach (string mail in CcEmails)
            {
                mailMessage.CC.Add(mail);
            }
            //Set Bcc Email Information
            mailMessage.Bcc.Clear();
            foreach (string mail in BccEmails)
            {
                mailMessage.Bcc.Add(mail);
            }
            //Set Mail Information
            mailMessage.Subject = mSubject;
            mailMessage.Body = mBody;

            //Configuration Mail Option
            mailMessage.IsBodyHtml = isBodyHtml;
            mailMessage.SubjectEncoding = mSubjectEncoding;
            mailMessage.BodyEncoding = mBodyEncoding;
            mailMessage.Priority = mPriority;
            mailMessage.DeliveryNotificationOptions = mDeliveryNotificationOptions;

            //Clear Attachment File
            mailMessage.Attachments.Clear();

            //Add Attachments Internal File
            AddAttachImage(ref mailMessage);
            //Add Attachments External File
            if (attachmentFiles.Count > 0)
            {
                AddAttachFile(ref mailMessage);
            }
            //Link Event Handler
            smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);

            //Send Message Fuction

            SetLogfileSendEmail(smtpClient, mailMessage);


            ServicePointManager.ServerCertificateValidationCallback =
            delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
            { return true; };

            smtpClient.Send(mailMessage);

        }
        catch (Exception ex)
        {
            strInformation.Append("(Error) Method smtpClient_SendCompleted : " + ex.Message);
            WriteLogFile();
            throw new Exception("SMTP Exception: " + ex.Message);
        }
    }

它也给了我这个

SMTP 异常:SMTP 服务器需要安全连接或客户端未通过身份验证。 服务器响应为:需要 5.7.0 身份验证。

我希望能够发送电子邮件,而不必在 gmail 帐户上打开“允许不太安全的应用程序”选项。

是否有任何其他方式让第三方应用发送电子邮件而无需“允许不太安全的应用”??

【问题讨论】:

  • 我希望能够发送电子邮件,而不必在 gmail 帐户上打开“允许不太安全的应用程序”选项。抱歉我之前没说清楚。

标签: c# .net smtp smtpclient smtp-auth


【解决方案1】:

如果您(发件人)使用 gmail 提供商,请尝试激活此选项

https://www.google.com/settings/security/lesssecureapps

【讨论】:

  • 我希望能够发送电子邮件,而不必在 gmail 帐户上打开“允许不太安全的应用程序”选项。我在我的帖子上编辑了它。对不起:(。
  • 我明白了。我在学校项目中遇到了同样的问题,使用 gmail 发送邮件的唯一方法是激活此选项。祝你好运
  • 太糟糕了。
  • 第三方应用程序有没有其他方法可以发送电子邮件而无需“允许不太安全的应用程序”??
猜你喜欢
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 2015-01-30
  • 2015-05-06
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多