C#将System.Drawing.Image附加到电子邮件 [英] C# Attaching System.Drawing.Image to Email

查看:76
本文介绍了C#将System.Drawing.Image附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将System.Drawing.Image附加到电子邮件中,而无需保存它,然后从保存的路径中获取它。

Is there any way to attach System.Drawing.Image to email with out saving it, then grabbing it from the saved path.

现在我正在创建图片并保存。然后,我发送以下电子邮件:

Right now I'm creating the image and saving it. I then send email with:

MailMessage mail = new MailMessage();
                string _body = "body"

                mail.Body = _body;
                string _attacmentPath;
                if (iP.Contains(":"))
                    _attacmentPath = (@"path1");//, System.Net.Mime.MediaTypeNames.Application.Octet));
                else
                    _attacmentPath = @"path2");
                mail.Attachments.Add(new Attachment(_attacmentPath, System.Net.Mime.MediaTypeNames.Application.Octet));
                mail.To.Add(_imageInfo.VendorEmail);
                mail.Subject = "Rouses' PO # " + _imageInfo.PONumber.Trim();
                mail.From = _imageInfo.VendorNum == 691 ? new MailAddress("email", "") : new MailAddress("email", "");
                SmtpClient server = null;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.Normal; 
                server = new SmtpClient("server");
                try
                {

                    server.Send(mail);
                }
                catch
                {

                }

反正有直接将System.Drawing.Image传递到mail.Attachments.Add()吗?

Is there anyway to directly pass the System.Drawing.Image to mail.Attachments.Add()?

推荐答案

您无法将图片直接传递给附件,但是您可以跳过该文件系统,只需将图片保存到 MemoryStream ,然后将 MemoryStream 提供给附件构造函数:

You can't pass an Image directly to an attachment, but you can skip the file system by just saving the image to a MemoryStream, and then providing that MemoryStream to the attachment constructor:

var stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;

mail.Attachments.Add(new Attachment(stream, "image/jpg"));

这篇关于C#将System.Drawing.Image附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆