不幸的是,由于我们的Exchange服务提供商进行了更新,我们无法再发送带有嵌入式图像的电子邮件。
一旦我们发送电子邮件,我们就会收到此错误:“远程服务器返回错误:(401)未经授权。”如果我们从电子邮件中删除图像,一切正常。有没有解决这个问题的工作?
我们的c#代码:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
LinkedResource imagelink;
MatchCollection matches = Regex.Matches(Body, "<img.*?src=\"cid:(.*?)\"");
foreach (Match match in matches)
{
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(match.Groups[1].Value));
myHttpWebRequest.UseDefaultCredentials = true;
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
using (myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
{
receivedStream = myHttpWebResponse.GetResponseStream();
MemoryStream streamReceivedCopy = new MemoryStream();
ImageUtils.CopyStream(receivedStream,streamReceivedCopy);
var imageType = ImageUtils.GetImageMIMEType(streamReceivedCopy);
streamReceivedCopy.Position = 0;
imagelink = new LinkedResource(streamReceivedCopy, imageType);
imagelink.ContentId = match.Groups[1].Value;
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
}
}
mail.AlternateViews.Add(htmlView);
然后我们使用.net SmtpClient发送电子邮件
我们设法追踪错误:它只是一个部署的旧库导致错误。旧库缺少这些代码行
myHttpWebRequest.UseDefaultCredentials = true;
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
抱歉! :(