我想在我的控制檯應用程序中發送電子郵件。我用:由SmtpClient發送電子郵件導致system.dll中出現System.InvalidOperationException
SmtpClient client = new SmtpClient();
MailMessage msg = new MailMessage();
MailAddress to = new MailAddress("[email protected]");
MailAddress from = new MailAddress("[email protected]");
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到你的項目中,並把配置放在那裏。 – Joe 2014-09-06 19:12:31