远离Gmail SMTP服务器,还是还有其他选择?

发布于 2025-02-07 08:33:45 字数 2755 浏览 5 评论 0 原文

由于Google停止较少安全的应用程序功能功能更难更难使用其SMTP服务器发送电子邮件,我已经迁移到另一个提供商。

现在,我要获得STMP服务器需要安全的连接,否则客户端未经身份验证。服务器响应是:需要身份验证5.7.0。

该代码与Gmail的Out Out SMTP运行良好,但我的新服务器正在抱怨。我尝试了几种代码变化,结果是相同的。

private void SendEmail()
{
  MailMessage message = new MailMessage();
  SmtpClient smtpClient = new SmtpClient("smtp-relay.sendinblue.com");
  message.From = new MailAddress("[email protected]");
  message.To.Add("[email protected]");
  message.Subject = "NEW LICENCE REQUEST FROM " + ((User) this.Session["User"]).Name;
  message.Body = "LICENCE DEATIL" + Environment.NewLine + "SELLER = " + ((User) this.Session["User"]).Name + Environment.NewLine + "ID = " + this.TextBox__id.Text + Environment.NewLine + "NAME = " + this.TextBox__register_nam.Text + Environment.NewLine + "ADDRESS = " + this.TextBox__address.Text + Environment.NewLine + "LIC = " + this.TextBox__licence.Text + Environment.NewLine + "PDA = " + this.TextBox__pda.Text + Environment.NewLine + "CONTACT = " + this.TextBox__contact.Text + Environment.NewLine + "PHONE = " + this.TextBox__phone.Text + Environment.NewLine + "EMAIL = " + this.TextBox__email.Text + Environment.NewLine + "EXP = " + this.Calendar.SelectedDate.ToShortDateString() + Environment.NewLine + "AD SCREEN = " + this.CheckBox__AdScreen.Checked.ToString() + Environment.NewLine + "Biometrics = " + this.CheckBox__Biometrics.Checked.ToString() + Environment.NewLine + "Debit = " + this.CheckBox__Debit.Checked.ToString() + Environment.NewLine + "Draft = " + this.CheckBox__DraftControl.Checked.ToString() + Environment.NewLine + "KitchenScreen = " + this.CheckBox__KitchenScreen.Checked.ToString() + Environment.NewLine + "LiquorControl = " + this.CheckBox__LiquorControl.Checked.ToString() + Environment.NewLine + "WinAuthorize = " + this.CheckBox__WinAuthorize.Checked.ToString() + Environment.NewLine + "Lite = " + this.CheckBox__Lite.Checked.ToString() + Environment.NewLine + "CashNoBill = " + this.CheckBox__DisableCashNoBill.Checked.ToString();
  smtpClient.Port = 587;
  smtpClient.Credentials = (ICredentialsByHost) new NetworkCredential("[email protected]", "XSnfc213213216");
  smtpClient.EnableSsl = true;
  smtpClient.Send(message);
}

出于隐私原因,凭证已更改。

Since Google discontinued the Less secure apps feature and made is more difficult to send e-mail using their smtp server, I've migrated over to another provider.

Now I'm getting The STMP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication required.

This code was working well with Gmail's outgoing SMTP but my new server is complaining. I've tried several code variations and the result is the same.

private void SendEmail()
{
  MailMessage message = new MailMessage();
  SmtpClient smtpClient = new SmtpClient("smtp-relay.sendinblue.com");
  message.From = new MailAddress("[email protected]");
  message.To.Add("[email protected]");
  message.Subject = "NEW LICENCE REQUEST FROM " + ((User) this.Session["User"]).Name;
  message.Body = "LICENCE DEATIL" + Environment.NewLine + "SELLER = " + ((User) this.Session["User"]).Name + Environment.NewLine + "ID = " + this.TextBox__id.Text + Environment.NewLine + "NAME = " + this.TextBox__register_nam.Text + Environment.NewLine + "ADDRESS = " + this.TextBox__address.Text + Environment.NewLine + "LIC = " + this.TextBox__licence.Text + Environment.NewLine + "PDA = " + this.TextBox__pda.Text + Environment.NewLine + "CONTACT = " + this.TextBox__contact.Text + Environment.NewLine + "PHONE = " + this.TextBox__phone.Text + Environment.NewLine + "EMAIL = " + this.TextBox__email.Text + Environment.NewLine + "EXP = " + this.Calendar.SelectedDate.ToShortDateString() + Environment.NewLine + "AD SCREEN = " + this.CheckBox__AdScreen.Checked.ToString() + Environment.NewLine + "Biometrics = " + this.CheckBox__Biometrics.Checked.ToString() + Environment.NewLine + "Debit = " + this.CheckBox__Debit.Checked.ToString() + Environment.NewLine + "Draft = " + this.CheckBox__DraftControl.Checked.ToString() + Environment.NewLine + "KitchenScreen = " + this.CheckBox__KitchenScreen.Checked.ToString() + Environment.NewLine + "LiquorControl = " + this.CheckBox__LiquorControl.Checked.ToString() + Environment.NewLine + "WinAuthorize = " + this.CheckBox__WinAuthorize.Checked.ToString() + Environment.NewLine + "Lite = " + this.CheckBox__Lite.Checked.ToString() + Environment.NewLine + "CashNoBill = " + this.CheckBox__DisableCashNoBill.Checked.ToString();
  smtpClient.Port = 587;
  smtpClient.Credentials = (ICredentialsByHost) new NetworkCredential("[email protected]", "XSnfc213213216");
  smtpClient.EnableSsl = true;
  smtpClient.Send(message);
}

Credentials have been altered for privacy reasons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2025-02-14 08:33:45

如果您想留在Gmail SMTP中,您有一些选择。无论哪种方式,您的错误

5.7.0所需的身份验证。

意味着您需要进行身份验证才能使用该SMTP服务器。这意味着您需要配置OAuth2。与Google SMTP服务器一起使用的Simlar to XoAuth版本的Simlar。我将检查该新服务器的文档,它应该告诉您如何配置授权。

的Google SMTP选项

在删除较不安全的应用程序功能之后,下面

是使用Google SMPT服务器的两种方法。 XOAuth2

以下代码显示了如何将XOAuth2与Google SMTP服务器一起使用。它要求您在Google Cloud Cloud Console 上创建安装的应用程序凭证。

using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;


var path = @"C:\YouTube\dev\credentials.json";
var scopes = new[] { "https://mail.google.com/" };
var userName = "test2";

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromFile(path).Secrets,
    scopes,
    userName,
    CancellationToken.None,
    new FileDataStore(Directory.GetCurrentDirectory(), true)).Result;

credential.GetAccessTokenForRequestAsync();

var message = new EmailMessage()
{
    From = "[email protected]",
    To = "[email protected]",
    MessageText = "test",
    Subject = "test"
};

try
{
    using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        
        var oauth2 = new SaslMechanismOAuth2 ("[email protected]", credential.Token.AccessToken);
        await client.AuthenticateAsync (oauth2, CancellationToken.None);
        
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

   
}
catch (Exception ex)
{
    int i = 1;  // throws MailKit.Security.AuthenticationException here
}


public class EmailMessage
{
    public string To { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string MessageText { get; set; }

    public MimeMessage GetMessage()
    {
        var body = MessageText;
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("test", From));
        message.To.Add(new MailboxAddress("test", To));
        message.Subject = Subject;
        message.Body = new TextPart("plain") { Text = body };
        return message;
    }
}

应用程序密码

另一个选项是创建 apps passwords密码您之前使用的密码的位置。

using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("[email protected]", "appspassword");
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

smtp用户名的快速修复和密码不接受错误

If you would like to stay with gmail smtp you have a few options. Either way your error

5.7.0 Authentication required.

Means that you need to be authentication in order to use that smtp server. Which means you would need to configure Oauth2. Something simlar to the xoauth version below that works with googles smtp server. I would check the docs for this new server it should tell you how to configure authorizaiton.

Google SMTP options below

The following are two ways to use googles smpt server after the removal of less secure apps feature.

xoauth2

The following code shows how to use XOauth2 with Googles smtp server. It requires that you create installed app credentials on Google cloud console.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;


var path = @"C:\YouTube\dev\credentials.json";
var scopes = new[] { "https://mail.google.com/" };
var userName = "test2";

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromFile(path).Secrets,
    scopes,
    userName,
    CancellationToken.None,
    new FileDataStore(Directory.GetCurrentDirectory(), true)).Result;

credential.GetAccessTokenForRequestAsync();

var message = new EmailMessage()
{
    From = "[email protected]",
    To = "[email protected]",
    MessageText = "test",
    Subject = "test"
};

try
{
    using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        
        var oauth2 = new SaslMechanismOAuth2 ("[email protected]", credential.Token.AccessToken);
        await client.AuthenticateAsync (oauth2, CancellationToken.None);
        
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

   
}
catch (Exception ex)
{
    int i = 1;  // throws MailKit.Security.AuthenticationException here
}


public class EmailMessage
{
    public string To { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string MessageText { get; set; }

    public MimeMessage GetMessage()
    {
        var body = MessageText;
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("test", From));
        message.To.Add(new MailboxAddress("test", To));
        message.Subject = Subject;
        message.Body = new TextPart("plain") { Text = body };
        return message;
    }
}

Apps password

Another option is to create an apps password and use that in place of the password you were using previously.

using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("[email protected]", "appspassword");
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

Quick fix for SMTP username and password not accepted error

清旖 2025-02-14 08:33:45

看起来应用程序密码有效。您需要激活2个因子身份验证,然后Google为您提供16位数字密码。谢谢。

Looks like apps password worked. You need to activate 2 factor authentication and then google gives you a 16 digit password. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文