如何通过C#asp.net发送邮件联系表格

use*_*946 5 c# asp.net email

我正在尝试创建联系表单以发送电子邮件(from并将to来自用户界面):

try {
   MailMessage mail = new MailMessage();
   SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
   mail.From = new MailAddress("fromadd");
   mail.To.Add("toadd");
   mail.Subject = "Test Mail";
   mail.Body = "This is for testing SMTP mail from GMAIL";
   SmtpServer.Port = 587;
   SmtpServer.Credentials = new System.Net.NetworkCredential("username","password");
   SmtpServer.EnableSsl = true;

   SmtpServer.Send(mail);
   MessageBox.Show("mail Send");
}
catch (Exception ex) {
   MessageBox.Show(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)

这仅适用于Gmail - 但是,我想让它适用于任何电子邮件提供商 - 我将如何解决这个问题?

Dan*_*ite 1

SmtpClient您可以在 web.config 中设置您的配置。这将使其变得灵活。

http://blog.dotnetclr.com/archive/2009/08/18/511.aspx

http://msdn.microsoft.com/en-us/library/w355a94k.aspx