Click here to Skip to main content
Rate this: bad
good
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();
        try
      {
            
            SmtpClient smtpClient = new SmtpClient();
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
               
                    MailAddress fromAddress = new MailAddress("ramji.kid@gmail.com", "ramji.kid, gmail.Com");
 
                    // You can specify the host name or ipaddress of your server
                    smtpClient.Host = "smtp.gmail.com"; //you can also specify mail server IP address here
                    smtpClient.EnableSsl = true;
                    //Default port will be 25
                    smtpClient.Port = 25;
 
                    NetworkCredential info = new NetworkCredential("ramji.kid@gmail.com", "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.To.Add(toAddress);
                    message.Subject = subject;
                    if (ccAddress.Length > 0)
                    {
                        message.CC.Add(ccAddress);
                    }
                    if (bccAddress.Length > 0)
                    {
                        message.Bcc.Add(bccAddress);
                    }
 
                    //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));
                        message.Attachments.Add(attachment);
                    }
 
                    // Send SMTP mail
                    smtpClient.Send(message);
 
                    lblMessage.Text = "Email sent to " + toAddress + " successfully !";
               
            
        }
        catch (Exception ee)
        {
            lblMessage.Text = ee.ToString();
        }
    }
Posted 19-Dec-12 0:36am
Edited 19-Dec-12 0:46am
v3
Rate this: bad
good
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,
OI
  Permalink  
v2
Comments
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
   
Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
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