首页 > 解决方案 > SMTP 邮件发送服务器名称与发件人电子邮件不同

问题描述

我陷入了 SMTP 服务器名称不同且发件人电子邮件名称不同的情况。

SMTP Server :- mail.in.xyz.com
Sender :- hrd@in.xyz.com

您可以在这里注意到 Server ismail.in.xyz.com和 Sender domain@in.xyz.com都是不同的。

它给出了一个错误: -

System.Net Information: 0 : [15384] SecureChannel#57667028 - Remote certificate has errors:
System.Net Information: 0 : [15384] SecureChannel#57667028 - A certificate chain processed, 
but terminated in a root certificate which is not trusted by the trust provider.

当它被转发给客户端时:- 错误文件是指自签名的证书之一(in.xyz.com)。但它需要引用另一个证书,即 XYZ 信任的 mail.in.xyz.com。由于它指的是自签名,因此发生了日志文件中提到的错误。

我正在使用以下代码发送邮件。

SmtpClient smtp = new SmtpClient
                {
                    Host = data.SMTPServer, // smtp server address here...                    
                    Port = data.PortNo,
                    EnableSsl = data.SSL,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                    Timeout = 30000,
                };


smtp.Send(message);

关于如何解决这个问题的任何想法?

标签: c#asp.net-mvcc#-4.0smtpsmtpclient

解决方案


public class Email
{
    private const string fromEmail = "mail@site.com";
    private const string webEmail = "webmail.site.com";
    private const string password = "Y2";

    //  Email.Send("x@gmail.com", "blah blah", "HIIIIIIIIIIIIIIIIIIII");
    public static void Send(string to, string subject, string body)
    {
        try
        {
            var message = new MailMessage
            {
                From = new MailAddress(fromEmail),
                To = { to },
                Subject = subject,
                Body = body,
                DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
            };
            using (SmtpClient smtpClient = new SmtpClient(webEmail))
            {
                smtpClient.Credentials = new NetworkCredential(fromEmail, password);
                smtpClient.Port = 25;
                smtpClient.EnableSsl = false;
                smtpClient.Send(message);
            }
        }
        catch (Exception excep)
        {
            throw new Exception(excep.Message);
        }
    }
}

推荐阅读