无法从localhost发送电子邮件

时间:2011-05-27 05:25:22

标签: asp.net

我正在尝试使用以下asp.net代码从localhost发送邮件..但电子邮件发送失败..

为什么会这样/ ???

protected void Button1_Click(object sender, EventArgs e)

{

    MailMessage mail = new MailMessage();

    string name = TextBox1.Text.ToString();
    string contact = TextBox2.Text.ToString();
    mail.To.Add("email@gmail.com");

    mail.From = new MailAddress(name.Trim());


    mail.Subject = "In line image test";

    mail.Body = "Hello email...." +name.Trim() +", "+contact.Trim();

    mail.IsBodyHtml = true;

    SmtpClient client = new SmtpClient("127.0.0.1");
    client.UseDefaultCredentials = false;
    client.EnableSsl = true; 
    client.Host = "127.0.0.1";
    client.Port = 25;

    NetworkCredential credentials = new NetworkCredential("email@gmail.com", "abc");

    client.Credentials = credentials;

    try
    {

       client.Send(mail);
    }
    catch
    {

        Button1.Text = "Fail";

    }
}

提前致谢..

3 个答案:

答案 0 :(得分:2)

试试这个

client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

client.Send(mail);

答案 1 :(得分:0)

首先在本地系统中设置SMTP服务器,然后点击此链接 http://www.arialsoftware.com/SetupSMTPServer.htm

然后你试试你的代码,如果你看到问题尝试整理,有人可以帮助你。

答案 2 :(得分:0)

请在Web.Config档案

中添加以下详细信息
<system.net>
    <mailSettings>
      <smtp from="FROM EMAIL ADDRESS">
        <network host="SERVER IP" userName="USER NAME" password="PASSWORD" port="25"/>
      </smtp>
    </mailSettings>
  </system.net>

希望它对你有所帮助!