不幸的是,由于我们的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发送电子邮件
答案 0 :(得分:0)
我们设法追踪错误:它只是一个部署的旧库导致错误。 旧图书馆缺少这些代码行
myHttpWebRequest.UseDefaultCredentials = true;
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
对不起! :(