0

I have an ecard application that allows a user to pick an image and send an email. But I am not able to figure out how to send the email here is my code.

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);  
}

I tried to follow the example here C# add image to email body as well as other solutions in the community but I am still unable to find a solution.

Community
  • 1
  • 1
Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • The post that post links to seems to be more what you're after http://stackoverflow.com/questions/1113345/sending-mail-along-with-embedded-image-using-asp-net – tbddeveloper Aug 19 '15 at 20:49

2 Answers2

1

You need to add the embedded image to the email , then you can use the cid id in the template:

LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPictureRes);
Community
  • 1
  • 1
Dmitry Sadakov
  • 2,128
  • 3
  • 19
  • 34
0

You're setting the CID of the IMG tag's src to the result of Server.MapPath(...). This will be a full path on your machine. I think what needs to go in there if you're referring to the attachment is the filename (Path.GetFileName(Server.MapPath(...)))

Chris Disley
  • 1,286
  • 17
  • 30