Click here to Skip to main content
11,797,355 members (75,618 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET
i got the error like this while attach the file to send a mail but without attachment the message has been send (if i hide attach comment)
the error like this:
System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\free-photo-background-952-m.jpg'. File name: 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\free-photo-background-952-m.jpg' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) at System.Net.Mail.AttachmentBase..ctor(String fileName) at System.Net.Mail.Attachment..ctor(String fileName) at customerdetails.SendEmail(String toAddress, String ccAddress, String bccAddress, String subject, String body, MailPriority priority, Boolean isHtml) in e:\E-mobileshop\admin\customerdetails.aspx.cs:line 275
and i use this code :
protected void SendEmailWithAttachment(object sender, EventArgs e)
        SendEmail(txtTo.Text.Trim(), "", "", txtSubject.Text.Trim(), txtBody.Text.Trim(),System.Net.Mail.MailPriority.High, false);
    private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body,  System.Net.Mail.MailPriority priority, bool isHtml)
        body = txtBody.Text.ToString();
        subject = txtSubject.Text.ToString();
        toAddress = txtTo.Text.ToString();
            SmtpClient smtpClient = new SmtpClient();
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                    MailAddress fromAddress = new MailAddress("", "ramji.kid, gmail.Com");
                    // You can specify the host name or ipaddress of your server
                    smtpClient.Host = ""; //you can also specify mail server IP address here
                    smtpClient.EnableSsl = true;
                    //Default port will be 25
                    smtpClient.Port = 25;
                    NetworkCredential info = new NetworkCredential("", "hi");
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpClient.UseDefaultCredentials = false;
                    smtpClient.Credentials = info;
                    //From address will be given as a MailAddress Object
                    message.From = fromAddress;
                    message.Priority = priority;
                    // To address collection of MailAddress
                    message.Subject = subject;
                    if (ccAddress.Length > 0)
                    if (bccAddress.Length > 0)
                    //Body can be Html or text format
                    //Specify true if it is html message
                    message.IsBodyHtml = isHtml;
                    // Message body content
                    message.Body = body;
                     //Add the attachment, if any
                   if (FileUpload1.PostedFile.ContentLength > 0)
                        Attachment attachment = new Attachment(Path.GetFullPath(FileUpload1.PostedFile.FileName));
                    // Send SMTP mail
                    lblMessage.Text = "Email sent to " + toAddress + " successfully !";
        catch (Exception ee)
            lblMessage.Text = ee.ToString();
Posted 18-Dec-12 23:36pm
Edited 18-Dec-12 23:46pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You may want to update your code to the following;

string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
  Attachment myAttachment =
                 new Attachment(FileUpload1.FileContent, fileName

Also use a debugger to see if the file content and path is not null.

Good luck,
ramjiricky at 19-Dec-12 6:40am
thanks for your reply i got the answer now
Orcun Iyigun at 19-Dec-12 8:47am
You are welcome.
AshishChaudha at 19-Dec-12 6:44am
my +5!
Orcun Iyigun at 19-Dec-12 8:47am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use Server.MapPath in place of Path.GetFullPath(). May be it will help you.


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

  Print Answers RSS
0 CPallini 455
1 OriginalGriff 410
2 Maciej Los 395
3 Abhinav S 359
4 KrunalRohit 266
0 OriginalGriff 2,032
1 Maciej Los 1,725
2 KrunalRohit 1,310
3 CPallini 1,055
4 ppolymorphe 883

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100