Guys I am stuck with a problem and want your genuine suggestion..
Actually the problem is that i am trying to send a mail using c#.net..
There is no issues while sending mail but when i try to include an attachment with it is not taking the path of the concerned directory.. But when i am attaching the file from the root directory here i have my codes it is working absolutely well..
Plz look at the code below and give some suggestion to rectify the problem..

protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();

                mail.To.Add(txtTo.Text);
                mail.From = new MailAddress("orangesoftech2012@gmail.com");

                mail.Subject = txtSubject.Text;
                mail.Body = txtBody.Text;
                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment(FileUpload1.FileName);
                mail.Attachments.Add(attachment);

                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address

                smtp.Credentials = new System.Net.NetworkCredential
                ("orangesoftech2012@gmail.com", "orangeorange");
                //Or your Smtp Email ID and Password
                smtp.EnableSsl = true;
                smtp.Send(mail);
                Response.Write("<script language='javascript'>alert('MAIL SENT');</script>");
                Response.Write("<script language='javascript'>alert('Thank You for using VishalMail');</script>");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message;

            }
        }

Needless to say i am using demo gmail id over here so better try with your own id while debugging changing the webconfig as well...

Guys i solved this problem.. The code is:

protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                
                mail.To.Add(txtTo.Text);
                mail.From = new MailAddress("orangesoftech2012@gmail.com");

                mail.Subject = txtSubject.Text;
                mail.Body = txtBody.Text;
                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream,FileUpload1.FileName);
                mail.Attachments.Add(attachment);

                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address

                smtp.Credentials = new System.Net.NetworkCredential
                ("orangesoftech2012@gmail.com", "********");
                //Or your Smtp Email ID and Password
                smtp.EnableSsl = true;
                smtp.Send(mail);
                Response.Write("<script language='javascript'>alert('MAIL SENT');</script>");
                Response.Write("<script language='javascript'>alert('Thank You for using VishalMail');</script>");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message;

            }
        }

hii..

Even i did the same but then it didnt work for me ... it was working good in my previous project :-( ...couldnt find where i have gone wrong now...

Then post the code for your project... Lets see what is the problem....

hi..this is my code ....did the same

try
        {
            MailMessage mail = new MailMessage();

            mail.To.Add(txtto.text);
            mail.From = new MailAddress("username");

            mail.Subject = "well";
            mail.Body = "hii";
            // mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address

            smtp.Credentials = new System.Net.NetworkCredential
            ("username", "password");
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

Have you changed the configuration in web.config??

1> IF not then configure the settings for system.net... mention the network credentials over dere alongwith the port number....

2> Change the IMAP/POP settings in your userid...

3> If still getting error paste the error msg with error img...

hii..

I forgot to update my username in webconfig file ...thank you soo much for your prompt reply...got fixed it ..:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.