Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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 2: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

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
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900