0

I want to know how can I send emai from my pc I dont have any server to test my code and I think may be I can use google/yahoo smtp but I dont know how to use them.and I want to know if I use google/yahoo smtp then I can send email to every one? and I dont know what exactly should I put in sc.Credentials = new NetworkCredential(username,password); I put my admin username and password which I defined it in my web.config file in credential tag this is my code:

 protected void SendMail(string ma,DataRow dataRow)
{
    try
    {
        string from = "myEmail@yahoo.com";
        string subject = "forgotten password";
        string body = "name: " + dataRow["user_name"].ToString() + "<br/>" + "familly : " + dataRow["user_familly"].ToString() + "<br/>" + "password : " + dataRow["user_password"].ToString();
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, ma, subject, body);
        mail.IsBodyHtml = false;

        System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();

        sc.Port = 587;
        sc.Host = "smtp.mail.yahoo.com";// "smtp.gmail.com";


        sc.EnableSsl = false;
        sc.Send(mail);

        Label1.ForeColor = System.Drawing.Color.Blue;
        Label1.Text = "your password sent";
    }
    catch (Exception e)
    {

        Label1.Text = e.ToString();
    }

}
adt
  • 4,320
  • 5
  • 35
  • 54
  • 2
    http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – adt Feb 11 '13 at 09:21

2 Answers2

0

This is what I use and it works for me, Put this in your web config:

<system.net>
<mailSettings>
  <smtp from="FromEmailAddress">
    <network host="smtp.gmail.com" password="YourPassword" port="587" enableSsl="true" 
      userName="YourEmailAddress" />
  </smtp>
</mailSettings>

Liam Kenneth
  • 982
  • 1
  • 11
  • 21
  • I want to have a form in my contact us page,then how I should change my code to have both together I mean Admin can send email to users and user can send email to admin – Elmira Golshan Feb 11 '13 at 10:47
-1

Can I send Email from Yahoo/Gmail from your PC through code?

Yes

I dont know what exactly should I put in sc.Credentials = new NetworkCredential(username,password);

You have to use your GMail/Yahoo username and password. You will have to enable SSL while sending through GMail.

Please check related questions for more code and explanation.

Shoban
  • 22,920
  • 8
  • 63
  • 107