Click here to Skip to main content
15,891,253 members

i got the error like this while attach the file to send a mail

ramjiricky asked:

Open original thread
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();
        }
    }
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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