public static void Send(string sendto, string sendfrom, string subject, string body,
string[] attachFileNames,
string smtpServerHostName, int smtpPort,
string smtpUserId, string smtpUserPassword,
string popServerHostName, int popPort,
string popUserId, string popUserPassword)
{
if (isDisable(sendto)) return;
if (isDisable(sendfrom)) return;
if (isDisable(smtpServerHostName)) return;
if (smtpPort == 0) return;
bool useSmtpAuth = !isDisable(smtpUserId) && !isDisable(smtpUserPassword);
bool usePopBeforeSmpt = !isDisable(popServerHostName) && popPort != 0
&& !isDisable(popUserId) && !isDisable(popUserPassword);
var message = new MimeMessage();
message.From.Add(new MailboxAddress(sendfrom));
message.To.Add(new MailboxAddress(sendto));
message.Subject = subject;
if (attachFileNames.Length == 0)
{
message.Body = new TextPart("plain") { Text = body };
}
else
{
var multi = new Multipart();
multi.Add(new TextPart("plain") { Text = body });
foreach (var filename in attachFileNames)
{
var attachment = new MimePart()
{
ContentObject = new ContentObject(File.OpenRead(filename), ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName(filename)
};
multi.Add(attachment);
}
message.Body = multi;
}
if (usePopBeforeSmpt)
{
using (var client = new Pop3Client())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(popServerHostName, popPort, true);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(popUserId, popUserPassword);
client.Disconnect(true);
}
}
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(smtpServerHostName, smtpPort, true);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(smtpUserId, smtpUserPassword);
client.Send(message);
client.Disconnect(true);
}
}
「実は.NET Frameworkの電子メール送信関係のクラスを見ている時、特に問題は無かったのだがMailkitを使ってね(英文の大意)という記述を発見してね。でも、問題は無いから無視しようと思った。ところが、問題ないというのは錯覚で実際は問題があった」