此代码不起作用
错误是: The remote certificate is invalid according to the validation procedure.
var client = new SmtpClient("smtp.domain.com.br", 25000)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true
};
client.Send("emailfrom@domain.com.br", "emailto@gmail.com", "test", "testbody");
// I also tested like this
// client.Send("emailfrom@domain.com.br", "emailto@domain.com", "test", "testbody");
但是这段代码可行
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true
};
client.Send("emailfrom@domain.com.br", "emailto@gmail.com", "test", "testbody");
我测试了Outlook中的第一个代码数据,如果我尝试发送到同一个域的电子邮件,则可以使用。
我认为错误是一些SMTP配置,但我不知道如何解决这个问题。有什么帮助吗?
答案 0 :(得分:1)
答案 1 :(得分:0)
根据下面的答案,你可以传入.config文件中的值,但是你必须改变我所拥有的..如果你愿意,首先使用硬编码值测试,然后将工作版本转换为使用参数值< / p>
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var _smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials =
new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
_smtpClient.Send(message);
}
答案 2 :(得分:0)
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
答案 3 :(得分:0)
我使用outlook中生成的数字证书解决了这个问题,并将其放在我的应用程序运行的服务器上。
根据我的问题中的代码加上证书,我终于使其有效。
以下是如何执行此操作的链接:create your own digital certificate