c#console app自动发送电子邮件

时间:2016-01-13 16:41:59

标签: c# email outlook

我想使用c#和outlook发送自动电子邮件。我很困惑如何实际发送电子邮件,我认为.Send()方法将执行此但没有任何反应,当我运行这个,我没有得到任何编译错误。

有谁知道如何激活/执行此代码或知道某处我搞砸了。谢谢。

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)
        {

        }
    } 
}

/////////////////////////////////////////////// ////////////

正确代码:

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@WHATEVER.COM", "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 个答案:

答案 0 :(得分:1)

您可以使用SMTP来执行此操作

using System.Net;
using System.Net.Mail;
using System.Configuration;

static void Main(string[] args)
{
   SendEmail("Lukeriggz@gmail.com", "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);
    };
}

如果您希望联系人列表发送相同的电子邮件,请创建List<string>并使用foreach循环将其添加到代码的message.To.Add(sTo)部分..非常直接..

如果您坚持使用Outlook发送电子邮件,请查看此MSDN链接

Programmatically Send E-Mail Programmatically

答案 1 :(得分:0)

您的问题标题是&#34; c#控制台应用自动发送电子邮件&#34;但您似乎使用的是用于Outlook AddIn 的代码。 addin用于在启动addin时调用SendEmail函数,该函数将执行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
    }

请尝试从SendEmailtoContacts()调用Main()函数,因为它是一个控制台应用程序,Main()是运行时实际执行的内容:

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

正如我在评论中所指出的那样进一步调试,因为这是Outlook互操作,你应该能够看到这些操作发生在Outlook窗口中,因为它们是在本地桌面上执行的。例如,如果您注释掉最后的((Outlook._MailItem)eMail).Send();行并运行该程序,则应该在程序终止后留下电子邮件,等待您单击“发送”按钮。

看起来您还必须将其他函数的方法签名修改为静态方法,并删除this.CreateEmailItem中的 this 引用?

        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();
        }