C# 使用 Outlook 发送邮件

标签 c# c#-4.0 console-application sendmail outlook-2010

我正在控制台应用程序中工作,我使用下面的 C# 代码在按钮单击事件上自动发送邮件,

    public void SendMail()
    {
        //// Create the Outlook application by using inline initialization.
        Outlook.Application oApp = new Outlook.Application();

        ////Create the new message by using the simplest approach.
        Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        //Add a recipient.
        // TODO: Change the following recipient where appropriate.
        Outlook.Recipients oRecips = oMsg.Recipients;
        List<string> oTORecip = new List<string>();
        List<string> oCCRecip = new List<string>();

        oTORecip.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c5d8c1cdd0ccc5e0d4c5d3d48ec3cfcd" rel="noreferrer noopener nofollow">[email protected]</a>");
        oCCRecip.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e4b564f435e424b6e5a4b5d5a004d4143" rel="noreferrer noopener nofollow">[email protected]</a>");
        foreach (string t in oTORecip)
        {
            Outlook.Recipient oTORecipt = oRecips.Add(t);
            oTORecipt.Type = (int)Outlook.OlMailRecipientType.olTo;
            oTORecipt.Resolve();
        }

        foreach (string t in oCCRecip)
        {
            Outlook.Recipient oCCRecipt = oRecips.Add(t);
            oCCRecipt.Type = (int)Outlook.OlMailRecipientType.olCC;
            oCCRecipt.Resolve();
        }

        //Set the basic properties.
        oMsg.Subject = "TestMail- " + DateTime.Today.ToString("MM/dd/yyyy");
        oMsg.HTMLBody = "<html>" +
            "<head>" +
            "<title>TestMail</title>" +
            "</head>" +
            "<body style='background-color:#E6E6E6;'>" +
            "<div style='font-family: Georgia, Arial; font-size:14px; '>Hi,<br /><br />" +
            "PFA<br />" +
            "This is Test Mail.Please Ignore.<br /><br /><br /><br /><br />" +
            "Thanks & Regards<br />" +
            "test"+
            "</div>" +
            "</body>" +
            "</html>";
        string date = DateTime.Today.ToString("MM-dd-yyyy");

        //Add an attachment.
        // TODO: change file path where appropriate
        String sSource = "D:\\Test\\test_" + date + ".xlsx";
        String sDisplayName = "MyFirstAttachment";
        int iPosition = (int)oMsg.Body.Length + 1;
        int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
        Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);

        // If you want to, display the message.
        // oMsg.Display(true);  //modal

        //Send the message.
        oMsg.Save();
        oMsg.Send();

        //Explicitly release objects.
        oTORecip = null;
        oCCRecip = null;
        oAttach = null;
        oMsg = null;
        oApp = null;
    }

这段代码工作正常。我想使用另一个帐户发送邮件。如何在上面的代码中给出发件人地址? 是否可以这样做?我需要使用什么 C# 代码来使用 C# 代码代表其他帐户发送邮件?

最佳答案

在 oMsg 上设置 MailItem.SendUsingAccount 属性

http://msdn.microsoft.com/en-us/library/office/ff869311%28v=office.14%29.aspx

您只能从 Application.Session.Accounts 集合中进行选择。

或者可能检查

MailItem.SentOnBehalfOfName http://msdn.microsoft.com/en-us/library/office/ff862145%28v=office.14%29.aspx

(用于交换)

还有一些类似的帖子:

Outlook automation - Change Sender Account

Sending defer message delivery and change default account using Powershell

编辑

以下代码可以使用智能感知,请检查您的 DLL 版本。使用 Outlook PIA v 14 和 v 12 进行检查。

包下载自 http://www.microsoft.com/en-us/download/details.aspx?id=43664

using Microsoft.Office.Interop.Outlook;
//C:\Program Files\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Outlook.dll
//14.0.0.0 or 12.0.0.0 - it works from Outlook 2007 and higher

namespace ConsoleApplication1
{
    class Program
    {
        public void SendMail()
        {
            //// Create the Outlook application by using inline initialization.
            Application oApp = new Application();
            ////Create the new message by using the simplest approach.
            MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
            oMsg.SendUsingAccount = oApp.Session.Accounts[2]; // it starts at 1

...

您似乎正在使用其他一些程序集 DLL,因为对我来说,语句“using Microsoft.Office.Interop;”根本不起作用。

关于C# 使用 Outlook 发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817558/

相关文章:

c# - 调用基函数然后继承函数

c# - 如何在 ASP.NET Web API 实现中将数组传递给 OData 函数?

c#-4.0 - 如何订阅 IObservable,但缓冲其中的数据,直到另一个 IObservable 发布?

.net - 如何使用 Ninject 在依赖链下传递参数

c# - 如何将这些调用放在 SqlTransaction 中?

winapi - 带有 ReadConsoleInputW 的 Windows 控制台中的 Ctrl-S 输入事件

.net - VS 2017 (.net core) 中的 Nuget 包安装失败

c# - 如何连接到 Windows 手机上的 SQLite(CE) 数据库?

c# - 字体大时 DataGridView 复选框不显示

c++ - SetConsoleCtrlHandler 例程问题