设为首页 收藏本站
查看: 1742|回复: 0

[经验分享] 浅谈Exchange 2013开发-如何操作邮件的附件

[复制链接]

尚未签到

发表于 2015-9-10 13:51:59 | 显示全部楼层 |阅读模式
  因为项目中客户有一个的要求,所以这个Exchange前段时间搞的我很是头疼,没接触过这个东西,但是现在看来,纸老虎一个。希望我的经验可以帮助初次接触它的人少走一些弯路!
  简单介绍一下:客户要求在自己的Exchange生产环境上创建一个传输规则,当用户邮件中包含有pdf类型的附件时,将这个附件下载到本地服务器,然后在加上可预览这个附件的链接(服务商提供的web应用程序而不是微软提供的的Office Web App Services)。
  环境:一台DC,一台Exchange Server 2013服务器,建议8G内存至少
  
  ----------------------------------------------------------------------------------------
  如何把邮件的附件Load下来网上应该很多源代码,基本上都是使用Exchange Web Service,简单说如下:



public void StripAttachments(ItemId id, string folder)
{
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new NetworkCredential("Administrator", "Password01!", "TEST.com");
service.Url = new Uri("https://192.168.1.116/ews/exchange.asmx");
EmailMessage email = EmailMessage.Bind(service, id);
foreach (Attachment attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
if (attachment.Name.Contains("pdf"))
{
// do your thing
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
}
}
}
}
catch (Exception e)
{
//
            }
  对已经存在于收件箱的邮件使用它操作最好不过了,但是不符合我的要求。因为我要对所有经过边缘传输服务器的邮件进行操作。
  微软对于用户自定义传输规则提供了两个方式SmtpReceiveAgent和RoutingAgent,我使用了后者。



namespace Microsoft.Exchange.Data.Transport.Routing
{
public abstract class QueuedMessageEventArgs : EventArgs
{
public abstract MailItem MailItem { get; }
}
}
QueuedMessageEventArgs的MailItem为我们提供了操作邮件的支持
声明一个继承自RoutingAgentFactory的类,重写CreateAgent方法



public sealed class MyRoutingFactory : RoutingAgentFactory
{
public override RoutingAgent CreateAgent(SmtpServer server)
{
RoutingAgent arcEmail = new EmailArchivingRoutingAgent();
return arcEmail;
}
}

  声明一个操作类



public class EmailArchivingRoutingAgent : RoutingAgent
  构造方法中绑定触发事件



public EmailArchivingRoutingAgent()
{
//Invoked by Exchange when the entire message has been Submitted.
base.OnSubmittedMessage += new SubmittedMessageEventHandler(EmailArchivingRoutingAgent_OnSubmittedMessage);
}
  然后我们在EmailArchivingRoutingAgent_OnSubmittedMessage这个方法里面去写对邮件的具体操作。通过循环e.MailItem.Message.Attachments,访问所有附件对象,然后判断类型再保存。



public void EmailArchivingRoutingAgent_OnSubmittedMessage(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
{
for (int index = e.MailItem.Message.Attachments.Count - 1; index >= 0; index--)
{
//Get Attachment
Microsoft.Exchange.Data.Transport.Email.Attachment atAttach = e.MailItem.Message.Attachments[index];
//Get FileName of this Attachment
String feFileExtension = string.Empty;
//Effective Attachment
if (atAttach.AttachmentType == Microsoft.Exchange.Data.Transport.Email.AttachmentType.Regular & atAttach.FileName != null)
{
feFileExtension = atAttach.FileName.Substring((atAttach.FileName.Length - 4), 4);
}
//Judge the type of Attachment
if (feFileExtension.ToLower() == ".pdf")
{
FileStream atFileStream = File.Create("E:\\Share\\" + atAttach.FileName);
Stream attachstream = atAttach.GetContentReadStream();
byte[] bytes = ReadFully(attachstream, (int)attachstream.Length);
//byte[] bytes = this.StreamToBytes(attachstream);
atFileStream.Write(bytes, 0, bytes.Length);
atFileStream.Close();
atFileStream = null;
bytes = null;
attachstream.Close();
attachstream = null;
atAttach = null;
//After load the Attachment,wirte a Link message into Body of mail
if (File.Exists("E:\\Share\\" + e.MailItem.Message.Attachments[index].FileName))
{
this.ChangeBodyOfMail(source, e,index);
e.MailItem.Message.Subject += " PDF 写在E盘了";
}
}
}
ReadFully是把GetContentReadStream()返回的流转化为byte[]的方法


public static byte[] ReadFully(Stream stream, int initalLength)
{
if (initalLength < 1)
{
//min size
initalLength = 32768;
}
byte[] buffer = new byte[initalLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
//if we've reached the end of our buffer,check to see if there's
//any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
//End of stream?if so we're done
if (nextByte == -1)
{
return buffer;
}
//nope,Resize the buffer,putin the byte we've just
//read,and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
//Buffer is now too big,shrink it
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
  当然,一般安全的写法不是直接使用GetContentReadStream(),而是使用TryGetContentReadStream(),如下:



Stream stream = null;
if(attachment.TryGetContentReadStream(out stream))
{操作}
  然后把编译生成的dll部署到Exchange生产环境即可。
  管理员身份运行Exchange Management Shell,输入如下:
  net stop msexchangetransport #停止传输服务
  install-transportagent -name "Myagent" -assemblypath C:\Agent\Agents.dll -transportagentfactory Agents.MyRoutingFactory
enable-transportagent -identity "Myagent"
  net start msexchangetransport #启动传输服务
DSC0000.jpg
  再看看保存的pdf

  补充一下:
  如果发生不能写入的问题:
是NTFS的权限问题,IIS的帐户没有权限在E盘根目录写入数据。
如果是临时文件,建议把文件保存到临时目录里去,可以用System.IO.Path.GetTempPath方法得到临时目录的路径。
如果不是临时文件,建议专门在服务器上创建一个专用的文件夹,并且使IUSR_matchinename或NETWORK_SERVICE帐号对这个文件夹有写入的权限,然后把文件保存到这个文件里。
  不足之处请大家指出...

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-112017-1-1.html 上篇帖子: 管理Exchange收件人 下篇帖子: Exchange编程学习实例1--定期删除管理员邮件 vbs
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表