Hi everyone. I have a few question to ask regarding email notification in c#.

I have the following codes:

SmtpClient smtpClient = new SmtpClient();
             MailMessage message = new MailMessage();

             message.To.Add("alex@hotmail.com");
             message.To.Add("alex2@gmail.com");
             message.Subject = "Test";
             message.IsBodyHtml = true;
             message.Body = "<html><b>Hello World!</b></html>";

             smtpClient.Send(message);

While in my web.config I do have such codes.

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="testing@gmail.com">
        <network host="smtp.gmail.com" port="587" enableSsl ="true" defaultCredentials ="false" password ="123" userName ="testing@gmail.com"></network>
      </smtp>
    </mailSettings>
  </system.net>

But still I cant get any email in my inbox. Is there anything else to configure or I
miss it out?

Thanks in advance!

Recommended Answers

All 4 Replies

Try incorporating what is in the web.config into the code itself. This worked for me.

MailMessage message = new MailMessage();
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");

            message.From = new MailAddress("testing@gmail.com");
            message.To.Add("alex@hotmail.com");
            message.To.Add("alex2@hotmail.com");
            message.Subject = "Test Mail";
            message.Body = "This is for testing SMTP mail from GMAIL";

            smtpClient.Port = 587;
            smtpClient.Credentials = new System.Net.NetworkCredential("Address", "Password");
            smtpClient.EnableSsl = true;

            smtpClient.Send(message);

smtpClient.Credentials = new System.Net.NetworkCredential("Address", "Password");

Sorry this line of code
address and password put my email address?

Your gmail address and password, yes.

Thanks buddy. It works fine...

Thanks in advance.

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.