无法从ASP.NET发送电子邮件

时间:2010-10-21 13:33:22

标签: c# asp.net email smtp

我有代码在我们的网站上注册时向用户发送电子邮件我做了代码但没有发送消息并且出现错误(发送电子邮件失败)。请尽快帮助我。

bool SendMail(string account, string password, string to, string subject, string message)
{
    try
    {
        NetworkCredential loginInfo = new NetworkCredential(elarabyAccount, password);
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(account);
        msg.To.Add(new MailAddress(to));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;
        SmtpClient client = new SmtpClient("mail.example.com", 8080);
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = loginInfo;
        client.Send(msg);

        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

void BtnSend_Click(object sender, EventArgs e)
{
    SendMail("noreply@example.com", "xxxxx", TxtEmail.Text, "Hi", "Hi");
}

2 个答案:

答案 0 :(得分:2)

您将端口设置为8080.尝试25

答案 1 :(得分:1)

我怀疑它只是简单到了端口号。但是,为了在将来尝试找到这样的问题,您应该尝试查看从Try-Catch语句中获得的错误消息

try
{
   //code in here
}
catch (System.Net.Mail.SmtpException exsmtp)
{
    throw new Exception(exsmtp.ToString());
} 
catch (Exception ex)
{
   throw new Exception(ex.ToString());
}

您将从中获得大量使用(更少)的信息:)