无法在Ubuntu服务器上发送电子邮件

时间:2016-03-24 19:26:15

标签: c# email ubuntu

我的小应用程序出了问题。应用程序能够在Windows上运行时发送电子邮件,但不能在Ubuntu 15.10上运行时发送。

C#代码:

    public bool sendMail(string subject, string msg, string[] emailTo)
    {
        MailMessage mail = new MailMessage();
        SmtpClient client = new SmtpClient();
        MailAddress from = new MailAddress("noreply@someserver.com", "Someserver - No Reply");
        // client settings
        client.EnableSsl = true;
        client.Port = 587;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("noreply@someserver.com", "someserverPassword");
        client.Host = "smtp.office365.com";

        //main settings
        mail.IsBodyHtml = true;
        mail.Body = msg + footer;
        mail.Subject = subject;
        mail.From = from;
        foreach (string mailTo in emailTo)
        {
            mail.To.Add(mailTo);

        }
        client.Send(mail);
        return true;
    }

这是我得到的错误:

Unhandled Exception: 
System.Net.Mail.SmtpException: Message could not be sent. ---> System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException: The authentication or decryption has failed. ---> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable
  at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (IAsyncResult asyncResult) <0x41d12100 + 0x000ff> in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) <0x41d12040 + 0x0002b> in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) <0x41cf64c0 + 0x00213> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (IAsyncResult result) <0x41d12d70 + 0x000bf> in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) <0x41d12b10 + 0x0007f> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (IAsyncResult asyncResult) <0x41cf4090 + 0x00153> in <filename unknown>:0
  at System.Net.Security.SslStream.EndAuthenticateAsClient (IAsyncResult asyncResult) <0x41cf3ec0 + 0x0003e> in <filename unknown>:0
  at System.Net.Security.SslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation) <0x41ced140 + 0x00055> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.InitiateSecureConnection () <0x41cecc70 + 0x000df> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.SendCore (System.Net.Mail.MailMessage message) <0x41cea260 + 0x0020f> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail.MailMessage message) <0x41ce3120 + 0x00207> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) <0x41ce2d90 + 0x00113> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) <0x41ce2d90 + 0x001c7> in <filename unknown>:0
  at Chillblast_ERP.MailingSystem.sendMail (System.String subject, System.String msg, System.String[] emailTo) <0x41cc8c80 + 0x00273> in <filename unknown>:0
  at erptesting.Program.Main (System.String[] args) <0x41c72800 + 0x001f3> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Mail.SmtpException: Message could not be sent. ---> System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException: The authentication or decryption has failed. ---> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable
  at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (IAsyncResult asyncResult) <0x41d12100 + 0x000ff> in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) <0x41d12040 + 0x0002b> in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) <0x41cf64c0 + 0x00213> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (IAsyncResult result) <0x41d12d70 + 0x000bf> in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) <0x41d12b10 + 0x0007f> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (IAsyncResult asyncResult) <0x41cf4090 + 0x00153> in <filename unknown>:0
  at System.Net.Security.SslStream.EndAuthenticateAsClient (IAsyncResult asyncResult) <0x41cf3ec0 + 0x0003e> in <filename unknown>:0
  at System.Net.Security.SslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation) <0x41ced140 + 0x00055> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.InitiateSecureConnection () <0x41cecc70 + 0x000df> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.SendCore (System.Net.Mail.MailMessage message) <0x41cea260 + 0x0020f> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail.MailMessage message) <0x41ce3120 + 0x00207> in <filename unknown>:0
  at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) <0x41ce2d90 + 0x00113> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) <0x41ce2d90 + 0x001c7> in <filename unknown>:0
  at Chillblast_ERP.MailingSystem.sendMail (System.String subject, System.String msg, System.String[] emailTo) <0x41cc8c80 + 0x00273> in <filename unknown>:0
  at erptesting.Program.Main (System.String[] args) <0x41c72800 + 0x001f3> in <filename unknown>:0

1 个答案:

答案 0 :(得分:0)

找到必要的证书:

sudo certmgr -ssl -m https://go.microsoft.com
sudo certmgr -ssl -m https://nugetgallery.blob.core.windows.net
sudo certmgr -ssl -m https://nuget.org
mozroots –-import -–sync