私はASP.NET Coreで作業しており、メール機能としてMailKitを使用しています。これまで使用してきた方法はもはや機能しません。関数呼び出しで使用されているGmailがMailKitで応答しない
public class EmailSender
{
public EmailSender()
{
}
public async Task SendEmailAsync(
SmtpOptions smtpOptions,
string to,
string from,
string subject,
string plainTextMessage,
string htmlMessage,
string replyTo = null)
{
if (string.IsNullOrWhiteSpace(to))
{
throw new ArgumentException("no to address provided");
}
if (string.IsNullOrWhiteSpace(from))
{
throw new ArgumentException("no from address provided");
}
if (string.IsNullOrWhiteSpace(subject))
{
throw new ArgumentException("no subject provided");
}
var hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage);
var hasHtml = !string.IsNullOrWhiteSpace(htmlMessage);
if (!hasPlainText && !hasHtml)
{
throw new ArgumentException("no message provided");
}
var m = new MimeMessage();
m.From.Add(new MailboxAddress("", from));
if (!string.IsNullOrWhiteSpace(replyTo))
{
m.ReplyTo.Add(new MailboxAddress("", replyTo));
}
m.To.Add(new MailboxAddress("", to));
m.Subject = subject;
//m.Importance = MessageImportance.Normal;
//Header h = new Header(HeaderId.Precedence, "Bulk");
//m.Headers.Add()
BodyBuilder bodyBuilder = new BodyBuilder();
if (hasPlainText)
{
bodyBuilder.TextBody = plainTextMessage;
}
if (hasHtml)
{
bodyBuilder.HtmlBody = htmlMessage;
}
m.Body = bodyBuilder.ToMessageBody();
using (var client = new SmtpClient())
{
await client.ConnectAsync(
smtpOptions.Server,
smtpOptions.Port,
smtpOptions.UseSsl)
.ConfigureAwait(false);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
if (smtpOptions.RequiresAuthentication)
{
await client.AuthenticateAsync(smtpOptions.User, smtpOptions.Password)
.ConfigureAwait(false);
}
await client.SendAsync(m).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
public async Task SendMultipleEmailAsync(
SmtpOptions smtpOptions,
string toCsv,
string from,
string subject,
string plainTextMessage,
string htmlMessage)
{
if (string.IsNullOrWhiteSpace(toCsv))
{
throw new ArgumentException("no to addresses provided");
}
if (string.IsNullOrWhiteSpace(from))
{
throw new ArgumentException("no from address provided");
}
if (string.IsNullOrWhiteSpace(subject))
{
throw new ArgumentException("no subject provided");
}
var hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage);
var hasHtml = !string.IsNullOrWhiteSpace(htmlMessage);
if (!hasPlainText && !hasHtml)
{
throw new ArgumentException("no message provided");
}
var m = new MimeMessage();
m.From.Add(new MailboxAddress("", from));
string[] adrs = toCsv.Split(',');
foreach (string item in adrs)
{
if (!string.IsNullOrEmpty(item)) { m.To.Add(new MailboxAddress("", item)); ; }
}
m.Subject = subject;
m.Importance = MessageImportance.High;
BodyBuilder bodyBuilder = new BodyBuilder();
if (hasPlainText)
{
bodyBuilder.TextBody = plainTextMessage;
}
if (hasHtml)
{
bodyBuilder.HtmlBody = htmlMessage;
}
m.Body = bodyBuilder.ToMessageBody();
using (var client = new SmtpClient())
{
await client.ConnectAsync(
smtpOptions.Server,
smtpOptions.Port,
smtpOptions.UseSsl).ConfigureAwait(false);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
if (smtpOptions.RequiresAuthentication)
{
await client.AuthenticateAsync(
smtpOptions.User,
smtpOptions.Password).ConfigureAwait(false);
}
await client.SendAsync(m).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
}
:
私はSmtpOptions.csクラスを持っている:
public class SmtpOptions
{
public string Server { get; set; } = "smtp.gmail.com"; //Gmail limited to 2000 emails per day
public int Port { get; set; } = 465; //default for SSL using GMail
public string User { get; set; } = "[email protected]"; //must match server domain
public string Password { get; set; } = "myPwd";
public bool UseSsl { get; set; } = true; //gmail requires SSL
public bool RequiresAuthentication { get; set; } = true; //gmail requires authentication
public string PreferredEncoding { get; set; } = string.Empty;
}
とEmailSender.csクラスに述べたように
public async void ContactMessage(string title, string message, string toEmail)
{
string thisMessage = "No Message Provided";
if (!String.IsNullOrEmpty(message)) { thisMessage = message; } //in case empty form
string thisTitle = title;
//create email objects
EmailSender emailSender = new EmailSender();
SmtpOptions smtpOptions = new SmtpOptions(); //default settings ok
string fromEmail = smtpOptions.User;
string subjectLine = "Message Title";
await emailSender.SendEmailAsync(smtpOptions, toEmail, fromEmail, subjectLine, thisMessage, "");
}
、この方法はこれまでうまくいきましたが、現在はGmailの対応に失敗しています。 IMAPとPOPをチェックして有効にしました。
特定の例外は、読み取ります 「System.Net.Internals.SocketExceptionFactory.ExtendedSocketE xception:接続されたホストが持っているので、「接続先が適切に一定期間後に応答しなかったため、接続の試みが失敗した、または確立された接続に失敗しました応答しませんでした」「
このコードはデプロイされていませんが、このエラーはテスト環境で発生します。別のファイアウォールを持つシステムに移動すると、「Mailkit.Security.Authentication; MailKit認証が弱すぎます」というエラーが発生します。
Yahoo!でのテストsmtpは上記のコードを動作させるので、MailkitとGmailの互換性が変わったようです。 Gmailを使用するための適切なMailkit設定は何ですか?
これまでに動作していたものの、コードやGmailの設定で何も変更していない場合は、Googleのサポートチームに連絡する必要はありませんか? StackOverflowはプログラミングに関連する質問のためのもので、正確なエラーを提供していません – Tseng
私は十分に明確でない場合は申し訳ありません。これはコードの再利用の例です。以前はWebFormsアプリケーションで動作しましたが、現在はasp.netコアMVCを試しています。私はここでASP.NET Coreを初めて勉強しているので、フレームワークの違いに関係していると思っています。正確な例外は次のとおりです: "System.Net.Internals.SocketExceptionFactory.ExtendedSocketException:接続されたパーティーが一定期間後に正しく応答しなかったか、接続されたホストが応答しなかったために確立された接続が失敗したために接続に失敗しました" " – coolhand
Better答えの中の例外を更新してください。誰もあなたの質問を読むときにすべてのコメントを読んでいるわけではありません。あまりにも多くの情報が欠けている、それはあなたがローカルでテストするとき、または配備後に起こるか? Webformアプリケーションはまったく同じコードで実行されますか? WinFormsは.NET Framework上で動作しますが、ASP.NET Coreはデフォルトで.NET Core上で動作します。.NET Frameworkを対象とするASP.NET Core 2.0を実行すると、同じエラーが発生しますか? – Tseng