C# call outlook send email

C# is easier to calling the outlook send email , because outlook and visual studio are microsoft products.

1.Install the Microsoft.office.Interop.Outlook in Manager NuGet Package 

 

2. using drective

using Outlook = Microsoft.Office.Interop.Outlook;

3. code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Outlook_MainHelper
{
    class Program
    {
        static void Main(string[] args)
        {
            SendMail();
        }
        /// <summary>
        /// outlook send email
        /// </summary>
        public static void SendMail()
        {
            //new outlook application 
            Outlook.Application olApp = new Outlook.Application();
            Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem);
            //set email to , email Subject , email CC 
            mailItem.To = "v-yyauo@microsoft.com";
            mailItem.CC = "zhiyi.yan@pactera.com";
            mailItem.Subject = "C#_SendEmail_Test";
            //set the body
            mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
            string content = "附件为用来测试,请点击查看是否能用";
            content = "收件人,<br /><br />请注意以为内容:<br /><br />" + content + "<br /><br /><br /><br />不要回复此邮件,谢谢;";
            content = content + "<br />\r\n                            <br />Thanks\r\n                 <br />\r\nzhiyi";
            mailItem.HTMLBody = content;
            //send email
            mailItem.Attachments.Add(@"D:\Test.txt");
            ((Outlook._MailItem)mailItem).Send();
            mailItem = null;
            olApp = null;
        }
    }
}
View Code

4.result

5. In the Microsoft Office Outlook need to configure a sender's e-mail account, if it is 163 free e-mail addresses, you need to set the "client authorization password", then use the client authorization password replacement mailbox login password to configure, otherwise it is not send a mail.

how to set the "client authorization password " : https://jingyan.baidu.com/article/636f38bb7964cbd6b84610d3.html

6.Typical problems you may encounter

(1).How to send a simple email

// Create Outlook App

ApplicationClass outlookApp = newMicrosoft.Office.Interop.Outlook.ApplicationClass();
View Code

// Create Outlook object

MailItemmail = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);

// Set Permission : Don't Forward

mail.Permission = OlPermission.olDoNotForward;

// Delivery Report requested

mail.OriginatorDeliveryReportRequested = true;

// Set the message expiration time

mail.ExpiryTime = DateTime.Now.AddMinutes(ConfigParameterManager.ExpiryDay);

// Set the Mail Recipient

mail.To = record.ToAccount;

// Set email Subject

mail.Subject = record.Subject; 

// Set email Format

mail.BodyFormat = OlBodyFormat.olFormatHTML; 

// Set message text, HTML format should be assigned to the HTMLBody property values

mail.HTMLBody = record.Content; 

// Send email

Microsoft.Office.Interop.Outlook._MailItem mailItem = (Microsoft.Office.Interop.Outlook._MailItem)mail;
mailItem.Send();

(2) How to send an email with pictures

// Create email object

MailItemmail= (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);

// Get Picture attachments, and add to the Message object

string headImage = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Image/head.png";

mail.Attachments.Add(headImage, OlAttachmentType.olOLE, 1, "");

Note : The picture is added to the Attachment of resources, refer to it in the HtmlBody inside should look like this:

<img src="cid:head.png"></img>

  

 

posted @ 2017-08-11 16:04  Yanzhiyi  阅读(747)  评论(0)    收藏  举报