如何做电子邮件html文档,使用c#嵌入图像? [英] How to do email html document, embedding images using c#?

查看:89
本文介绍了如何做电子邮件html文档,使用c#嵌入图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我尝试了这个概念,文字你好世界即将到来,但我的形象不会出现在代码中设置的只有小型影像箱来......这是什么原因?任何人都可以帮忙解决这个问题...?



我尝试过:



为此我使用的是这样的HTML

Hey i tried with this concept, text "hello world" coming but my image not coming whtever am setting in code only small imagebox coming..what is the reason?? Can anyone help to resolve this issue...?

What I have tried:

for this i am using html like this

<html>
<body>
    <table border="0" cellpadding="0" cellspacing="0" width="100%" height="50%><br" mode="hold" />
	<tr>
            <td width="230" valign="top">
                <table border="0" width="50%">
                    <tr>
                        <td>
                            Dear All,
                            
                             a very Happy Birthday!!
                        </td>
                    </tr>
                    <tr style="...">
                        <td style="font-weight:bold;">
                            Hello World!<br>
                            <div style="...">
                                <img src=cid:myImageID style="position: relative; top: 0; left: 0;" />
                            </div>
                        </br></td>
                    </tr>
                </table>
            </td>
        </tr>
</body>
</html>




//create an instance of new mail message
MailMessage mail = new MailMessage();

//set the HTML format to true
mail.IsBodyHtml = true;

//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

//Add Image
LinkedResource theEmailImage = new LinkedResource(imagePath);
theEmailImage.ContentId = "myImageID";

//Add the Image to the Alternate view
htmlView.LinkedResources.Add(theEmailImage);

//Add view to the Email Message
mail.AlternateViews.Add(htmlView);

//set the "from email" address and specify a friendly 'from' name
mail.From = new MailAddress(from_Email, from_Name);

//set the "to" email address
mail.To.Add(to_Email);

//set the Email subject
mail.Subject = Subject;

//set the SMTP info
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("username", "pwd");
SmtpClient smtp = new SmtpClient("server", port);
//smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
//send the email
smtp.Send(mail);



请告诉我我在哪里做错了?


Please tell me where i am doing wrong??

推荐答案

嵌入式图片是部分具有单独内容类型和处置多部分电子邮件。从技术上讲,这样的部分是使用类 System.Net.Mail.Attachment 创建的:

附件类(System.Net.Mail) [ ^ ]。



内容类型 AttachmentBase.ContentType属性(System.Net.Mail) [ ^ ]),另见我过去的答案: 如何在smtp邮件实施中附加文件 [ ^ ]。



通常,您将图像部分放置在base64编码的字符串。请参阅:



如果您正确选择了内容处理,附件将显示在邮件中,并嵌入邮件中。我在过去的答案中解释了这一点:动态生成pdf文件和发送电子邮件作为附件。 [ ^ ]。



在上面提到的答案中,你会找到所有的链接你需要嵌入你的图像。



如果你还需要在邮件消息中创建一个指向图像的超链接,你需要使用特殊的 URI scheme cid(Content-id)。请参阅我过去的回答:发送带图片的HTML附件 [ ^ ]。br />


-SA
"Embedded image" is a part of a multipart e-mail with separate content-type and disposition. Technically, such part is created using the class System.Net.Mail.Attachment:
Attachment Class (System.Net.Mail)[^].

On content types (AttachmentBase.ContentType Property (System.Net.Mail)[^]), see also my past answer: How to attach a file in smtp mail implementation[^].

Normally, you place your image part in the form of base64-encoded string. Please see:

The attachment will be presented in a mail viewer as embedded in your message, if you chose content disposition properly. I explain it in my past answer: Dynamically generate pdf file and Send an Email as a attachment.[^].

In the answer referenced above, you will find all links you need to embed your images.

If you also need to create a hyperlink to the image inside a mail message, you would need to use the special URI scheme "cid" (Content-id). Please see my past answer: Sending HTML attachment with images[^].

—SA


这篇关于如何做电子邮件html文档,使用c#嵌入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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