如何使用Gmail/Yahoo的SMTP



我想知道如何从我的电脑发送emai我没有任何服务器来测试我的代码,我想可能是我可以使用google/yahoo smtp,但我不知道如何使用它们。我想知道如果我使用google/yahoo smtp,那么我可以向每个人发送电子邮件?我不知道我到底应该放什么sc.Credentials=新的NetworkCredential(用户名、密码);我把我在web.config文件中定义的管理员用户名和密码放在凭据标记中这是我的代码:

 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();
    }
}

这就是我使用的,它对我有效,把它放在你的web配置中:

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

我可以通过代码从你的电脑上从Yahoo/Gmail发送电子邮件吗?

我不知道我应该在sc.Credentials=new NetworkCredential(用户名,密码)中放什么;

您必须使用您的GMail/Yahoo用户名和密码。通过GMail发送时,您必须启用SSL

请查看相关问题以了解更多代码和解释。

最新更新