使用C#发送ASP.NET简单邮件

时间:2012-10-21 10:24:53

标签: c# asp.net

我试图在asp.net中发送简单的邮件。它不起作用。

这是代码:

protected void Button2_Click(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gorenparmak.com");
    mail.From = new MailAddress("radyo@gorenparmak.com");
    mail.To.Add("radyo@gorenparmak.org");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("radyo@gorenparmak.com", "write password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);
}

运行时产生错误:

  

无法解析远程名称:'smtp.gorenparmak.com'

我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试使用PORT 25,因为PORT 587用于gmail

示例尝试下面查看它是否有效,否则您遇到DNS问题

 // Set the to and from addresses.
 // The from address must be your GMail account
 mail.From = new MailAddress("gorenparmak.net<radyo@gorenparmak.com>");
 mail.To.Add(new MailAddress(to));

 // Define the message
 mail.Subject = subject;
 mail.IsBodyHtml = isHtml;
 mail.Body = message;

 // Create a new Smpt Client using Google's servers
 var mailclient = new SmtpClient();
 //mailclient.Host = "smtp.gmail.com";//ForGmail
 mailclient.Host = "mail.gorenparmak.com";
 //mailclient.Port = 587; //ForGmail
 mailclient.Port = 25;

 // This is the critical part, you must enable SSL
 //mailclient.EnableSsl = true;//ForGmail
 mailclient.EnableSsl = false;
 mailclient.UseDefaultCredentials = true;

 // Specify your authentication details
 //mailclient.Credentials = new System.Net.NetworkCredential("SomeGmailAccount@gmail.com", "PaswordXYX123");//ForGmail
 mailclient.Credentials = new System.Net.NetworkCredential("noreply@gorenparmak.com", "PaSsWaRd");

 mailclient.Send(mail);
 mailclient.Dispose();