Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
-----------------------
VB
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 :
C#
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
Updated 18-Dec-12 23:46pm
v3

You may want to update your code to the following;

C#
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
 
Share this answer
 
v2
Comments
ramjiricky 19-Dec-12 6:40am    
thanks for your reply i got the answer now
Orcun Iyigun 19-Dec-12 8:47am    
You are welcome.
AshishChaudha 19-Dec-12 6:44am    
my +5!
Orcun Iyigun 19-Dec-12 8:47am    
Thanks.
Use Server.MapPath in place of Path.GetFullPath(). May be it will help you.

Thanks
 
Share this answer
 

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



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