如何从asp.net发送邮件?

时间:2016-03-04 20:15:32

标签: asp.net

        mailMessage.From = new System.Net.Mail.MailAddress("abc@yourserver.com");
        mailMessage.Subject =TextBox3.Text;
        mailMessage.Body = TextBox5.Text;
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(new System.Net.Mail.MailAddress(TextBox4.Text));
        mailMessage.CC.Add(new System.Net.Mail.MailAddress("xyz@gmail.com"));
        System.Net.Mail.SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtpout.secureserver.net";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = "abc@yourserver.com";
        NetworkCred.Password = "1234566";
       smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.Send(mailMessage);

错误

连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应173.201.193.228:587

1 个答案:

答案 0 :(得分:0)

以下是ASP.NET身份消息的发送方式的简要说明,但您可以将其用于任何目的。它使用外部SMTP服务器进行操作(在这种情况下为gmail)。

 public Task SendAsync(IdentityMessage message)
    {
        try
        {
            // Plug in your email service here to send an email.
            using(MailMessage mail = new MailMessage())
            {
                mail.To.Add(message.Destination);

                mail.From = new MailAddress(WebConfigurationManager.AppSettings["accountsServerEmail"]);
                mail.Subject = "Mobiler-registration";

                mail.Body = message.Body;
                mail.IsBodyHtml = true;
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host = WebConfigurationManager.AppSettings["smtpServerPath"];
                    smtp.Port = Convert.ToInt32(WebConfigurationManager.AppSettings["smtpServerPort"]);
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential(
                        WebConfigurationManager.AppSettings["smtpServerRegisterCredentialsLogin"],
                        WebConfigurationManager.AppSettings["smtpServerRegisterCredentialsPassword"]);// Enter seders User name and password  
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                    return Task.FromResult(1);
                }
            }
        }
        catch (Exception e)
        {

            throw;
        }
    }