0

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?

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
  • What port are you going across..? please show the values for the SMTP Host, Port # , etc.. – MethodMan Sep 13 '12 at 17:58
  • Look at your SMTP Credentials you need to do NetworkCredential basicCredential = new NetworkCredential("username", "password"); smtpClient.UseDefaultCredentials = false; also wrap your code around the Try{}Catch{} smtpClient.Credentials = basicCredential; you did not follow the example exactly.. lol – MethodMan Sep 13 '12 at 18:04
  • Inside the second IF (`if (!String.IsNullOrEmpty(smtpLogin))`) as you can see, I'm using the example (There is a commented part on the IF, but I tested it without the comment) – Michel Ayres Sep 13 '12 at 18:07
  • Please don't complain about downvotes. Others are free to vote as they see fit, and will comment if they choose to. – Kyle Trauberman Sep 13 '12 at 18:42
  • @KyleTrauberman I'm just trying to create a post to help me and others, so if something is wrong is good to know, so I could fix it. – Michel Ayres Sep 13 '12 at 18:45

4 Answers4

1

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

Raj Tamakuwala
  • 1,163
  • 1
  • 9
  • 18
  • This is not really a answer more like a comment. The port used is not from GMAIL. I tested the information with outlook and it works fine – Michel Ayres Sep 13 '12 at 18:09
  • 1
    GMAIL and OutLooks Authentication are not the same you may want to add that to your initial question.. – MethodMan Sep 13 '12 at 18:15
  • 1
    I will post an example that you can try.. let me know if works for you. give me 1 min – MethodMan Sep 13 '12 at 18:17
0

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);
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Same error =/ I tried with your sample changing `from@gmail.com` to `sample@mydomain.com` and `smtp.gmail.com` to `smtp.mydomain.com` Tried with port `587` and port `25000` (of course, changed the password as well ^_^) – Michel Ayres Sep 13 '12 at 18:57
  • is this from your local machine or from a server where you have a real domain setup on your machine.. that should work for you.. – MethodMan Sep 13 '12 at 23:34
  • from the server with a real domain. I'm guessing that is about the cryptography. But not sure about it =/ – Michel Ayres Sep 14 '12 at 02:11
  • Btw this is the error `The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at` I even tried with my gmail account – Michel Ayres Sep 14 '12 at 18:44
0

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

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

  • Really good link, but I'm not using the `@gmail.com`, but `@my_domain.com`. But I did give it a try, without success. – Michel Ayres Sep 13 '12 at 18:34
0

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

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97