I accept this because it helped me to find solution, thou. I had to do like 2 hours of research still on the stuff. Some of your code didn’t work for me, after I installed package I still needed to use the full path to the objects I couldn’t use aliases for namespace (using didn’t work) for me.
I did not use everything from your code I don’t need to check other attachments, because I always start with a template that has no attachments, and the file I create is always deleted If exist prior to this code running.
Also one little note that oMsg.To doesn’t have any methods no “Add” it’s just have constructor set/get, after digging I figured you simply use it as a variable, and for some reason System.Net.Mail.MailAddress …that didn;'t work for me, I simply did this line to add email as string:
oMsg.To = “email@blabla.com”;
It worked when I open in Outlook address is there, I have to check that if it works correctly (when I send), maybe I need to format it later with your code and I need to find why it didn’t work for me, but “Add” is not working. (I don’t know if studio version matters when you use .Net code)
As I also mentioned, you referred to “vb.net” but you wrote c# code, I’d like to mention that to prevent some confusion. Yes .NET is pretty similar, but your code is c# not VB.
I still accepted your solution, it mostly worked after I fully typed in the lines.
Since topic is closed I can just edit this message to put the solition “fixed” as you requested, I’ll just drop some screenshots:
Arguments:
Code:
//Creating Office object with name oApp
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
//Creating email Item object with the name oMsg
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
//Assign the template message file to the oMsg object, casting type too
oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.Session.OpenSharedItem(in_TemplatePath);
//Create attachments object (assigning the ponter from oMsg)
Microsoft.Office.Interop.Outlook.Attachments oAtts = oMsg.Attachments;
//Add the attachment with path and name
oAtts.Add(in_AttachmentPath,Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue,1,in_AttachmentName);
//assign Addressee (oMsg.To has constructor so it can be just used similar to a varible in programming.
oMsg.To = in_To;
//saving email
oMsg.SaveAs(in_SavePath);
//deleting email from Outlook
oMsg.Delete();
//Free all objects from memory
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAtts);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oMsg);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp);
I also commented all lines in case someone in future (with less programming knowledge) need to look at it later, for some reason namespaces didn’t work for me so I also had to fully type in all method names and properties… I use old 20.4 version studio for this…