0

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.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
rockenpeace
  • 1,011
  • 5
  • 18
  • 29
  • which email service you using? shouldn't it comes under host? – huMpty duMpty Jul 20 '12 at 12:07
  • while it is in debug, i realized that host equals to "gmail.com". do you mean that ? – rockenpeace Jul 20 '12 at 12:10
  • are you using gmail server to send emails?? – huMpty duMpty Jul 20 '12 at 12:10
  • i don't know that i use which email service. i just want to send an email from a gmail adress to another gmail adress. can you show me how to show email service in the code ? – rockenpeace Jul 20 '12 at 12:15
  • I have already answer that question here **http://stackoverflow.com/questions/7982810/how-to-use-gmail-smtp-in-asp-net-form/7982891#7982891** Hope this helps – huMpty duMpty Jul 20 '12 at 12:16
  • i will try your code but in here sc.Port = 25; but your code shows port="587". are these different things ? should i remove sc.port part ? – rockenpeace Jul 20 '12 at 12:21
  • i have tried this but it didn't work.. – rockenpeace Jul 20 '12 at 12:35
  • Check your code against this : **http://stackoverflow.com/questions/8517258/email-alert-when-page-is-open/8517666#8517666** – huMpty duMpty Jul 20 '12 at 13:15
  • `1.` Don't send emails from localhost unless it's an internal email. If it's an external email your chances of your email not being caught as spam are very slim. `2.` You specify the delivery method as `SmtpDeliveryMethod.PickupDirectoryFromIis;` therefore, a file containing the email will simply be placed in `c:\inetput\mailroot\`. `3.` *Perhaps*, I am not sure, the reason is going to the Catch block is you specified `PickupDirectoryFromIis` as delivery method but you are also defining a host and port, as if you were going to send it via smtp server. Make sure that both things are compatible. – Icarus Jul 20 '12 at 13:17
  • http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx some documentation for you to look at. – Icarus Jul 20 '12 at 13:21
  • while i make debug, it gives this error: cannot get IIS pickup directory. – rockenpeace Jul 20 '12 at 13:51
  • i could not find inetput folder in my computer. i suppose i don' t have any smtp service. and i have tried as "sc.DeliveryMethod = SmtpDeliveryMethod.Network;" but the error is still same. – rockenpeace Jul 20 '12 at 14:19

2 Answers2

0

Why not use GMAIL as the SMTP server, since you are defining a GMAIL account, by defining the host as "smtp.gmail.com", and port at 587?

Specific details here: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • i set as sc.host="smtp.gmail.com" and sc.port=587 but it didn't change and i have realized while doing debug. when it drops to catch, it gives this error:cannot get IIS pickup directory – rockenpeace Jul 20 '12 at 13:50
  • Remove this line: sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; and try again. – Brian Mains Jul 20 '12 at 14:01
  • i have removed this part and tried but it gives this error for this time: SmtpException was caught : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. o2sm15359902wiz.11 – rockenpeace Jul 20 '12 at 14:17
  • On the SmtpClient, set EnableSsl = true; via http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx – Brian Mains Jul 20 '12 at 14:42
  • i have added sc.EnableSsl = true; and removed sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; but there is an error: SmtpException was caught : SSL must not be enabled for pickup-directory delivery methods. i – rockenpeace Jul 20 '12 at 14:51
  • OK, I thought that may be it, but remove the EnableSsl then... you need to supply the credentials of your gmail user ID/password, using the credentials property as in http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials.aspx. Note, you can't pass the default credentials here; you need to instantiate the credentials object and pass in the gmail ID/password. – Brian Mains Jul 20 '12 at 15:03
0

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.. :)

rockenpeace
  • 1,011
  • 5
  • 18
  • 29