日期:2014-05-18 浏览次数:20865 次
MailMessage objMailMessage = new MailMessage(); objMailMessage.From = new MailAddress("Xin_Yin@intra.pegatroncorp.com"); //objMailMessage.To.Add(new MailAddress(To[i])); objMailMessage.To.Add(new MailAddress("kylin_liu@pegatroncorp.com")); objMailMessage.To.Add(new MailAddress("azai_wang@intra.pegatroncorp.com")); objMailMessage.CC.Add(new MailAddress("xin_yin@intra.pegatroncorp.com")); objMailMessage.Subject = message.Subject; objMailMessage.Body = body; objMailMessage.IsBodyHtml = true; System.String strFileName8 = v_Img1; Attachment attached8 = new Attachment(strFileName8); attached8.ContentId = "Img1"; objMailMessage.Attachments.Add(attached8); System.String strFileName5 = v_Img2; Attachment attached5 = new Attachment(strFileName5); attached5.ContentId = "Img2"; objMailMessage.Attachments.Add(attached5); System.String strFileName6 = v_Img3; Attachment attached6 = new Attachment(strFileName6); attached6.ContentId = "Img3"; objMailMessage.Attachments.Add(attached6); Attachment attached7 = new Attachment(_Xls); objMailMessage.Attachments.Add(attached7); SmtpClient sender = new SmtpClient(); sender.Host = "relay-b.sz.pegatroncorp.com"; sender.Port = 25; sender.Credentials = new NetworkCredential("mail賬號", "密碼", "網域"); sender.DeliveryMethod = SmtpDeliveryMethod.Network; //sender.EnableSsl = true; sender.Send(objMailMessage);
------解决方案--------------------
从3年前的blog里翻出来的:
客户端用javascript操作outlook(包括自动加附件)的代码:
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject = "主题";
mailItem.To = "abc@abc.com";
mailItem.HTMLBody = "邮件内容";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var tempfile = "c:\\temp\\t1.txt"
var addr = "http://abc.com/test.aspx?filename=test.pdf";//这个aspx的后台代码比较简单,就是利用Response.ContentType = "application/pdf"; Response.WriteFile(fileName)的方式返回pdf文件
var oHttp = new ActiveXObject("Microsoft.XMLHTTP");//用ajax方法获得pdf文件,本来想用jquery做,但查了一下,说是jquery不直接支持传二进制文件,虽然有别人写的base64的扩充函数,但觉得比较麻烦.
oHttp.open("GET", addr, false);
oHttp.send();
var ostream = new ActiveXObject("ADODB.Stream");//Scripting.FileSystemObject说是不支持二进制文件,只能用这个
with (ostream) {
type = 1; //binary
mode = 3; //readwrite
open();
write(oHttp.responseBody);
savetofile(tempfile, 1); //savecreatenotexist
close();
mailItem.Attachments.Add(tempfile );
fso.DeleteFile(tempfile);//delete tempfile
mailItem.display(0);
}
ostream = null;
oHttp = null;
fso = null;
这个方法的主要缺点,就是必须修改浏览器的安全性设置,ie下:(其他浏览器没有测试)
1.把服务器加到受信任的站点
2.点击自定义级别,这两项必须都设为"启用":
1)对没有标记为安全的ActiveX控件进行初始化和脚本运行
2)下载未签名的ActiveX控件
另外,savetofile函数只接受绝对路径.
所以,这种方法的实用性很低。
结论:要自动添加附件,只能在服务器端做。