Can't send email using implicit SSL smtp serve

2019-09-09 05:48发布

问题:

I wrote up a sample program by copying the code in this KB article with some little edit as far as user's info. It uses the deprecate .NET library System.Web.Mail to do it because the new System.Net.Mail does not support implicit SSL. I went and tested it with Google smtp server on port 465 which is their implicit email port and everything works. However, when I gave this to a client to test it at his network, nothing get sent/receive, here is the error:

2013-03-07 15:33:43 - The transport failed to connect to the server.
2013-03-07 15:33:43 -    at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
2013-03-07 15:33:43 -    at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message)
2013-03-07 15:33:43 -    at System.Web.Mail.SmtpMail.Send(MailMessage message)

I'm not very well versed when it comes to email SSL so here is my possible theory to the root cause:

Assume he is using the right smtp server and right port (SSL port), I wonder if if any of the following could be the cause:

  1. They are using SSL on the mail server and yet he does not have the certificate installed on the machine where he runs my program from even though he is on the same domain and use the same email domain as a sender.

  2. They are using SSL but they maybe using NTLM or Anonymous authentication while my program uses basic authentication.

Sorry if I provide little information because I myself is quite foreign in this area so I'm still researching more.

Do you know of any steps I can do at my end to ensure my little test program can send using the smtp server of an implicit SSL email server?

Edit: I did add the following line in my code to indicates I'm using SSL.

oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

回答1:

Maybe this is to late to answer but have a look on https://sourceforge.net/p/netimplicitssl/wiki/Home/

You can send mail to port 465 Without the need of modifying your code, that much.

From the wiki page of project :

var mailMessage = new MimeMailMessage();
mailMessage.Subject = "test mail";
mailMessage.Body = "hi dude!";
mailMessage.Sender = new MimeMailAddress("you@gmail.com", "your name");
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MimeMailAddress("yourfriend@gmail.com", "your friendd's name")); 
mailMessage.Attachments.Add(new MimeAttachment("your file address"));
var emailer = new SmtpSocketClient();
emailer.Host = "your mail server address";
emailer.Port = 465;
emailer.EnableSsl = true;
emailer.User = "mail sever user name";
emailer.Password = "mail sever password" ;
emailer.AuthenticationMode = AuthenticationType.PlainText;
emailer.MailMessage = mailMessage;
emailer.OnMailSent += new SendCompletedEventHandler(OnMailSent);
//Send email
emailer.SendMessageAsync();

// A simple call back function:
private void OnMailSent(object sender, AsyncCompletedEventArgs asynccompletedeventargs)
{
    Console.Out.WriteLine(asynccompletedeventargs.UserState.ToString());
}


回答2:

Here I am using gmail smtp to send mail using c#. See the code below. It will give you an insight, How the stuffs are working. Replace gmail settings with your email server settings. Dont worry about the security certificates, they will be taken care of by the framework itself.

    public static bool SendMail(string to, string subject, string body)
    {
        bool result;
        try
        {
            var mailMessage = new MailMessage
            {
                From = new MailAddress("your email address")
            };

            mailMessage.To.Add(new MailAddress(to));
            mailMessage.IsBodyHtml = true;
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            var userName = "your gmail username";
            var password = "your gmail password here";
            var smtpClient = new SmtpClient
            {
                Credentials = new NetworkCredential(userName, password),
                Host = smtp.gmail.com,
                Port = 587,
                EnableSsl = true
            };

            smtpClient.Send(mailMessage);
            result = true;
        }
        catch (Exception)
        {
            result = false;
        }
        return result;
    }

The piece of code you were referencing was pretty old and obselete too. CDO was used in ASP apps to send mails. I think you havent scroll down to see

Article ID: 555287 - Last Review: April 7, 2005 - Revision: 1.0
APPLIES TO
Microsoft .NET Framework 1.1

You are refering a code that is pretty old... anyways follow the code shown up, everything will be FINE...

UPDATE

My bad, I have'nt read it carefully. But

I am leaving the above code as it is, as it might be a help for you or any other guy, who need the mailing functionality via SSL over gmail or any other server later.

. Then in such case you need some third party app.I found you a library See here