我想将带有smtp的电子邮件发送到某些未经身份验证的电子邮件,但出现异常。
这是我的代码:
MailMessage mail = new MailMessage();
mail.To.Add("myEmail@gmail");
mail.From = new MailAddress("SomeEmail@test.com");
mail.Subject = "test subject";
mail.IsBodyHtml = true;
mail.Body = "test body";
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Host = "smtpserver";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod=System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine("Exception Message: " + ex.Message);
if (ex.InnerException != null)
Console.WriteLine("Exception Inner:"+ex.InnerException);
}
这是我的app.config
:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network
clientDomain="www.contoso.com"
defaultCredentials="false"
enableSsl="false"
host="mail.contoso.com"
port="25"
/>
</smtp>
</mailSettings>
</system.net>
</configuration>
我尝试将defaultCredentials
设置为true,但这也不起作用-请帮助我。
这是我得到的例外:
异常消息:发送邮件失败。
内部异常:
System.Net.WebException:无法解析远程名称:'smtpserver'at System.Net.ServicePoint.GetConnection(PooledStream PooledStream,对象所有者,布尔异步,IPAddress&地址,Socket&abortSocket,Socket&abortSocket6)
在System.Net.PooledStream.Activate(对象owningObject,布尔异步,GeneralAsyncDelegate asyncCallback)
在System.Net.PooledStream.Activate(对象owningObject,GeneralAsyncDelegate asyncCallback)处
在System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)
在System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)>在System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
在System.Net.Mail.SmtpClient.GetConnection()
在System.Net.Mail.SmtpClient.Send(MailMessage消息)