【问题标题】:Why is my image not showing in the email?为什么我的图片没有显示在电子邮件中?
【发布时间】:2015-11-13 07:56:57
【问题描述】:

我有一个电子贺卡应用程序,允许用户选择图像并发送电子邮件。但我无法弄清楚如何发送电子邮件,这是我的代码。

protected void SendEmail()
{
    StringBuilder message;
    Attachment attachFile;
    string sImage = System.Web.HttpContext.Current.Server.MapPath("~/DesktopModules/KDMC_EcardList/images/" + txtCapture.Text);
    LinkedResource yourPictureRes = new LinkedResource(txtCapture.Text, MediaTypeNames.Image.Jpeg);
    yourPictureRes.ContentId = "YourPictureId";
    altView.LinkedResources.Add(yourPictureRes);
    //Internal Message
    message = new StringBuilder();
    message.AppendLine(@"<p><img src=cid: sImage /></p>");
    message.AppendLine(@"<pre><table style='width:650px; word-break:break-word; height:auto;'>
                             </td><tr><td style='width:150px;'>From: </td><td style='width:500px;'>" + txtFName.Text + " " + txtLName.Text + @"
                            </td><tr><td>To: </td><td>" + txtFriendFName.Text + " " + txtFriendLName.Text + @"
                            </td><tr><td>Room Number: </td><td>" + txtRoom.Text + @"
                           </td><tr><td>Message: </td><td>" + txtMessage.Text + @"                                 
                           </td><tr><td>Date Sent: </td><td>" + DateTime.Now.ToString() + "</td></tr></table></pre>");

    //attachFile = new Attachment(FileUploadVideo.PostedFile.InputStream, strFileName);
    System.Threading.Thread.Sleep(1000);
    // need to create new reference to attachment, cannot use same reference to send email twice
    //attachment = new Attachment(theMemoryStream, strSubject);
    Mail.SendMail("test@test.net", "test@test.net", "", "test@test.net",
            "test@test.net", MailPriority.Normal, "A new Ecard has been submitted", MailFormat.Html, Encoding.UTF8, message.ToString(),
            new List<Attachment>() { }, null, null, null, null, Host.EnableSMTPSSL);  
}

我尝试按照C# add image to email body 此处的示例以及社区中的其他解决方案进行操作,但我仍然无法找到解决方案。

【问题讨论】:

标签: c# asp.net email


【解决方案1】:

您需要添加embedded image to the email,然后您可以在模板中使用cid id:

LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPictureRes);

【讨论】:

  • 我知道添加图片名称的部分,但YourPictureId 是什么。这是否也意味着我不需要使用&lt;img src=cid: sImage /&gt;&lt;/p&gt;
  • ID 可以是任何文本 - 例如尝试img1,或链接示例中的YourPictureId
  • 我已经编辑了上面的代码,以反映您要求我尝试的内容。它在altview 行出现错误。
【解决方案2】:

您将 IMG 标记的 src 的 CID 设置为 Server.MapPath(...) 的结果。这将是您机器上的完整路径。如果您指的是附件,我认为需要输入的是文件名 (Path.GetFileName(Server.MapPath(...)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2021-09-13
    • 2020-10-19
    • 2021-10-10
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多