Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
1,020 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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);
}
}
}