0

Please can anyone help here: I have this asp.net form am trying to send as email and here is the code:

try 
{
    MailMessage mailSend = new MailMessage();
    mailSend.From = new MailAddress("williams@iquo.com.ng");
    mailSend.To.Add("williams@iquo.com.ng");
    mailSend.Subject = "Contact message from www.iquo.com.ng";

    mailSend.Body = "<b>My name is: </b>" + firstName.Text + lastName.Text + "<br/>" + "<b>My phone number is: </b>" + pNumber.Text + "<br/>"
    + "<b>My email address is: </b>" + email.Text + "<br/>" + "<b>The message is: </b>" + message.Text;
    mailSend.IsBodyHtml = true;

    SmtpClient smtpClient = new SmtpClient("smtp.iquo.com.ng", 25);                
    smtpClient.Credentials = new System.Net.NetworkCredential("williams@iquo.com.ng", "password");
    smtpClient.Send(mailSend);

    firstName.Enabled = false; 
    lastName.Enabled = false;
    pNumber.Enabled = false;
    email.Enabled = false;
    message.Enabled = false;
    Response.Redirect("about-me.html");
}
catch (Exception ex)
{
    throw new Exception(ex.ToString());
}

Is there anything wrong and why will the mail not sent? Also, I place this:

<system.net>
    <mailSettings>
    <smtp>
  <network host="smtp.iquo.com.ng" userName="williams@iquo.com.ng"    password="password />
  </smtp>
  </mailSettings>
</system.net>

into the the web.config file as instructed by server admin. What can I do?

Abhitalks
  • 27,721
  • 5
  • 58
  • 81
Xsamms
  • 1
  • 3
  • 2
    Do your SMTP server work correctly? You should use public SMTP server like gmail, hotmail... to test your code first, then you try again with your server. – tdat00 Jan 11 '16 at 04:36
  • 1
    is there any exception occured? have you checked that? also check your smtp configuration. – Pedram Jan 11 '16 at 04:44
  • If you are working inside a corporate firewall, please check if port 25 is open or not – Satyaki Chatterjee Jan 11 '16 at 05:18
  • If you're running your code behind a proxy, you should add your proxy to your web.config inside . See here for details http://stackoverflow.com/questions/3311746/how-to-pass-credentials-in-defaultproxy-config-setting – Scotty Jan 11 '16 at 05:22
  • there was missing quote on your password. `password="password />` – rjps12 Jan 11 '16 at 06:08

1 Answers1

-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

using System.Net.Mail;

namespace asp_Semester_Project
{
    public partial class ContactUs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.From = new MailAddress("ishahanbutt789@gmail.com");
                    mailMessage.To.Add("ishahanbutt789@gmail.com");
                    mailMessage.Subject = txtSubject.Text;
                    mailMessage.Body = "<b>Sender Name : </b>" + txtName.Text + "<br/>"
                        + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>"
                        + "<b>Comments : </b>" + txtComments.Text;
                    mailMessage.IsBodyHtml = true;


                    SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.EnableSsl = true;
                NetworkCredential nc = new NetworkCredential();
                nc.UserName = "ishahanbutt789@gmail.com";
                nc.Password = "******";
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = nc;
                smtpClient.Port = 587;
                    smtpClient.Send(mailMessage);
                    lblMessage.ForeColor = System.Drawing.Color.Blue;
                    lblMessage.Text = "Thank you for contacting us";

                    //txtName.Enabled = false;
                    //txtEmail.Enabled = false;
                    //txtComments.Enabled = false;
                    //txtSubject.Enabled = false;
                    //Button1.Enabled = false;
            }
            catch (Exception ex)
            {
                // Log the exception information to 
                // database table or event viewer
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = ex.StackTrace;
            }
        }
    }
}