Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像嵌入System.Web.Mail.MailMessage中的电子邮件_C#_Email - Fatal编程技术网

C# 将图像嵌入System.Web.Mail.MailMessage中的电子邮件

C# 将图像嵌入System.Web.Mail.MailMessage中的电子邮件,c#,email,C#,Email,我想使用System.Web.Mail.MailMessage和System.Web.Mail.SmtpMail类发送电子邮件。 我知道这是一个不推荐使用的类,但使用System.Net.Mail不是我的选择 到目前为止,我能够发送html格式的邮件,但在发送时无法将图像嵌入到电子邮件中 这就是我迄今为止所尝试的 private static void SendMailMethod(string mailServer, int mailServerPort, string userName, s

我想使用System.Web.Mail.MailMessage和System.Web.Mail.SmtpMail类发送电子邮件。 我知道这是一个不推荐使用的类,但使用System.Net.Mail不是我的选择

到目前为止,我能够发送html格式的邮件,但在发送时无法将图像嵌入到电子邮件中

这就是我迄今为止所尝试的

private static void SendMailMethod(string mailServer, int mailServerPort, string userName, string password)
    {
        const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver";
        const string SMTP_SERVER_PORT =
            "http://schemas.microsoft.com/cdo/configuration/smtpserverport";

        const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing";

        const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl";


        const string SMTP_AUTHENTICATE =
           "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";


        const string SEND_USERNAME =
            "http://schemas.microsoft.com/cdo/configuration/sendusername";
        const string SEND_PASSWORD =
            "http://schemas.microsoft.com/cdo/configuration/sendpassword";


        var mailMessage = new System.Web.Mail.MailMessage();

        mailMessage.Fields[SMTP_SERVER] = mailServer;
        mailMessage.Fields[SMTP_SERVER_PORT] = mailServerPort;
        mailMessage.Fields[SEND_USING] = 2;
        mailMessage.Fields[SMTP_USE_SSL] = false;
        mailMessage.Fields[SMTP_AUTHENTICATE] = 0;
        mailMessage.Fields[SEND_USERNAME] = userName;
        mailMessage.Fields[SEND_PASSWORD] = password;

        mailMessage.From = "abc@xyz.com";
        mailMessage.To = "abc@xyz.com";
        mailMessage.Subject = "Test mail:";
        mailMessage.BodyFormat = MailFormat.Html;

        var mailAttachment = new MailAttachment("E:\\imageToEmbed.jpg");
        mailMessage.Attachments.Add(mailAttachment);
        mailMessage.Attachments.Add(new MailAttachment("E:\\TestAttachmentFile.txt"));

        var htmlBody =
            "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
            "<html xmlns = \"http://www.w3.org/1999/xhtml\" >" +
            " <head >" +
            "<meta http - equiv = \"content-type\" content = \"text/html; charset=UTF-8\" />" +
            "</head >" +
            "<body style = \"font-family: Segoe UI; text-align:left;\" >" +
            "Following is an embedded image:" +
            "<br />" +
            "<img alt = \"\" src = \"imageToEmbed.jpg\" />" +
            "</body >" +
            "</html >";

        mailMessage.Body = htmlBody;

        try
        {
            SmtpMail.Send(mailMessage);

            Console.WriteLine("Mail sent");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:" + ex.ToString());
            throw ex;
        }
    }
私有静态void SendMailMethod(字符串mailServer、int mailServerPort、字符串用户名、字符串密码)
{
常量字符串SMTP_服务器=”http://schemas.microsoft.com/cdo/configuration/smtpserver";
常量字符串SMTP\u服务器\u端口=
"http://schemas.microsoft.com/cdo/configuration/smtpserverport";
const string SEND_USING=”http://schemas.microsoft.com/cdo/configuration/sendusing";
常量字符串SMTP\U USE\U SSL=”http://schemas.microsoft.com/cdo/configuration/smtpusessl";
常量字符串SMTP\U身份验证=
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";
常量字符串发送\u用户名=
"http://schemas.microsoft.com/cdo/configuration/sendusername";
常量字符串发送密码=
"http://schemas.microsoft.com/cdo/configuration/sendpassword";
var mailMessage=new System.Web.Mail.mailMessage();
mailMessage.Fields[SMTP_SERVER]=mailServer;
mailMessage.Fields[SMTP_服务器_端口]=mailServerPort;
mailMessage.Fields[SEND_USING]=2;
mailMessage.Fields[SMTP\u USE\u SSL]=false;
mailMessage.Fields[SMTP_AUTHENTICATE]=0;
mailMessage.Fields[SEND_USERNAME]=用户名;
mailMessage.Fields[发送密码]=密码;
mailMessage.From=”abc@xyz.com";
mailMessage.To=”abc@xyz.com";
mailMessage.Subject=“测试邮件:”;
mailMessage.BodyFormat=MailFormat.Html;
var mailAttachment=新邮件附件(“E:\\imageToEmbed.jpg”);
mailMessage.Attachments.Add(mailAttachment);
mailMessage.Attachments.Add(新邮件附件(“E:\\TestAttachmentFile.txt”);
变量htmlBody=
"" +
"" +
" " +
"" +
"" +
"" +
“以下是嵌入的图像:”+
“
”+ "" + "" + ""; mailMessage.Body=htmlBody; 尝试 { SmtpMail.Send(mailMessage); 控制台。WriteLine(“已发送邮件”); } 捕获(例外情况除外) { Console.WriteLine(“错误:+ex.ToString()); 掷骰子; } }
此代码发送电子邮件,但邮件中显示的不是图像,而是带有十字架的小正方形。
如何将这个“imageToEmbed.jpg”嵌入到我的电子邮件中

它可能在指定的路径中找不到图像,或者它可能是一个权限问题,因此出现了损坏的图像。您需要首先检查,但是如果您要使用base64,您将拥有一个字符串中的图像

尝试将映像更改为base64


此链接可能也会有所帮助

使用类
System.Web.Mail
无法实现这一点,因为该实现抽象了对底层系统许多功能的访问

与其使用
System.Web.Mail.Message提供的包装器,不如直接切换到使用CDO COM对象。从“引用”对话框的“COM”选项卡添加对Microsoft CDO for Windows 2000库的引用

之后,您可以使用以下代码创建并发送带有嵌入图像的html电子邮件:

var cdo = new CDO.Message();
// configuration
var cfg = cdo.Configuration;
cfg.Fields[SMTP_SERVER].Value = "smtp.server.com";
cfg.Fields[SMTP_SERVER_PORT].Value = 22;
cfg.Fields[SEND_USING].Value = 2;
cfg.Fields[SMTP_USE_SSL].Value = true;
cfg.Fields[SMTP_AUTHENTICATE].Value = 1;
cfg.Fields[SEND_USERNAME].Value = "user@example.com";
cfg.Fields[SEND_PASSWORD].Value = "password";
cfg.Fields.Update();

cdo.To = "awesome@example.com";
cdo.Sender = "me@example.com";

// attachment
var cdoatt = cdo.AddAttachment("file:///E:/imageToEmbed.jpg");
//this is why System.Web.Mail can't embed images
cdoatt.Fields["urn:schemas:mailheader:content-id"].Value = Guid.NewGuid().ToString("N");
cdoatt.Fields.Update();

// get a reference to out content-id field
var cid = cdoatt.Fields["urn:schemas:mailheader:content-id"];
// notice the special layout of SRC on the image tag, 
//it will be somethong like CID:123456789abcdef
cdo.HTMLBody = @"<HTML><BODY><B>CDO</B><BR /> <IMG SRC=""cid:" + cid.Value + @"""/></BODY></HTML>";

cdo.Send();

如您所见,没有添加内容ID头,也没有从.Net实现添加该头的代码路径

您的问题在这里得到了回答@photowalker,您共享的链接中的答案对我不起作用。因为我没有使用System.Net.Mail空间。我正在使用System.Web.Mail.MailMessage类发送电子邮件为什么您只限于那个过时的类?您在.Net 1.1上吗?我被限制使用这个过时的类,因为System.Net.Mail中的新类不允许我使用SSL安全性发送邮件。我已经问过了,谢谢你这么详细的回答。我已尝试使用您的解决方案,但电子邮件仍然没有显示图像。这就是我的原始电子邮件现在的样子。@krw12572嗯,奇怪,你的内容看起来还不错,即使你加了分数,我也会得到一个嵌入的图像。顺便说一句,我正在用gmail测试它。谢谢!代码正在运行。可能是以前邮件服务器的问题。它现在正在与gmail合作。
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="----=_NextPart_000_0001_01D1AF72.C8AE8C70"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384

This is a multi-part message in MIME format.

------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_0002_01D1AF72.C8AE8C70"


------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

plain cdo 

------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/html;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<HTML><BODY><B>plain cdo</B> <IMG SRC="cid:42"/></BODY></HTML>
------=_NextPart_001_0002_01D1AF72.C8AE8C70--

------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: image/png;
    name="file.png"
Content-Transfer-Encoding: base64
Content-ID: <42>
Content-Disposition: attachment;
    filename="file.png"

iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMA
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="----=_NextPart_000_0000_01D1AF70.59FC25F0"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384

This is a multi-part message in MIME format.

------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_0001_01D1AF70.59FC25F0"


------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Following is an embedded image:

  _____  

test afterit

------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/html;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml" > <head ><meta http - equiv = "content-type" content = "text/html; charset=UTF-8" /></head ><body style = "font-family: Segoe UI; text-align:left;" ><i>Following is an embedded image:</i><br /><img alt = "" src ="<file:///file.png>"/><hr><b>test afterit</b></body ></html >
------=_NextPart_001_0001_01D1AF70.59FC25F0--

------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: image/png;
    name="file.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="file.png"

iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMAAAAI0l