Unable to send embedded images with Azure Communication Service SMTP
Aalap
0
Reputation points
I'm unable to send an email with embedded images with ACS SMTP. The same code works for other non-ACS SMTP servers. I'm creating an alternate view and a linked resource with matching ContentID.
public void SendEmail()
{
MailMessage mail = new MailMessage
{
From = new MailAddress(_sender),
Subject = "Test Email with embedded image",
IsBodyHtml = true
};
mail.To.Add(_recipient);
string htmlBody = "<html><body><p>This is an email with an embedded image.</p><img src='cid:image001'></body></html>";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
using (FileStream imageStream = new FileStream(@"D:\Pictures\logo.jpg", FileMode.Open, FileAccess.Read))
{
LinkedResource inlineImage = new LinkedResource(imageStream, "image/jpeg") { ContentId = "image001" };
htmlView.LinkedResources.Add(inlineImage);
mail.AlternateViews.Add(htmlView);
SmtpClient smtpClient = new SmtpClient(_smtpHost)
{
Credentials = new NetworkCredential(_smtpUsername, _smtpPassword),
Port = 587,
EnableSsl = true
};
try
{
smtpClient.Send(mail);
Console.Write("Email sent successfully!");
}
catch (Exception ex)
{
Console.Write("Error sending email: " + ex.Message);
}
}
}
Sign in to answer