[转]C#操作Outlook

2021-06-05 14:04

阅读:337

标签:.dll   本地磁盘   net   mbed   mic   compute   string   ons   excel   

本文转自:https://blog.csdn.net/yanlovehan/article/details/8500449

//引用Microsoft.Office.Interop.Outlook.dll
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;


//创建Application
Outlook.Application myOutlookApp = new Outlook.Application();
Outlook.NameSpace myNameSpace = myOutlookApp.GetNamespace("MAPI");


//本地邮箱
Outlook.MAPIFolder myFolderInbox = myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);//获取收件箱对象,如获取其他箱可在参数中控制
Outlook.MAPIFolder myFolder = myFolderInbox.Folders["xx"];//“xx”为收件箱下的一个文件夹
Outlook.MAPIFolder MyParentFolder = myFolderInbox.Parent as Outlook.MAPIFolder;//获取收件箱上一级的文件夹对象,以次来获取与收件箱同级的文件夹
Outlook.MAPIFolder MyNewFolder = MyParentFolder.Folders["yy"];//“yy”为与收件箱同级的文件夹


//公共邮箱
string publicMailBoxName="jpn_eco_ci04@hp.com";
Outlook.Recipient objRecipient = myNameSpace.CreateRecipient(publicMailBoxName);//jpn_eco_ci04@hp.com为公共邮箱地址的全名称
Outlook.MAPIFolder myFolderInbox = myNameSpace.GetSharedDefaultFolder(objRecipient, OlDefaultFolders.olFolderInbox);
Outlook.MAPIFolder myFolder = myFolderInbox.Parent as Outlook.MAPIFolder;
Outlook.MAPIFolder myNewFolder = myFolder.Folders["(Z) 処理済"];


//指定发送的邮箱为公共邮箱
string publicMailBoxName = "vpchi.dlbc@hp.com";
mail.SentOnBehalfOfName = publicMailBoxName;

 


//***筛选***
//根据Subject属性对邮件进行筛选
const string subject = "BIS B2B Document Delivery";
string sFilter = "@SQL=" + "\"" + "urn:schemas:httpmail:subject" + "\"" + " like ‘%" + subject + "%‘";
//根据邮件的收取时间进行筛选
Outlook.Items myMails = myFolderInbox.Items.Restrict(sFilter) as Outlook.Items;
//通配符
通配符
%
包含零个或多个字符的任意字符串。
WHERE title LIKE ‘%computer%‘ 将查找在书名中任意位置包含单词 "computer" 的所有书名。
_(下划线)
任何单个字符。
WHERE au_fname LIKE ‘_ean‘ 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。
[ ]
指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。
WHERE au_lname LIKE ‘[C-P]arsen‘ 将查找以 arsen 结尾并且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如 Carsen、Larsen、Karsen 等。在范围搜索中,范围包含的字符可能因排序规则的排序规则而异。
[^]
不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。
WHERE au_lname LIKE ‘de[^l]%‘ 将查找以 de 开始并且其后的字母不为 l 的所有作者的姓氏。


//1.获取邮件对象
//2.根据筛选条件移动邮件
//3.获取附件并将其存到本地磁盘
const string filePath = @"C:\Print Attachment Folder\";
if (MyMails.Count != 0)
{
    do
    {
        Outlook.MailItem myMail = myMails[1] as Outlook.MailItem;
        Outlook.Attachments myAttachments = myMail.Attachments as Outlook.Attachments;
        foreach (Outlook.Attachment myAttachment in myAttachments)
        {
            myAttachment.SaveAsFile(filePath + myAttachment.FileName);
        }
        myMail.Move(MyMoveFolder);
        myMails = MyFolderInbox.Items.Restrict(sfilter) as Outlook.Items;
    }
    while (myMails.Count != 0);
}

 


//创建新邮件并添加附件发送
Outlook.Application myOutlookApp = new Outlook.Application();
Outlook.MailItem mail = myOutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Attachments.Add(TemplateExcel, Outlook.OlAttachmentType.olEmbeddeditem, 1, System.Type.Missing);
mail.Subject = "QC Check File";
string to = string.Empty; ;
for (int i = 0; i {
    to = to + mails[i] + ";";
}
mail.To = to;
mail.Send();
---------------------
作者:刘岩Tony
来源:CSDN
原文:https://blog.csdn.net/yanlovehan/article/details/8500449
版权声明:本文为博主原创文章,转载请附上博文链接!

[转]C#操作Outlook

标签:.dll   本地磁盘   net   mbed   mic   compute   string   ons   excel   

原文地址:https://www.cnblogs.com/freeliver54/p/10801552.html


评论


亲,登录后才可以留言!