相关疑难解决方法(0)

如何根据新的安全政策在.Net中发送电子邮件?

为了更好地保护您的用户,GMail和其他邮件提供商建议将我们的所有应用程序升级到OAuth 2.0.

我是对的,这意味着System.Net.Mail不再工作,我们需要使用另一个库MailKit吗?

一般来说,我试图了解如何发送电子邮件而不允许"访问不太安全的应用程序"?

因为我有System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.smtpClient.Send(message);执行.

如果解决这个问题的唯一方法是使用MailKit,我认为这个问题将是一个很好的实用的逐步切换教程从System.Net.Mail使用MailKitGoogle.Apis.Auth.OAuth2.我不知道可能会使用一般解决方案DotNetOpenAuth吗?

我的应用程序中有以下类,对应于向任何地址(gmail,yandex和其他人)发送电子邮件:

public class EmailSender
{
    public void SendEmail(SmtpServerSettings serverSettings, SendEmailRequest emailRequest)
    {
        // Usually I have 587 port, SmtpServerName = smtp.gmail.com 
        _logger.Trace("Sending message with subject '{0}' using SMTP server {1}:{2}",
                      emailRequest.Subject,
                      serverSettings.SmtpServerName,
                      serverSettings.SmtpPort);

        try
        {
            using …
Run Code Online (Sandbox Code Playgroud)

.net c#

68
推荐指数
2
解决办法
2万
查看次数

如何使用C#中的服务帐户登录Google API - 凭据无效

我试图通过简单的服务帐户登录来使用C#,Google API和Google Analytics(分析)来打败自己.我的公司已经将数据导入到Analytics中,我可以使用他们的查询资源管理器查询信息,但是.Net的入门不会随处可见.我正在使用谷歌生成的带有PKI的json文件,因为文档说这样的服务帐户是与Googla API进行计算机到计算机通信的正确方法.代码snipet:

public static GoogleCredential _cred;
public static string _exePath;

static void Main(string[] args) {
    _exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace(@"file:\", "");
    var t = Task.Run(() => Run());
    t.Wait();
}

private static async Task Run() {
    try {
        // Get active credential
        using (var stream = new FileStream(_exePath + "\\Default-GASvcAcct-508d097b0bff.json", FileMode.Open, FileAccess.Read)) {
            _cred = GoogleCredential.FromStream(stream);
        }
        if (_cred.IsCreateScopedRequired) {
        _cred.CreateScoped(new string[] { AnalyticsService.Scope.Analytics });
        }
        // Create the service
        AnalyticsService service = new AnalyticsService(
            new BaseClientService.Initializer() {
                HttpClientInitializer = _cred, …
Run Code Online (Sandbox Code Playgroud)

c# google-analytics-api google-oauth google-api-dotnet-client service-accounts

24
推荐指数
4
解决办法
2万
查看次数

如何在使用SHA-2而不是SHA-1之间切换?

我的程序使用SHA-1证书进行SSL连接.现在,某些Web服务(Gmail)已广泛使用SHA-2证书.这会导致在电子邮件通知设置期间阻止到SMTP服务器的传入连

要发送电子邮件,我使用像这样的SmtpClient

using (var smtpClient = new SmtpClient(serverSettings.SmtpServerName, (int)serverSettings.SmtpPort))
{
     smtpClient.EnableSsl = serverSettings.SmtpUseSsl;
     smtpClient.UseDefaultCredentials = false; 

     if (!string.IsNullOrEmpty(serverSettings.UserName) || !string.IsNullOrEmpty(serverSettings.EncryptedPassword))
     {
          smtpClient.Credentials = new NetworkCredential(serverSettings.UserName, serverSettings.EncryptedPassword);
     }
                ...
      smtpClient.Send(message);
}
Run Code Online (Sandbox Code Playgroud)

我无法使用此代码发送电子邮件,我不想在我的Gmail帐户中允许"安全性较低的应用程序".

如何实现或切换到电子邮件通知的SHA-2证书?

c# ssl sha

9
推荐指数
1
解决办法
841
查看次数

使用 MailKit 和 Gmail OAuth 发送邮件

我正在尝试创建一个应用程序,该应用程序将在客户进行购买时向他们发送电子邮件。我们有自己的 GMail 帐户,我将用它来发送电子邮件。

我已经在 Google API 控制台中设置了我的应用程序并创建了凭据。我在 MailKit 的 GitHub 上发现了这个问题,它看起来很简单,但似乎对我不起作用。

这是我的代码:

var secrets = new ClientSecrets
{
    ClientId = [CLIENTID]
    ClientSecret = [SECRET]
};

var googleCredentials = await GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, new[] { GmailService.Scope.MailGoogleCom }, email, CancellationToken.None);
await googleCredentials.RefreshTokenAsync(CancellationToken.None);

using (var client = new SmtpClient())
{
    client.Connect("smtp.gmail.com", 587);

    var credentials = new NetworkCredential(googleCredentials.UserId, googleCredentials.Token.AccessToken);
    client.Authenticate(credentials);

    await client.SendAsync(message);
    client.Disconnect(true);
}
Run Code Online (Sandbox Code Playgroud)

调用Authenticate给出了以下错误:

MailKit.Security.AuthenticationException : AuthenticationInvalidCredentials: 5.7.8 用户名和密码不被接受。在 5.7.8 https://support.google.com/mail/?p=BadCredentials m3-v6sm3447324wrs.39 - gsmtp 中了解更多 信息

例外中的 Google 支持页面基本上只是说要么使用两步验证 + 应用密码,要么启用不太安全的应用。这两个我都不想要。为什么在 …

c# email oauth-2.0 google-oauth asp.net-core

8
推荐指数
2
解决办法
7147
查看次数

如何在 ASP.NET 中使用具有 HTML 正文 + 附件的 GMAIL API 发送电子邮件

var msg = new AE.Net.Mail.MailMessage
              {
                  Subject = subject,
                  Body = bodyhtml,
                  From = new System.Net.Mail.MailAddress("myemail")

              };
            foreach (string add in vendorEmailList.Split(','))
            {
                if (string.IsNullOrEmpty(add))
                    continue;

                msg.To.Add(new System.Net.Mail.MailAddress(add));
            }

            msg.ReplyTo.Add(msg.From); // Bounces without this!!
            msg.ContentType = "text/html";

            ////attachment code

            foreach (string path in attachments)
            {
                var bytes = File.ReadAllBytes(path);
                string mimeType = MimeMapping.GetMimeMapping(path);
                AE.Net.Mail.Attachment attachment = new AE.Net.Mail.Attachment(bytes, mimeType, Path.GetFileName(path), true);
                msg.Attachments.Add(attachment);
            }
            ////end attachment code

            var msgStr = new StringWriter();
            msg.Save(msgStr);

            Message message = new Message();
            message.Raw = Base64UrlEncode(msgStr.ToString()); …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net gmail-api

2
推荐指数
1
解决办法
4052
查看次数