无法在asp.net中发送电子邮件

时间:2014-07-08 13:07:11

标签: c# asp.net email webforms

这是我的代码。我知道这是一个重复的问题,但我访问了staqckoverflow和codeproject中的所有链接,但我仍然无法得到答案。

try
    {
        MailMessage mailmessage = new MailMessage();
        mailmessage.To.Add("xxx.com");
        mailmessage.From = new MailAddress("xxx.com");
        mailmessage.Subject = "Transaction sent for Verification ";
        mailmessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
        SmtpClient smtpClient = new SmtpClient(ConfigurationSettings.AppSettings["SmtpAdd"].ToString());
        smtpClient.Send(mailmessage);
        ClientScript.RegisterStartupScript(this.GetType(), "mailalert", "alert('" + "Mail sent for verification" + "');", true);
    }
    catch(Exception ex)
    {
        //Response.Write("Could not send E-mail- error: " + ex.Message);
        ClientScript.RegisterStartupScript(this.GetType(), "mailfailure", "alert('" + ex.Message + "');", true);
    }

1 个答案:

答案 0 :(得分:0)

这会对你有所帮助

MailMessage msg = new MailMessage();
        msg.From = new MailAddress(email);
        msg.To.Add(new MailAddress("To_address"));
        msg.Subject = "You have a WebMail!!";
        msg.IsBodyHtml = true;
        msg.Body = "Message";
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new NetworkCredential("your mail address", "your password");
        smtp.Host = "host name";
        smtp.Port = "host number";
        smtp.EnableSsl = false;
        smtp.Send(msg);