Microsoft Outlook发送和电子邮件自动化C# [英] Microsoft Outlook Send and Email Automation C#

查看:284
本文介绍了Microsoft Outlook发送和电子邮件自动化C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何发送包含主题,附件,正文等的电子邮件.我在网站上查看过的内容仅是Visual Basic代码. 我已经添加了参考:

How can I send an email with Subject, Attachment, Body, etc. I have looked on the website and all they give is Visual Basic Code. I already added the reference:

 using Microsoft.Office.Interop.Outlook;

推荐答案

您应该提高自己的谷歌搜索技能;-)

you should improve your googling skills ;-)

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;

        }

    }
}

这篇关于Microsoft Outlook发送和电子邮件自动化C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆