簡體   English   中英

Microsoft Outlook發送和電子郵件自動化C#

[英]Microsoft Outlook Send and Email Automation C#

如何發送包含主題,附件,正文等的電子郵件。我在網站上查看過,他們提供的只是Visual Basic代碼。 我已經添加了參考:

 using Microsoft.Office.Interop.Outlook;

你應該提高你的谷歌搜索技巧;-)

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Outlook_SendMailItem
{
    public class Class1
    {
        public static int Main(string[]args)
        {
            try
            {
                // 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.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("e-mail address");
                oRecip.Resolve();

                //Set the basic properties.
                oMsg.Subject = "This is the subject of the test message";
                oMsg.Body = "This is the text in the message.";

                //Add an attachment.
                // TODO: change file path where appropriate
                String sSource = "C:\\setupxlg.txt";
                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.
                oRecip = null;
                oAttach = null;
                oMsg = null;
                oApp = null;
            }

                // Simple error handler.
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught: ", e);
            }

            //Default return value.
            return 0;

        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM