Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
mail is going well with the first attachment but cannot carry the second attachment.

C#
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;
}
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 10:33am    
It has nothing to do with ASP.NET. Note that you are blocking exception propagation, which is a big no-no.
—SA

1 solution

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;

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

string annexFileName = string.Concat(Path.GetDirectoryName(fileName), Path.DirectorySeparatorChar.ToString(), "Annex_", Path.GetFileName(fileName));
 
Share this answer
 
Comments
manish.communityhub 11-Jan-16 5:41am    
No the file exists and having the correct directory it adds the attachment but
the second file is not attached in mail though the file size is 1 kb

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900