通过SmtpClient发送电子邮件会在system.dll中导致'System.InvalidOperationException [英] Sending e-mail by SmtpClient causes 'System.InvalidOperationException in system.dll
本文介绍了通过SmtpClient发送电子邮件会在system.dll中导致'System.InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在控制台应用程序中发送电子邮件。我用过:
I wanted to send e-mail in my console application. I used:
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.InvalidOperationException'的第一次机会异常
该代码的作者说,应该添加:
author of this code said that one should add:
<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应用程序,我看不到任何网站
to Web.config but this is not ASP .NET MVC application and I don't see any web config.
推荐答案
如果不使用任何配置文件,则必须使用代码配置SMTP客户端。
例如:
You have to configure your SMTP client in code if you are not using any configuration file. For example:
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = System.Net.NetworkCredential("youremail", "yourpassword");
smtp.EnableSsl = true;
这篇关于通过SmtpClient发送电子邮件会在system.dll中导致'System.InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文