2009-01-20 45 views

回答

1

最好的答案是,如果你直到现场一无所知,你能否将所有设置移到web.config中?这将允许配置直到最后一刻。下面是一些代码转储到你的web.config文件。我会问,为什么你没有访问这些信息虽然

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="SMTP SERVER ADDRESS" port="25" 
     userName="USERNAME" password="PASSWORD"> 
     </smtp> 
    </mailSettings> 
    </system.net> 
0

如果您的SMTP配置是正确的,只是这样做:

MailMessage mail = new MailMessage(); 

mail.To = "To"; 
mail.From = "From"; 
mail.Subject = "Subject";  
mail.Body = "Body"; 

SmtpMail.SmtpServer = "localhost"; 
SmtpMail.Send(mail); 
+0

要为“搞定”的属性,你不能对它进行设置这样的。 – Tarik 2009-07-20 00:13:33

11

添加到您的web.config(MSDN reference here ):

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network host="localhost" port="25" /> 
     </smtp> 
    </mailSettings> 
</system.net> 

使用SmtpClient而不指定配置设置将使用的值从web.config:

MailMessage msg = new MailMessage(...); 
// build message contents 
SmtpClient client = new SmtpClient(); 
client.Send(msg); 
+0

那个很棒。谢谢。 – Tarik 2009-07-10 00:24:07

2

我不久前回答了一个类似于这个问题。你可以查看它here。使用papercut,您可以在不知道或使用实际生产smtp服务器的情况下测试您的应用程序。

然后在测试过程中,您可以将主机设置为在app/web配置中运行papercut的本地机器。因此一旦转入生产就可以改变。

Papercut会告诉你发送的邮件以及内容。

0

作为替代方案:如果你不想依赖于服务器的配置和做编程你总是可以做到这一点:

MailMessage mail = new MailMessage() { 
    To = "[email protected]somewhere", 
    From = "[email protected]", 
    Subject = "My Subject", 
    Body = "My message" 
}; 

SmtpClient client = new SmtpClient("SMTP Server Address"); 
    // Naturally you change the "SMTP Server Address" to the 
    // actual SMTP server address 
client.Send(mail); 

但我建议你坚持在web.config文件(其中也可以通过ASP.NET Web配置工具进行配置)。

+0

这不起作用。 我得到一个.NET异常: “发送邮件失败。” 内部异常说:“{”远程名称无法解析:'SMTP服务器地址'“}” – Jonathan 2009-01-20 22:01:20

相关问题