-7

I have shared my code below which throws errors while sending mail.

var fromAddress = new MailAddress("sender@gmail.com", "Customer");
        var toAddress = new MailAddress("reciever@gmail.com", "HR");
        const string fromPassword = "senderpass";
        const string subject = "Subject";
        const string body = "Body";

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential(fromAddress.Address, fromPassword);

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }

this is the updated code but this also not working

1 Answers1

0

Try to configure your host from the Web-config as below:

<system.net>
    <mailSettings>
      <smtp from="mail####hr@gmail.com">
        <network host="smtp.gmail.com" port="587" userName="mail####hr@gmail.com" password="##########"/>
      </smtp>
    </mailSettings>
  </system.net>

In your class file change the method as follows and let me know:

   string strMailBody ="test";
   string strToEmailId ="da###dotnet@gmail.com";
   string strSubject = "Mail Testing";
   SmtpClient Client = new SmtpClient();
   Client.EnableSsl = true;
   System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.To.Add("mail####hr@gmail.com");
            message.Bcc.Add(strToEmailId);// (ToEmailId);
            message.Subject = strSubject;// "Subject";
            message.IsBodyHtml = true;
            message.Body = strMailBody;//Content;
            try
            {
                Client.Send(message);
            }
            catch (Exception ex)
            {

            }
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48