ユーザー入力に基づいて電子メールを送信する WCF サービスがあります。最近、特定のユーザーのメールが本文なしで配信されていることに気付きました。.IsBodyHtml が true に設定されている場合、本文テキストは転送されません。ただし、.IsBodyHtml が false に設定されている場合、本文には適切なテキストが含まれます。
ただし、そのユーザーのメールアドレスが「差出人」アドレスとして設定されている場合にのみ発生するようで、一貫性はないようです。
技術の詳細:
MS Exchange メール サーバーがあります。メッセージを送信するために組み込みの SMTP クラスに渡す MailMessage オブジェクトを作成しています。
簡潔/明確にするために、コードは少し単純化されています。それにもかかわらず、元のコードは非常に標準的で単純です。email
LINQ-to-SQL クラス オブジェクトを参照します
MailMessage message = new MailMessage();
message.From = new MailAddress(email.fromAddress);
message.To.Add(email.toRecipient);
message.Subject = email.emailSubject;
//set email body
message.IsBodyHtml = true;
message.Body = email.emailBody;
Attachment attachmentFile = null;
if (email.hasAttachment == true)
{
//retrieve attachments for emailID
var attachments = from attach in db.EmailAttachments
where attach.emailID == emailID
select attach;
foreach (var attachment in attachments)
{//attach each attachment
string filePath = Path.Combine(UPLOAD_DIRECTORY, emailID.ToString(), attachment.fileName);
attachmentFile = new Attachment(filePath);
message.Attachments.Add(attachmentFile); //set attachment from input path
}
}
SmtpClient client = new SmtpClient(SMTP_SERVER, SMTP_PORT); //set SMTP server name/URL and port
client.Send(message); //try to send the SMTP email