【问题标题】:.IsBodyHtml=true causes an empty email body to be sent, but .IsBodyHtml=false sends the body content.IsBodyHtml=true 导致发送一个空的电子邮件正文,但 .IsBodyHtml=false 发送正文内容
【发布时间】:2012-04-14 14:16:13
【问题描述】:

我有一个基于用户输入发送电子邮件的 WCF 服务。引起我注意的是,最近,某个特定用户的电子邮件在发送时没有任何正文。如果 .IsBodyHtml 设置为 true,则不传输正文;但是,如果 .IsBodyHtml 设置为 false,则正文具有相应的文本。
但是,它似乎并不一致,因为它似乎仅在所述用户的电子邮件地址设置为“发件人”地址时才会发生。

技术细节:
我们有一个 MS交换邮件服务器。我正在编写一个 MailMessage 对象,将其传递给内置 SMTP 类以发送消息。

为了简洁/清晰起见,代码已经稍微简化了。尽管如此,原始代码还是非常标准/直接的。 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

【问题讨论】:

  • 你能发布一些示例代码吗?

标签: c# .net wcf exchange-server mailmessage


【解决方案1】:

由于问题与用户有关,因此可能与该用户的设置有关。

以该用户身份登录并打开 Outlook

选择:文件 -> 选项 -> 邮件

向下滚动到“消息格式”部分

可能“选择了转换为纯文本格式”将其更改为“转换为 HTML 格式”

【讨论】:

  • 感谢您的回复/提示,但是,该设置应该只影响用户在 Outlook 中发送的电子邮件,而不影响我的服务代码(使用我们的 SMTP 服务器)发送的电子邮件。如果我不清楚,请道歉。
  • 可能是该设置保存在服务器上的用户配置文件中,因此会影响该用户帐户发送的邮件。检查该用户的设置并与它的工作用户进行比较。
猜你喜欢
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
相关资源
最近更新 更多