@Andreas Sundberg Thanks for posting your question in Microsoft Q&A, apologize for any inconvenience caused on this.
I have modified the above shared code and using the below I am able to send an email from ACS using SMTP library (System.Net.Mail) with inline image successfully without any issues.
Modified Code:
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace EmailTest
{
public class EmailService
{
private string _recipient;
private string _sender;
private string _smtpAuthUsername;
private string _smtpAuthPassword;
private string _smtpServer;
public EmailService(string recipient, string sender, string smtpServer, string smtpAuthUsername, string smtpAuthPassword)
{
_recipient = recipient;
_sender = sender;
_smtpServer = smtpServer;
_smtpAuthUsername = smtpAuthUsername;
_smtpAuthPassword = smtpAuthPassword;
}
public void SendEmail()
{
// Set up the email message
MailMessage mail = new MailMessage
{
From = new MailAddress(_sender),
Subject = "Test Email with inline Image",
IsBodyHtml = true
};
mail.To.Add(_recipient);
SmtpClient smtpClient = new SmtpClient(_smtpServer)
{
Port = 587, // or your SMTP port
Credentials = new NetworkCredential(_smtpAuthUsername, _smtpAuthPassword),
EnableSsl = true
};
try
{
// Send the email
string fileName = "D:\\acssmtpimage.png";
FileStream imageStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Attachment imageAttachment = new Attachment(imageStream, new ContentType(MediaTypeNames.Image.Jpeg));
imageAttachment.ContentDisposition.Inline = true;
imageAttachment.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
imageAttachment.ContentId = "image1";
mail.Attachments.Add(imageAttachment);
string htmlBody = "<html><body><h1>Hello!</h1><p>This is the content of the email.Below email is sent from local machine.<br>Thanks & regards <\br> venkatesh </p><img src=\"cid:image1\"></body></html>";
// Create an AlternateView for the HTML
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
// Add the AlternateView to the email message
mail.AlternateViews.Add(htmlView);
smtpClient.Send(mail);
Console.Write("Email sent successfully!");
}
catch (Exception ex)
{
Console.Write("Error sending email: " + ex.Message);
}
}
}
}
Here is the sample output for your reference:
Hope this helps, let me know if you have any further questions on this.
Please accept as "Yes" if the answer is helpful so that it can help others in the community.