0

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!

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Stefan_Lam88
0

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);
0

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

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.