i think you need to use virtual path instead of real path. ATM i think fileName as real path. use result of ServerPath + "\" + fuUploadedFiles.UploadedFiles[i].FileName inplace of fileName. This should solve the issue
Wrong path in mail attachment
Domanda
Here i need when file uploaded then i need that file attached and mail to it.But attachment file path wrong.
Error : Could not find file 'C:\Program Files (x86)\IIS Express\test.xlsx'. //<--Wrong file path
Code
for (int i = 0; i < fuUploadedFiles.UploadedFiles.Count; i++)
{
if (fuUploadedFiles.UploadedFiles[i] != null)
{
fuUploadedFiles.UploadedFiles[i].SaveAs(ServerPath + "\\" + fuUploadedFiles.UploadedFiles[i].FileName);
using (MailMessage mm = new MailMessage())
{
if (fuUploadedFiles.UploadedFiles.Count != null)
{
string FileName = fuUploadedFiles.UploadedFiles[i].FileName;
mm.Attachments.Add(new Attachment(FileName));//<-- Error generate here
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("myemail@email.com", "mypassword");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow