【发布时间】:2014-10-31 07:25:14
【问题描述】:
我想在我的控制台应用程序中发送电子邮件。我用过:
SmtpClient client = new SmtpClient();
MailMessage msg = new MailMessage();
MailAddress to = new MailAddress("informatyka4445@wp.pl");
MailAddress from = new MailAddress("informatyka4444@wp.pl");
msg.IsBodyHtml = true;
msg.Subject = "Mail Title";
msg.To.Add(to);
msg.Body = "Your message";
msg.From = from;
try {
client.Send(msg);//THIS CAUSES ERROR
} catch (InvalidOperationException e) {
Console.WriteLine(e);
}
它会导致:
A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
这段代码的作者说应该添加:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="your email address" password="your password" defaultCredentials="false" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
到 Web.config,但这不是 ASP .NET MVC 应用程序,我没有看到任何 Web 配置。
【问题讨论】:
-
"这不是 ASP .NET MVC 应用程序,我没有看到任何 Web 配置。" - 然后将 app.config 添加到您的项目中,并将配置放在那里。
标签: c# .net web-config smtp console-application