Click here to Skip to main content
14,384,408 members
Rate this:
Please Sign up or sign in to vote.
See more:
I went through your example of "sending mail with asp.net with c#"..
Debugging it through visual studio I'm not getting any error. Mail is sending OK, but after uploading this to our online website I am getting an error. Can you give me idea for what to do?

I have written this code in my .cs file:
protected void submit_Click(object sender, EventArgs e)
    {
        String body = "<html><body>";

        // (MRB Edit:)
        // Deleted lots of these: body += "";
        // as they weren't doing anything useful

        body += " Name of Organization : " + name_organization.Text.Trim() + "Address: " + address.Text.Trim() + "Tel. No.:" + tele_no.Text.Trim() + "Fax : " + fax_no.Text.Trim() + " Mobile : " + mobile_no.Text.Trim() + "Company Email :" + company_email.Text.Trim() + "Contact Person : " + contact_person.Text.Trim() + "Accident Description :" + other_information0.Text.Trim() + "Contact Person : " + level_participants.Text.Trim() + "Picture of the Exact Location :" + object_training.Text.Trim() + "Any other information :" + other_information.Text.Trim() + "";
        body += "</body></html>";
            
            string attach3 = null;
            string strFileName = null;

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("XXXXX.XXXX@ask-ehs.com");
            mail.To.Add("XXXXX.XXXX@ask-ehs.com");
            mail.Subject = name_organization.Text;
            mail.Body = body;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            
            if (fileImage.PostedFile != null)
            {
                HttpPostedFile attFile = fileImage.PostedFile;
                int attachFileLength = attFile.ContentLength;
                if (attachFileLength > 0)
                {
                    strFileName = Path.GetFileName(fileImage.PostedFile.FileName);
                    fileImage.PostedFile.SaveAs(Server.MapPath(strFileName));
                    Attachment attach = new Attachment(Server.MapPath(strFileName));
                    mail.Attachments.Add(attach);
                    attach3 = strFileName;
                }
            }

            SmtpClient smtp = new SmtpClient("127.0.0.1");
            smtp.Send(mail);
            
            
            clear();

            Response.Redirect("enquiry_accident_scenario_thanks.html");

    }

Can anybody spot something suspicious in my code?

Thanks for your consideration!
Posted
Updated 30-Sep-11 3:19am
v3
Comments
Manfred Rudolf Bihy 30-Sep-11 8:20am
   
Edit: Code block added. Code tidyed up. Removed email receipient/sender adress.
pal5hah 30-Sep-11 23:39pm
   
then what should i write to run on my server..can you write the sample code pls???i have to change in my web.config file also???please explain in detail..thank you for your reply..

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If it works locally, but not on your website it should be obvious what's wrong. new SmtpClient("127.0.0.1"); is connecting to the SMTP server on the system where the code is running (127.0.0.1 == loopback IP). If there is no SMPT server on your webserver the program can't connect to it. You can easily check this by logging into your web server an issueing this command in a console window telnet 127.0.0.1 25. For reference purposes you can also try this on your local system where your code worked as you told us. On your local system you telnet should be able to connect to the SMTP server and on your webserver the command should fail.

Leave a comment if you're still having doubts.

—MRB
   
Comments
pal5hah 30-Sep-11 23:39pm
   
then what should i write to run on my server..can you write the sample code pls???i have to change in my web.config file also???please explain in detail..thank you for your reply..

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




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