1

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.

Cam06002
  • 21
  • 6
  • Show the code you have now, that works locally for you. The email cannot include a reference to either a file or a resource, because email might be read on a device that lacks the image. When you made the email HTML, did you create an embedded image? If not, first step is to Google embed image in email. – ToolmakerSteve Oct 05 '22 at 22:57
  • The first block of code works beautifully. It's the second block of code, where I try to pull from the resource instead of the full path that is giving me an issue. – Cam06002 Oct 05 '22 at 23:00
  • *"The first block of code works beautifully. "* What happens if you mail it to yourself, and read on some other device? Does it still work? (You haven't responded to my question about whether you create an "embedded image" in the email. If you don't, then it only works if you read the email on the same pc.) – ToolmakerSteve Oct 07 '22 at 01:51
  • There are numerous existing questions about creating MailItem attachments from resources, streams, etc. this is not a unique MAUI issue – Jason Oct 07 '22 at 10:59
  • @ToolmakerSteve, I am able to see it on any other device emailed with the first block of code, no problem. Jason, my understanding about this being a Maui issue is due to how Maui handles its resources, which seems to be unique (though I could be mistaken). I've taken a look at and tried several other ways to attach without the full path, and it does not seem to find the resource. Please see my edited question for a more complete list of what I've tried. As far as I can tell, the issue is directly related to not having a correct path to get to my resources in the app, only locally. – Cam06002 Oct 07 '22 at 19:18
  • OK, that is what I needed to know. Good attempts so far, but, AFAIK, none of the links you show get at the essence of the problem. They all attempt to use a **file url**, which won't work unless you actually have a file. Two ways to solve this: a) find a different approach, that does not rely on having a file url. or b) copy the resource to a file, then use its url. [I've never done either of these, so I can't be more specific than that. I do know that (b) is commonly done in Xamarin Forms for other purposes; should be similar in Maui.] – ToolmakerSteve Oct 11 '22 at 19:34
  • I'll dive into Xamarin Forms docs and forums and see if I can find anything. Thanks for the suggestion! – Cam06002 Oct 11 '22 at 20:11
  • @ToolmakerSteve I was able to find something that seems to be pointing me in the right direction. It's working on my development computer, but not on a co-worker's computer. Please see my update and let me know if you have any suggestions. – Cam06002 Oct 14 '22 at 15:29

0 Answers0