In my project, I have one email html template. I need to send that email to some user. Before sending the email, I need to embed one background image. I have that image stored in my DB as base64String. I used below code and It is working fine, when a user opens email using the web client, but when the user opens the mail using outlook desktop app the background image is not showing up.
public void SendSMTPEmail(Employee employee, EmailTemplate email)
{
string supervisorEmail = employee.SupervisorEmail;
string employeeEmail = employee.EmployeeEmail;
string subject = string.Format("Welcome {0}", employee.EmployeeFirstName);
string mailBody = email.TemplateBaseHTMLString;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailBody, null, "text/html");
var imageDataString = email.ImageBase64String;
var baseIndex = imageDataString.IndexOf("base64,", StringComparison.OrdinalIgnoreCase);
if (baseIndex > -1)
{
imageDataString = imageDataString.Substring(baseIndex + 7);
}
var imageData = Convert.FromBase64String(imageDataString);
using (var imageStream = new MemoryStream(imageData))
{
//LinkedResource theEmailImage = new LinkedResource(imageStream, email.ImageMimeType);
LinkedResource theEmailImage = new LinkedResource(imageStream, new ContentType { MediaType = email.ImageMimeType });
theEmailImage.ContentId = "myImageID";
htmlView.LinkedResources.Add(theEmailImage);
TriggerMail(supervisorEmail, employeeEmail, subject, htmlView, imageStream, email.ImageMimeType);
}
//Console.WriteLine("Email send successfully");
}
private static void TriggerMail(string supervisorEmail, string employeeEmail, string subject, AlternateView altView = null, Stream imageStream = null, string mimeType = null, string mailBody = null)
{
bool isValidEmployeeEmail = IsValidEmail(employeeEmail);
bool isValidSupervisorEmail = IsValidEmail(supervisorEmail);
if (isValidEmployeeEmail || isValidSupervisorEmail)
{
try
{
string formEmailAddress = ConfigurationManager.AppSettings["FormEmailAddress"];
string serverName = ConfigurationManager.AppSettings["SMTPServer"];
string smtpPort = ConfigurationManager.AppSettings["SMTPPort"];
SmtpClient client = new SmtpClient(serverName);
int port = 0;
if (int.TryParse(smtpPort, out port))
{
client.Port = port;
}
MailAddress from = new MailAddress(formEmailAddress);
MailAddress to = null;
MailAddress cc = null;
if (string.IsNullOrWhiteSpace(employeeEmail) || !isValidEmployeeEmail)
{
to = new MailAddress(supervisorEmail);
}
else
{
to = new MailAddress(employeeEmail);
if (!string.IsNullOrWhiteSpace(supervisorEmail) && isValidSupervisorEmail)
{
cc = new MailAddress(supervisorEmail);
}
}
using (MailMessage message = new MailMessage(from, to))
{
if (cc != null)
{
message.CC.Add(cc);
}
if (altView != null)
{
message.AlternateViews.Add(altView);
}
else
{
message.Body = mailBody;
}
message.Subject = subject;
message.IsBodyHtml = true;
var userName = ConfigurationManager.AppSettings["EmailUserId"];
var pwd = ConfigurationManager.AppSettings["EmailPassword"];
if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(pwd))
{
client.Credentials = new System.Net.NetworkCredential(userName, pwd);
}
client.EnableSsl = ConfigurationManager.AppSettings["IsEnableSSL"] == "Y";
client.Send(message);
//client.Send(mail);
message.Dispose();
}
}
catch (Exception ex)
{
//log
}
}
}