1

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(); } } } 
2
  • why are you using Interop you can do this much easier using SMTP if 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.. ? Commented Jan 13, 2016 at 17:07
  • Need some more details--Can you debug the addin? Is your SendEmailtoContacts() function ever getting called on ThisAddIn_Startup? If you comment out the .Send() line then the Outlook window should actually remain open on your desktop and you should be able see the email waiting to be sent. Commented Jan 13, 2016 at 17:23

2 Answers 2

1

Your question title says "c# console app to send email automatically" but you appear to be using code that was meant for an Outlook AddIn. The addin is meant to call the SendEmail function upon startup of the addin which would execute ThisAddIn_Startup:

 private void ThisAddIn_Startup(object sender, System.EventArgs e) { // this is never executed because it isn't an outlook addin SendEmailtoContacts(); } private void SendEmailtoContacts() { // sends your email... if it actually gets called } 

Instead try calling that SendEmailtoContacts() function from your Main() because it is a console app and Main() is what is actually executed when you run it:

 static void Main(string[] args) { SendEmailtoContacts(); } 

For further debugging, as I noted in a comment, because this is outlook interop you should be able to see these operations taking place in an outlook window as they are executed on your local desktop. For example, if you comment out the final ((Outlook._MailItem)eMail).Send(); line and run the program you should be left with an email waiting for you to click the send button after your program terminates.


It looks like you'll also have to modify the method signatures of the other functions to static methods and get rid of the this reference in this.CreateEmailItem?

 public 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); } } } public 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(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

i cannot call SendEmailtoContacts() in main. Visual Studio wont recognize it as a method so i tried making it public static and now there is an error under the foreach where it says this.CreateEmailItem. and outlook doesnt come up at all when i run this. The reason i want to use outlook is that this is for my job and there are security blocks everywhere, i was told to use outlook because it would use my current credential to log in and send
You're on the right track recgnizing that it wasn't a static method so SendEmailtoContacts couldn't be called. The other issue is that because this was an addon this must have referred to some other function that creates an email
yeah, i mean im optimizing someone elses code so it might be best to speak with them because i might be missing something. ill play around with the smtp way. Im not gunna lie its kind of bothering me how i cant get this to work, has anyone had success in using the outlook add in an automated way, ive done it with windows forms apps but i just want to run an exe that will send the email. jw thanks all for help
Oh, the problem was just that the CreateEmailItem function wasn't a static method and you were trying to access it with this
0

you could use SMTP to do this

using System.Net; using System.Net.Mail; using System.Configuration; static void Main(string[] args) { SendEmail("[email protected]", "This is a Test email", "This is where the Body of the Email goes"); } public static void SendEmail(string sTo, string subject, string body) { var Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]); using (var client = new SmtpClient(Your EmailHost, Port)) using (var message = new MailMessage() { From = new MailAddress(FromEmail), Subject = subject, Body = body }) { message.To.Add(sTo); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["EmailCredential"], ConfigurationManager.AppSettings["EmailPassword"]); client.EnableSsl = true; client.Send(message); }; } 

if you want a list of contacts to send the same email then create a List<string> and using a foreach loop add them to the message.To.Add(sTo) portion of the code.. pretty straight forward..

if you insist on using Outlook to send emails then looks at this MSDN link

Programmatically Send E-Mail Programmatically

11 Comments

im getting red squigglies everywhere on that smtp snippet, what namespace are u using? just Symstem.net....after i type int.PArse there is no option for Configuration manager, im trying to figure out if im moissing a reference , thanks
I will update.. keep in mind I am not using Interop so these will not be needed in my working example using System.Runtime.InteropServices; using Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook;
yeah i got interop to work now i am trying to use smtp, im trying to write code that utilizes smtp also
smtp is simpler then Interop not sure why you're having such issues ..perhaps if you update your question and post code that pertains to what I have shown you.. I have tested this in a Console app just now.. and It works like a charm.
can u explain this line [ var Port int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.