本帖最后由 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
|