asp.netemailiissmtpconfirmation-email

how to send a confirmation email in asp.net, cannot get IIS pickup directory error


I want to send a confirmation email to user in registration page. The following code is the related part:

try
{
    SmtpClient sc = new SmtpClient();
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    string Ema = u.UserMail.ToString();
    MailAddress gonderen = new MailAddress("admin@gmail.com", "Hello");
    sc.Host = "smtp.gmail.com";
    sc.Port = 587;
    mail.To.Add(Ema);
    mail.Subject = "Confirmation Message";
    mail.From = gonderen;
    mail.IsBodyHtml = true;
    mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
    mail.Body = "<html><body>";
    sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    sc.Send(mail);
    MESSAGE(true, "Sending mail is successful");
}
catch (Exception)
{
    MESSAGE(true, "Sending mail is unsuccessful!");
}

But it does not send an email to related user. I have looked at forums and I added to web.config the following part:

  <system.net>
    <mailSettings>
      <smtp from="myaddress@gmail.com ">
        <network host="smtp.gmail.com" defaultCredentials="false"
      port="587" userName ="myaddress@gmail.com" password="password" />
      </smtp>
    </mailSettings>
  </system.net>

But anything didn't change. Then i have debugged and it enters into the try statement and when it comes to sc.Send(mail);, it drops to catch. Where is my mistake?

Additionally, during debug i realized that it shows this error: cannot get IIS pickup directory. I have controlled whether I have a smtp service or not from services but i couldn' t see this service. Is this error related to that service?

Thanks in advance.


Solution

  • Thanks for your helps. I have solved this problem. I have changed my code as the following one:

    try
    { 
        SmtpClient sc = new SmtpClient();
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        string Ema = u.UserMail.ToString();
        MailAddress gonderen = new MailAddress("admin@gmail.com", "Hello");
        sc.Host = "smtp.gmail.com";
        sc.Port = 587;
        sc.EnableSsl = true;
        mail.To.Add(Ema);
        mail.Subject = "Confirmation Message";
        mail.From = gonderen;
        mail.IsBodyHtml = true;
        mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
        mail.Body = "<html><body>";
        mail.Body += "</body></html>";
        sc.DeliveryMethod = SmtpDeliveryMethod.Network;
        sc.Send(mail);
        MESAJ(true, "Sending mail is successful");
    }
    catch (Exception)
    {
        MESAJ(true, "Sending mail is unsuccessful!");
    }
    

    and then i have set my web.config like the following :

    <mailSettings>
      <smtp deliveryMethod="Network"
                  from="admin@gmail.com">
        <network defaultCredentials="false" host="smtp.gmail.com" port="587"
                  userName="admin@gmail.com"  password="password" />
      </smtp>
    </mailSettings>
    

    that works.. :)