MailMessage"失败"发送时的实际消息

时间:2018-03-19 12:56:29

标签: c# asp.net mailmessage

我创建了自定义电子邮件提供商,需要阅读电子邮件以检查特定字符串。

如果它们存在,我会停止发送电子邮件。这项工作在一定程度上。 我的问题是有些电子邮件是空的(没有内容)。

protected override void SendEmailAsyncInternal(string siteName, MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)
{
    string contentAll = "";
    bool stopEmail = false;

    // read email content
    try
    {
        if (message.AlternateViews.Count > 0)
        {
            for (int i = 0; i < message.AlternateViews.Count; i++)
            {
               var stream = message.AlternateViews[i].ContentStream;
               if (stream.CanRead)
                {
                    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, false, 99999, true))
                    {
                        contentAll += reader.ReadToEnd();
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        // log event
    }

    if (stopEmail)
    {
       // mark email as DENIED
    }

    base.SendEmailAsyncInternal(siteName, message, smtpServer, emailToken);
}

我知道这与阅读流有关:

if (stream.CanRead)
{
    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, false, 99999, true))
    {
        contentAll += reader.ReadToEnd();
    }
}

我尝试过复制到临时流但遇到同样的问题:

 var stream = message.AlternateViews[i].ContentStream;
 //////
 //Temporary Stream
 MemoryStream tempStream = new MemoryStream();
 stream.CopyTo(tempStream);

 //
 if (tempStream.CanRead)
 {
     //  tempStream.Seek(0, SeekOrigin.Begin);
     tempStream.Position = 0;
     using (StreamReader reader = new StreamReader(tempStream, System.Text.Encoding.UTF8, false, 99999, true))
     {
         contentAll += reader.ReadToEnd();
     }
 }

为什么这一行代码会剥离发送的电子邮件的内容?

contentAll += reader.ReadToEnd();

0 个答案:

没有答案