I am trying to send emails from my c# .Net Maui application that includes a company logo in the body of the email above a table. This is working well when I provide the full path like the following:
string rootString = "C:\\Users\\MyName\\source\\repos\\HQApp\\Resources\\Images\\";
var logoPath = rootString + "logo.png";
MsOutlook.Application outlookAp = new MsOutlook.Application();
if (outlookAp == null)
return;
//tableBody code is built in HTML -- unrelated code
MsOutlook.MailItem mail = (MsOutlook.MailItem)outlookAp.CreateItem(MsOutlook.OlItemType.olMailItem);
Attachment logoAttacher = mail.Attachments.Add(logoPath, OlAttachmentType.olEmbeddeditem, null, "Company Logo");
string imageCid = "companyImage";
logoAttacher.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
var imageBody = String.Format("<body><img src=\"cid:{0}\" width='127' height='34' alt=\"\"/></body>", imageCid);
mail.Display(mail);
mail.To = emailTo;
mail.CC = ccTo;
mail.Subject = subject;
mail.HTMLBody = imageBody + tableBody + body + mail.HTMLBody;
mail.Send();
Because this app will eventually be used on the desktops of everyone else on my team, I cannot keep my local path to add the images. I have tried to use a stream based on a few answers that pointed me in that direction, but it does not seem to be able to read the file. This is how I tried to create the stream:
string myLogo = "logo.png";
using (var stream = await FileSystem.OpenAppPackageFileAsync(myLogo))
{
using (var reader = new StreamReader(stream))
{
var logoContent = await reader.ReadToEndAsync();
}
}
Then, in my logoAttacher I pass "logoContent" rather than "logoPath". However, when I try to run it this way, I get an error while debugging telling me that the file cannot be found.
The "logo.png" file is in my Resources -> Images folder in the solution explorer, and the direct path was easy enough to copy into my code. However, I cannot seem to find a way that will allow me to use the file system of the app itself. My thought is I'm likely using the stream incorrectly. Can someone please help me figure out how to get the image embedded in my email using the app resources rather than a local path?
To further explain what I've tried, please see the following links. I have attempted to process my image as directed in each link, to no avail:
.Net Maui - Loading an Embedded Image (Which I then tried to use as my image variable in my emailer) https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/image
Embedding an image in an Email with c# (this is how I eventually got my first block of code to work) C# Embed an image in an outlook email
Via a Linked Resource Constructor https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.linkedresource.-ctor?view=net-6.0#system-net-mail-linkedresource-ctor(system-string)
Using "FileSystem.AppDataDirectory" How to write and read files from .NET MAUI Blazor?
Using alternate view https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.alternateviews?view=net-6.0#system-net-mail-mailmessage-alternateviews & Send inline image in email
I have done a lot of research to try to figure this out, and I can't seem to find anything that works. This is why I am turning to stackoverflow.
UPDATE After some more research, I changed the path (formerly rootString) to the following:
string resFolder = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Resources\\Images\\");
This works well when running in VS. However, the first time I tried this with the published version, I received an error:
Cannot find part of the path C:\ProgramFiles\WindowsApps\HQApp\Resources\Images
I ran it again in VS, found the emailer working, and ran it again from the published version and was successful. However, when running it from another computer, I received the path error mentioned above. I think I'm on the right track, but any guidance would be appreciated.