电子邮件的多个附件在asp.net中不起作用 [英] Multiple attachments for email is not working in asp.net

查看:84
本文介绍了电子邮件的多个附件在asp.net中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个附件的邮件进展顺利,但不能携带第二个附件。



这里我的代码,









public Boolean SendMail_EmbededImage(string mail,string mailSubject,string MailBody,bool p,string fileName)

{

string SmtpClient = ConfigurationManager.AppSettings [SmtpClient]。ToString( ); $

string MailFrom = ConfigurationManager.AppSettings [MailFrom]。ToString();

string NetCredUserName = ConfigurationManager.AppSettings [NetCredUserName]。ToString();

string NetCredPassword = ConfigurationManager.AppSettings [NetCredPassword]。ToString();

string PdfPath = ConfigurationManager.AppSettings [CopyOfPDF]。ToString();

int SMTPPort = Convert.ToInt32(ConfigurationManager.AppSettings [SMTPPort]。ToString());



try

{



MailMessage mail1 = new MailMessage();

SmtpClient SmtpServer = new SmtpClient(SmtpClient);



mail1.From =新邮件地址(MailFrom);

mail1.To.Add(mail);

mail1.Subject = mailSubject;

mail1.IsBodyHtml = true;

mail1.Attachments.Add(新附件(fileName) );

if(File.Exists(Annex_+ fileName))

mail1.Attachments.Add(new Attachment(Annex_) + fileName));


mail1.Body = MailBody;

SmtpServer.Port = SMTPPort;

SmtpServer.Credentials = new System.Net.NetworkCredential(NetCredUserName,NetCredPassword);

//SmtpServer.EnableSsl = true;



ServicePointManager.ServerCertificateValidationCallback = delegate(object s,

System.Security.Cryptography.X509Certificates.X509证书,

System.Security.Cryptography.X509Certi ficates.X509Chain chain,

SslPolicyErrors sslPolicyErrors)

{

返回true;

};



SmtpServer.Send(mail1);

p = true;

}

catch(异常) ex)

{

p = false;

}

返回p;

}

解决方案

File.Exists不太可能评估为true,因为你没有指定查找文件的目录。如果fileName包含完整路径,包括驱动器和文件夹,然后在前面添加Annex_使您的代码查找



Annex_c:\ myfolder \ myfile.txt



即将失败。如果您只想将Annex_添加到文件名而不是整个字符串,请使用类似的代码;



  string  fileName =  @  c:\ myfolder\myfile.txt; 

string annexFileName = string .Concat(Path.GetDirectoryName(fileName), Path.DirectorySeparatorChar.ToString(), Annex _,Path.GetFileName(fileName));


mail is going well with the first attachment but cannot carry the second attachment.

Here is my code,





public Boolean SendMail_EmbededImage(string mail, string mailSubject, string MailBody, bool p, string fileName)
{
string SmtpClient = ConfigurationManager.AppSettings["SmtpClient"].ToString();
string MailFrom = ConfigurationManager.AppSettings["MailFrom"].ToString();
string NetCredUserName = ConfigurationManager.AppSettings["NetCredUserName"].ToString();
string NetCredPassword = ConfigurationManager.AppSettings["NetCredPassword"].ToString();
string PdfPath = ConfigurationManager.AppSettings["CopyOfPDF"].ToString();
int SMTPPort = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"].ToString());

try
{

MailMessage mail1 = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(SmtpClient);

mail1.From = new MailAddress(MailFrom);
mail1.To.Add(mail);
mail1.Subject = mailSubject;
mail1.IsBodyHtml = true;
mail1.Attachments.Add(new Attachment(fileName));
if (File.Exists("Annex_" + fileName))
mail1.Attachments.Add(new Attachment("Annex_" + fileName));

mail1.Body = MailBody;
SmtpServer.Port = SMTPPort;
SmtpServer.Credentials = new System.Net.NetworkCredential(NetCredUserName, NetCredPassword);
//SmtpServer.EnableSsl = true;

ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
};

SmtpServer.Send(mail1);
p = true;
}
catch (Exception ex)
{
p = false;
}
return p;
}

解决方案

It's unlikely the File.Exists is going to evaluate true as you're not specifying the directory to look for the file in. If fileName contains the full path, including drive and folder, then adding "Annex_" to the front is making your code look for

Annex_c:\myfolder\myfile.txt

which is going to fail. If you only want to add the Annex_ to the filename and not the whole string use code like;

string fileName = @"c:\myfolder\myfile.txt";

string annexFileName = string.Concat(Path.GetDirectoryName(fileName), Path.DirectorySeparatorChar.ToString(), "Annex_", Path.GetFileName(fileName));


这篇关于电子邮件的多个附件在asp.net中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆