代码如下:public partial class SendMail : System.Web.UI.Page
{
private string path=""; protected void Page_Load(object sender, EventArgs e)
{
path = @"~/attachment.xls"; path = Server.MapPath(path); bool result = SendEmail(path); Response.Write(result);
}
public static bool SendEmail(string path)
{
bool isSend = false; System.Web.Mail.MailMessage objMailMessage = new System.Web.Mail.MailMessage(); objMailMessage.From = "cs@***.cc";
objMailMessage.Cc = "***@qq.com;**@21cn.com";
objMailMessage.To = "***@qq.com;**@21cn.com";
objMailMessage.Subject = "Test mail";
objMailMessage.BodyFormat = MailFormat.Html;
objMailMessage.Body = "this is test message."; objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "****");
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "****"); SmtpMail.SmtpServer = "mail.***.cc"; if (!string.IsNullOrEmpty(path))
{
string[] pathArray = path.Split(',');
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < pathArray.Length; i++)
{
data = new Attachment(pathArray[i], MediaTypeNames.Application.Octet);//实例化附件
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(pathArray[i]);//获取附件的创建日期
disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathArray[i]);//获取附件的修改日期
disposition.ReadDate = System.IO.File.GetLastAccessTime(pathArray[i]);//获取附件的读取日期 objMailMessage.Attachments.Add(data);//添加到附件中
}
}
try
{
SmtpMail.Send(objMailMessage);
isSend = true; }
catch (Exception e)
{
return isSend;
} return isSend;
}
}
提交后..打印出来的result为false..为什么? 如果不带附件path为空的时候..能正常发送..
{
private string path=""; protected void Page_Load(object sender, EventArgs e)
{
path = @"~/attachment.xls"; path = Server.MapPath(path); bool result = SendEmail(path); Response.Write(result);
}
public static bool SendEmail(string path)
{
bool isSend = false; System.Web.Mail.MailMessage objMailMessage = new System.Web.Mail.MailMessage(); objMailMessage.From = "cs@***.cc";
objMailMessage.Cc = "***@qq.com;**@21cn.com";
objMailMessage.To = "***@qq.com;**@21cn.com";
objMailMessage.Subject = "Test mail";
objMailMessage.BodyFormat = MailFormat.Html;
objMailMessage.Body = "this is test message."; objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "****");
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "****"); SmtpMail.SmtpServer = "mail.***.cc"; if (!string.IsNullOrEmpty(path))
{
string[] pathArray = path.Split(',');
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < pathArray.Length; i++)
{
data = new Attachment(pathArray[i], MediaTypeNames.Application.Octet);//实例化附件
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(pathArray[i]);//获取附件的创建日期
disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathArray[i]);//获取附件的修改日期
disposition.ReadDate = System.IO.File.GetLastAccessTime(pathArray[i]);//获取附件的读取日期 objMailMessage.Attachments.Add(data);//添加到附件中
}
}
try
{
SmtpMail.Send(objMailMessage);
isSend = true; }
catch (Exception e)
{
return isSend;
} return isSend;
}
}
提交后..打印出来的result为false..为什么? 如果不带附件path为空的时候..能正常发送..
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货