The below code iterates through a directory with a bunch of images. These emails are attached to an email and sent to a number of people. However, the code below adds the contentID for the emails correctly, but does not attach the images to the email. When the email is sent, the html is correct, but the attached images are missing.
I am thinking this part of the code is the issue, but I cannot find the exact reason why it is not attaching the series of images to the email:
Code Issue?
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
LinkedResource theEmailImage = null;
DirectoryInfo d = new DirectoryInfo(@"C:\directory\");
FileInfo[] Files = d.GetFiles("*.png"); //Getting image files
StringBuilder sb = new StringBuilder();
string str = "";
foreach (FileInfo file in Files)
{
str = str + ", " + file.Name;
string fileToSend = d.ToString() + file.Name.ToString();
//Add Image
theEmailImage = new LinkedResource(fileToSend.ToString());
theEmailImage.ContentId = file.Name.ToString().Replace(".png", ""); //replace the png for ID
//Add the Image to the Alternate view
}
String strOutput = sb.ToString();
htmlView.LinkedResources.Add(theEmailImage);
Actual Code
private static void Page_Load()
{
DirectoryInfo d = new DirectoryInfo(@"C:\directory\");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.png"); //Getting image files
StringBuilder sb = new StringBuilder();
string str = "";
string Themessage = "";
sb.Append("<html> <body> <table width='100%'> <tr> <td style='font-style:arial; color:maroon; font-weight:bold'> Subject <br>");
foreach (FileInfo file in Files)
{
//str = str + ", " + file.Name;
sb.Append("< img src = cid:" + file.Name.ToString().Replace(".png", "") + " >"); //remove .png for file ID
}
sb.Append("</td></tr></table></body></html>");
String strOutput = sb.ToString();
sendHtmlEmail("fromEmail", "toEmail", strOutput, "output", "Subject", "smtp.mailserver.com", 587);
}
Send Code This is where I see a potential issue
private static void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
{
//create an instance of new mail message
MailMessage mail = new MailMessage();
//set the HTML format to true
mail.IsBodyHtml = true;
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
LinkedResource theEmailImage = null;
DirectoryInfo d = new DirectoryInfo(@"C:\directory\");
FileInfo[] Files = d.GetFiles("*.png"); //Getting image files
StringBuilder sb = new StringBuilder();
string str = "";
foreach (FileInfo file in Files)
{
str = str + ", " + file.Name;
string fileToSend = d.ToString() + file.Name.ToString();
//Add Image
theEmailImage = new LinkedResource(fileToSend.ToString());
theEmailImage.ContentId = file.Name.ToString().Replace(".png", "");
//Add the Image to the Alternate view
}
String strOutput = sb.ToString();
htmlView.LinkedResources.Add(theEmailImage);
//Add view to the Email Message
mail.AlternateViews.Add(htmlView);
//set the "from email" address and specify a friendly 'from' name
mail.From = new MailAddress(from_Email, from_Name);
//set the "to" email address
mail.To.Add(to_Email);
//set the Email subject
mail.Subject = Subject;
//set the SMTP info
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail", "password");
SmtpClient smtp = new SmtpClient("smtp.mailserver.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
//send the email
smtp.Send(mail);
}