使用wcf服务发送带有html body的电子邮件真实的图像

时间:2013-03-18 13:06:00

标签: c# wcf

我正在使用MVC 4.0 dot net中的应用程序。当用户第一次访问应用程序时,会向用户发送邮件。对于发送邮件,我正在使用我们公司实施的WCF邮件服务。

我创建了一个包含邮件格式HTML的.txt文件

这是我用来发送邮件的代码

public void SendWelcomeMail(string name, string email, string filePath)
        {
            try
            {
                string subject = ConfigurationManager.AppSettings["WelcomeMailSub"];
                string supportMail = ConfigurationManager.AppSettings["supportMail"];

                using (StreamReader reader = File.OpenText(filePath))
                {
                    string text = reader.ReadToEnd();
                    text = string.Format(text, name);

                    Mail mails = new Mail { MailTo = "suresh.negi89@gmail.com", Msg = text, Subject = subject, IsBodyHtml = true };

                    MailSenderServiceClient oClient = new MailSenderServiceClient();



                    oClient.SendMail(mails);
                }


            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

这是具有HTML格式的文件:

 <html> 
<body><
<div style="height:40px;width:675px;background:#000; text-align:center;color:red;">
<img src="~/Content/logo.png" alt="DTD" style="float:left">
<h1> {0} Congrates you  are registered as a prime user!! </h1>
</div>
<p style="font-family: arial,sans-serif;">
Hi, Welcome to you
</p>
</body>
</html>

图片文件logo.png位于内容文件夹中。

发送邮件时,不会显示图像。我想知道我在哪里做错了。

2 个答案:

答案 0 :(得分:3)

您希望~/Content/logo.png指向收件人的机器?

  

99%的时间它无处可指,而另外1%不会成为你想要的文件。


三种解决方案:

  1. 在公共服务器上托管您的图片,并使用完整的绝对网址引用它(它可能已存在?)。这基本上是事实上的标准。

    <img src="http://www.example.com/logo.png" />
    

    如果此位置发生变化,您当然可以在发送电子邮件之前使用占位符并在运行时解析URL。

  2. 将图像附加到电子邮件中。然后,您可以使用cid:内联引用它。

    <img src="cid:logo.png" />
    
  3. 使用数据URI并内联数据:

    <img src="data:image/x-icon,%00%00%01%00%01%00%10%10%00%00%01%00%20%00h%04%00%00%16%00%00%00(%00%00%00%10%00%00%00%20%00%00%00%01%00%20%00%00%00%00%00%00%00%00%00%13%0B%00%00%13%0B%00%00%00%00%00%00%00%00%00%00%00%00%00%00llm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FF%00%00%00%00%00%00%00%00llm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FF%00%00%00%00%00%00%00%00llm%FFllm%FF%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00llm%FFllm%FF%00%00%00%00%00%00%00%00llm%FFllm%FF%00%00%00%00llm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FF%00%00%00%00llm%FFllm%FF%00%00%00%00%00%00%00%00llm%FFllm%FF%00%00%00%00llm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FFllm%FF%00%00%00%00llm%FFllm%FF%00%00%00%00%00%00%00%00llm%FFllm%FF%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00Tx%9B%14Ox%A0%3CJx%A4dDx%AA%91Lx%A2%19llm%FFllm%FF%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00Kx%A3%0F%3Cx%B1R%3Ex%B0%84%3Ex%B0%B9%3Ex%B0%DE%3Ex%B0%FF%3Fx%AF%FFAw%AD%FFBv%AB%FFDw%A9%3E%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%3Dx%B0*Ax%AD%FFAx%AD%FFAx%AD%FFBw%AB%ECEv%A9%C0Cw%AB%85%3B%7B%B3T*%83%C5R%15%8D%DCY%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%3Fx%AF%09Bx%ACsDv%AAQDv%AA%2B%3B%7B%B3%08%00%00%00%00%18%8C%D9%0C%09%93%E8r%03%97%EF%E2%02%97%EE%FF%00%99%F2%1B%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%09%93%E8%19%03%97%EE%82%03%96%EE%ED%05%95%EC%FF%04%97%EB%EC%09%91%EA%82%1Dx%E6N%20r%E9%02%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0F%91%E1%02%02%97%EE%24%03%97%EE%86%05%96%EC%F3%06%95%EB%FF%06%95%EB%EB%04%98%EB%83%0D%8C%E9%10'm%E5L%2Fc%E4%FC.d%E4%81%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%04%96%ED%09%06%95%EB%D5%06%95%EB%FF%06%95%EB%E9%06%96%EB%7C%04%98%EB%17%00%00%00%00%2Bg%E46.d%E4%F8%2Cf%E4%F8%2Cf%E4D%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%06%95%EBl%06%95%EB~%05%96%EB%15%00%00%00%00%00%00%00%00-d%E4%22-e%E4%E5%2Cf%E4%FF%2Cf%E4X%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00.c%E4%0F%2Ce%E4%CB%2Cf%E4%FF%2Cf%E4%7B%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00.c%E4%02%2Cf%E4%AD%2Cf%E4%FF%2Cf%E4%A1%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00.d%E4%02%2Cf%E4%93%2Cf%E4%FF%2Cf%E4%C0%2Cf%E4%07%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%80%01%00%00%80%01%F0%BF%9F%F9%00%00%90%09%00%00%90%09%00%00%9F%01%00%00%E0%07%00%00%E0%07%00%00%E0%83%00%00%FE%01%00%00%F0%01%00%00%F0!%00%00%F8%C3%FF%FF%FF%87%FF%FF%FF%0F%00%00%FE%0F%00%00" />
    

    您可以使用data: URI Kitchen创建一个这样的内容,或者根据the spec使用base64。

答案 1 :(得分:1)

提供徽标图像的绝对路径,例如

 <img src="http://www.foobar.com/Content/logo.png" alt="DTD" style="float:left">

或将图像附加到电子邮件中。