c控制台应用程序自动发送电子邮件

本文关键字:电子邮件 控制台 应用程序 | 更新日期: 2023-09-27 18:30:14

我想用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.OlDefaultFolders.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();
    }

} 
}

c控制台应用程序自动发送电子邮件

您的问题标题显示"c#控制台应用程序自动发送电子邮件",但您似乎使用的是适用于OutlookAddIn的代码。该插件旨在在启动将执行ThisAddIn_Startup:的插件时调用SendEmail函数

    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
    }

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

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

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


看起来您还必须将其他函数的方法签名修改为静态方法,并去掉其中的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();
        }

您可以使用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链接

以编程方式发送电子邮件以编程方式