1

我正在 MVC 4.0 dot net 中开发一个应用程序。当用户第一次访问该应用程序时,会向用户发送一封邮件。为了发送邮件,我正在使用我们公司实现的 WCF 邮件服务。

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

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

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 位于 Content 文件夹中。

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

4

2 回答 2

3

您希望~/Content/logo.png指向收件人机器上的哪个位置?

99% 的时间它不会指向任何地方,另外 1% 无论如何都不是你想要的文件。


三种解决方案:

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

    <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来创建一个这样的,或者按照规范只使用 base64 。

于 2013-03-18T13:08:41.990 回答
1

提供logo图片的绝对路径,如

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

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

于 2013-03-18T13:09:18.750 回答