C# 如何使用内存流将WinForms应用程序中的MS图表插入Outlook电子邮件?
我正在尝试将MS图表放入outlook电子邮件中。我想避免将它保存到桌面上,所以我决定使用C# 如何使用内存流将WinForms应用程序中的MS图表插入Outlook电子邮件?,c#,.net,winforms,outlook,C#,.net,Winforms,Outlook,我正在尝试将MS图表放入outlook电子邮件中。我想避免将它保存到桌面上,所以我决定使用内存流。这是我到目前为止总结的内容: MemoryStream s = new MemoryStream(); chart1.SaveImage(s, ChartImageFormat.Png); s.Position = 0; OutlookApp outlookApp = new OutlookApp(); MailItem mailItem = outlookApp.CreateItem(OlIte
内存流
。这是我到目前为止总结的内容:
MemoryStream s = new MemoryStream();
chart1.SaveImage(s, ChartImageFormat.Png);
s.Position = 0;
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
chart1.SaveImage(s, ChartImageFormat.Png);
chart1.SaveImage(s, ChartImageFormat.Png);
mailItem.HTMLBody = "<html><body>This is the <strong>funky</strong> message body</body></html>" ;
mailItem.Attachments.Add(s);
//Set a high priority to the message
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.To = "testemail@gmail.com";
mailItem.Display(false);
//mailItem.Send();
MemoryStream s=新的MemoryStream();
chart1.SaveImage(s,ChartImageFormat.Png);
s、 位置=0;
OutlookApp OutlookApp=新建OutlookApp();
MailItem MailItem=outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject=“这是主题”;
chart1.SaveImage(s,ChartImageFormat.Png);
chart1.SaveImage(s,ChartImageFormat.Png);
mailItem.HTMLBody=“这是funky消息正文”;
mailItem.Attachments.Add(s);
//为消息设置高优先级
mailItem.Importance=OlImportance.olImportanceHigh;
mailItem.To=”testemail@gmail.com";
mailItem.Display(false);
//mailItem.Send();
我似乎找不到将
内存流
放入电子邮件的方法。Outlook支持使用SMTP发送电子邮件。因此,您可以使用发送电子邮件。然后,您可以将图像作为附件发送,或将图像放入电子邮件正文:
var body = @"Here is the chart: <br/> <img src=""$CONTENTID1$""/>";
var CONTENTID1 = Guid.NewGuid().ToString().Replace("-", "");
body = body.Replace("$CONTENTID1$", "cid:" + CONTENTID1);
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
var stream = new MemoryStream();
this.chart1.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
LinkedResource imagelink1 = new LinkedResource(stream, "image/png");
imagelink1.ContentId = CONTENTID1;
imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink1);
MailMessage mail = new MailMessage();
mail.From = new MailAddress("someone@outlook.com");
mail.To.Add(new MailAddress("someone@example.com"));
mail.Subject = "Subject";
mail.AlternateViews.Add(htmlView);
SmtpClient client = new SmtpClient();
client.Host = "smtp-mail.outlook.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("someone@outlook.com", "password");
client.Send(mail);
var body=@“这是图表:
”;
var CONTENTID1=Guid.NewGuid().ToString().Replace(“-”,”);
body=body.Replace(“$CONTENTID1$”,“cid:+CONTENTID1”);
var htmlView=AlternateView.CreateAlternateViewFromString(body,null,“text/html”);
var stream=newmemoryStream();
this.chart1.SaveImage(stream、System.Drawing.Imaging.ImageFormat.Png);
流位置=0;
LinkedResource imagelink1=新的LinkedResource(流,“图像/png”);
imagelink1.ContentId=CONTENTID1;
imagelink1.transferncode=System.Net.Mime.transferncode.Base64;
添加(imagelink1);
MailMessage mail=新的MailMessage();
mail.From=新邮件地址(“someone@outlook.com");
mail.To.Add(新邮件地址(“someone@example.com"));
mail.Subject=“Subject”;
mail.AlternateViews.Add(htmlView);
SmtpClient=新的SmtpClient();
client.Host=“smtp mail.outlook.com”;
client.Port=587;
client.enablesl=true;
client.DeliveryMethod=SmtpDeliveryMethod.Network;
client.UseDefaultCredentials=false;
client.Credentials=新的网络凭据(“someone@outlook.com“,”密码“);
客户端。发送(邮件);
这样,图表图像将显示在电子邮件正文中:
不太确定,但Outlook对象模型似乎不支持从内存添加文件附件。看见我建议您看看。您是在尝试创建带有嵌入式OLE对象(图表)的RTF电子邮件,还是创建带有嵌入式图像的HTML电子邮件?我收到此错误,服务器响应为:5.7.3请求的操作中止;未经验证的用户检查上述代码末尾的客户端设置,如主机、端口等。这些设置非常重要,我使用的正是这些设置,它会将一封电子邮件从我的outlook.com帐户发送到另一个帐户。这篇文章似乎回答了你的问题。这也是我还没有看到的答案。