Embedding Images in Email with C#

I am a newbie to C# dev and I’m stumped… I can’t figure out why my code won’t send the email. Any ideas? I don’t get any error messages. It just won’t send the email. I used this tut: http://aspalliance.com/1354_Sending_HTML_Mail_with%20_Embedded_Image_in_NET.all#Page8

C#

using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mime;

public partial class SendEmail3 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e){}

      /* Begin Code in Question*/

protected void sendButton_Click(object sender, EventArgs e)

{

    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();
    string FileNameToAttache;
    try
    {
        MailAddress fromAddress = new MailAddress(fromTextBox.Text);
        string[] toEmails = toTextBox.Text.ToString().Split(';');
        foreach (string toEmail in toEmails)
        {
           message.To.Add(toEmail);
        }
        message.From = fromAddress;
        message.Subject = subjectTextBox.Text;
        message.Priority = MailPriority.High;

string plainBody = “greetingTextBox” + “createTextBox” + “closingTextBox”;

AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, null, “text/plain”);

string htmlBody =

            "<html><head><title>" + (subjectTextBox.Text) +
            "</title></head><body><div>" + "<img src=\\"cid:HeaderId\\" />" +
            "<p>" + (greetingTextBox.Text) + "</p><br />" +
            "<p>" + (createTextBox.Text) + "</p>" +
            "<p>" + (closingTextBox.Text) + "</p>" +
            "</div></body>";

message.IsBodyHtml = true;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, “text/html”);

AlternateView imageView = new AlternateView(“http://www.perfect-vision.com/Images/FlashReplacement.jpg”, MediaTypeNames.Image.Jpeg);

      imageView.ContentId = "HeaderId";
      imageView.TransferEncoding = TransferEncoding.Base64;
      // add the views
      message.AlternateViews.Add(plainView);
      message.AlternateViews.Add(htmlView);
      message.AlternateViews.Add(imageView);


      /* End Code in Question*/


      /* Attaching Files Begin */

      if (AttachFile1.PostedFile != null)

      {
          HttpPostedFile attFile = AttachFile1.PostedFile;
          int attachFileLength = attFile.ContentLength;
          if (attachFileLength > 0)

          {

              FileNameToAttache = Path.GetFileName(AttachFile1.PostedFile.FileName);

AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));

          }
      }

      SmtpClient emailWithAttach = new SmtpClient();
      emailWithAttach.Host = "localhost";
      emailWithAttach.DeliveryMethod = SmtpDeliveryMethod.Network;
      emailWithAttach.UseDefaultCredentials = true;
      if (AttachFile2.PostedFile != null)

      {
          HttpPostedFile attFile = AttachFile2.PostedFile;
          int attachFileLength = attFile.ContentLength;
          if (attachFileLength > 0)
          {

FileNameToAttache = Path.GetFileName(AttachFile2.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));

          }
      }

      if (AttachFile3.PostedFile != null)

      {
         HttpPostedFile attFile = AttachFile3.PostedFile;
          int attachFileLength = attFile.ContentLength;
          if (attachFileLength > 0)

          {

FileNameToAttache = Path.GetFileName(AttachFile3.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));

          }
      }

      if (AttachFile4.PostedFile != null)

      {
          HttpPostedFile attFile = AttachFile4.PostedFile;
          int attachFileLength = attFile.ContentLength;
          if (attachFileLength > 0)

          {

FileNameToAttache = Path.GetFileName(AttachFile4.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));

          }
      }
      /* Attaching Files END */

        smtpClient.Host = "localhost";
        smtpClient.Send(message);
        resultLabel.Text = "Email sent! <br />";
    }
    catch (Exception ex)

    {
        resultLabel.Text = "Couldn't Send the Message!";
    }
}

protected void clearButton_Click(object sender, EventArgs e)

{

}

}

You’re swallowing the error, replace:

resultLabel.Text = "Couldn't Send the Message!";

with

resultLabel.Text = ex.Message;

Or better yet, remove the Try - Catch block completely.

Thanks!