无法使用c#发送电子邮件

时间:2015-03-13 18:07:36

标签: c# asp.net

我正试图在我的网站上注册一些身体时发送自动电子邮件。每当我在网站(本地服务器)上注册时,都没有例外。我还添加了使用system.Net.Mail和web.Config脚本a。 以下是我的ASP.NET代码:

 public void SendWelcomeMail()
    {
        string EmailTO = TextBoxEmail.Text;
        string EmailFrom = "exampleEmail@gmail.com";
        string Pass = TextBoxPassword.Text;
        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(EmailFrom);

            message.To.Add(new MailAddress(EmailTO));

            message.Subject = "Welcome";
            message.Body = "You are now registered with your email ID "+EmailTO;
            message.Body = "Your Password is "+Pass;
            SmtpClient client = new SmtpClient();
            client.Send(message);
        }
        catch
        {
            Response.Write("Sorry there is an exception. Some thing must be wrong");
        }
}

1 个答案:

答案 0 :(得分:2)

您需要SMTP主机才能发送电子邮件。您已创建客户端,但客户端无法在没有主机的情况下发送电子邮件。您可以将主机作为参数传递给构造函数,或将其分配给SmtpClient类上的Host属性

https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.host(v=vs.110).aspx