سؤال

REMAKING QUESTION

This code doesn't work

The error is: 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");

But this code works

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");

I tested the first code data in the Outlook, and works if I try to send to an email from the same domain.

I believe that the error is some SMTP configuration, but I don't know how to solve this. Any help?

هل كانت مفيدة؟

المحلول 4

I solve this using the digital certificate generated in the outlook and putting it on the server, where my application is running.

With the code in my question plus the certificate I finally made this works.

Here is a link on How to do this: create your own digital certificate

نصائح أخرى

which smtp server you using ? if you are using gmail smtp server then you must use port number 587 that is required to send through gmail

based on the answer below you can pass in the values from the .config file but you will have to change what I have ..test this using hard coded values first if you like then convert the working version to use param values

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);
}

did you try port 465. may be you can check this article.

http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top