I would like to send an automated email message using c# and outlook. I am confused how to actually send the email, i thought the .Send() method would execute this but nothing happens when i run this and i do not get any compilation errors.
Does anyone know how to activate/execute this code or know somewhere I am messing up. Thank you.
using System; using System.Management; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; using System.Net.Mail; using System.Net.NetworkInformation; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook; namespace email { class Program { private void ThisAddIn_Startup(object sender, System.EventArgs e) { SendEmailtoContacts(); } private void SendEmailtoContacts() { string subjectEmail = "test results "; string bodyEmail = "test results"; Microsoft.Office.Interop.Outlook.Application appp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MAPIFolder sentContacts = appp.ActiveExplorer().Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.O lDefaultFolders.olFolderContacts); foreach (Outlook.ContactItem contact in sentContacts.Items) { if (contact.Email1Address.Contains("gmail.com")) { this.CreateEmailItem(subjectEmail, contact.Email1Address, bodyEmail); } } } private void CreateEmailItem(string subjectEmail,string toEmail, string bodyEmail) { Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem eMail = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); eMail.Subject = subjectEmail; eMail.To = toEmail; eMail.Body = bodyEmail; eMail.Importance = Outlook.OlImportance.olImportanceLow; ((Outlook._MailItem)eMail).Send(); } static void Main(string[] args) { } } } ///////////////////////////////////////////////////////////
CORRECT CODE:
using System; using System.Management; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; using System.Net.Mail; using System.Net.NetworkInformation; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook; namespace email { class Program { static void Main(string[] args) { SendEmailtoContacts(); CreateEmailItem("yo", "[email protected]", "yoyoyoyoyo"); } private void ThisAddIn_Startup(object sender, System.EventArgs e) { SendEmailtoContacts(); } private static void SendEmailtoContacts() { string subjectEmail = "test results "; string bodyEmail = "test results"; Microsoft.Office.Interop.Outlook.Application appp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MAPIFolder sentContacts = appp.ActiveExplorer().Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.O lDefaultFolders.olFolderContacts);
foreach (Outlook.ContactItem contact in sentContacts.Items) { if (contact.Email1Address.Contains("gmail.com")) { CreateEmailItem(subjectEmail, contact.Email1Address, bodyEmail); } } } private static void CreateEmailItem(string subjectEmail, string toEmail, string bodyEmail) { Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem eMail = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); eMail.Subject = subjectEmail; eMail.To = toEmail; eMail.Body = bodyEmail; eMail.Importance = Outlook.OlImportance.olImportanceLow; ((Outlook._MailItem)eMail).Send(); } } }
SMTPif you would like a working method I can post one for you with much less code that works I notice also that you have a Main entry point.. but you are not executing any code.. so how do you expect to actually launch any code.. ?