C# 获取Exchange 2013 发送的邮件附件并保存(截取邮箱内容)
本帖最后由 MartinLee 于 2019-6-24 17:59 编辑需求是在一台Windows server 2012的服务器上获取Exchange 2013收件人信息及附件
项目**核心源码**是
```
/// <summary>
/// Invoked by Exchange when a message has been submitted.
/// </summary>
/// <param name="source">The source of this event.</param>
/// <param name="args">Arguments for this event.</param>
void SubmittedMessageHandler(SubmittedMessageEventSource source, QueuedMessageEventArgs args)
{
string sPath = @"C:\test\";
if (args.MailItem.Message.Attachments != null && args.MailItem.Message.Attachments.Count > 0)
{
var mailItem = args.MailItem;
try
{
/// 获取收件人
foreach (var recipient in mailItem.Recipients)
{
string dirName = sPath + recipient.Address.LocalPart + '@' + recipient.Address.DomainPart;
if (!Directory.Exists(dirName))
{
Directory.CreateDirectory(dirName);
}
MoveFile(dirName+ "\\", args.MailItem.Message.Attachments);
}
}
catch (Exception ex)
{
}
}
}
```
在MailItem中的Message中能拿到收件人及附件信息。根据这个创建文件夹写入文件即可。
**这是powershell命令**
`
$EXDIR="C:\InterceptEmailAttachment"
net stop MSExchangeTransport
write-output "Copying files"
new-item -Type Directory -path $EXDIR -ErrorAction SilentlyContinue
write-output "Copying files"
copy-item InterceptAttachment.dll $EXDIR -force
write-output "Registering InterceptEmailAttachment"
install-transportagent -Name "Intercept Email Attachment" -AssemblyPath $EXDIR\InterceptAttachment.dll -TransportAgentFactory Microsoft.Exchange.Samples.InterceptAttachment.InterceptAttachmentAgentFactory
write-output "Enabling agent"
enable-transportagent -Identity "Intercept Email Attachment"
get-transportagent -Identity "Intercept Email Attachment"
write-output "Starting Edge Transport"
net start MSExchangeTransport
Write-Output "Installation complete. Please exit the Exchange Management Shell."
`
个人不太擅长描述,有啥疑问请提问。 大佬请别教训小弟积极性。如有违规请提示删除
项目源码已发到GitHub
https://img-blog.csdnimg.cn/20190510155125227.gif
图片略大没法传上来,用CSDN 的可以否,不行的话删了这回复就行。 其实这个程序很有意思的...:lol MartinLee 发表于 2019-5-10 17:25
图片略大没法传上来,用CSDN 的可以否,不行的话删了这回复就行。
怎么删除这个回复呢... 所以github地址是多少。。。。
页:
[1]