简体   繁体   中英

windows service this mail server requires authentication when attempting to send to a non-local e-mail address

I have a Window Service(Asp.net C#) that sends emails (using SMTP) to users after some interval. It's working fine form some time, but when it encontered the email with "@gmail.com" it breaked with following error:

window service this mail server requires authentication when attempting 
to send to a non-local e-mail address. 
Please check your mail server client settings or contact your 
administrator to verify that the domain or address is defined for this server.

But with same credentials & code it sends email (to the same email address) when it's triggered on website hosted on same server(system) on IIS.

I searched on net, but couldn't understand why it's sending emails from Asp.net website code, but not from .net Windows service code (though this problem dosen't occure when sending en\\mails to same domain ie. abc.com that is used as SMTP server).

Any ideas what I am missing here or suggestions.

Thanks

Well in my case the culprit was the following line of code:

smtp.UseDefaultCredentials = false;

see the code below (after commenting it the emails are sent without error):

using (mmMessage)
        {
            mmMessage.Subject = strSubject;
            mmMessage.Body = strBody;
            mmMessage.IsBodyHtml = blnIsBodyHTML;
            mmMessage.Headers.Add("Reply-To", FromEmailAddress);
            SmtpClient smtp = new SmtpClient();
            using (smtp)
            {
                smtp.Host = SMTPServer;
                smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPwd);
                smtp.Port = Port;
                //smtp.UseDefaultCredentials = false;
                smtp.EnableSsl = IsEnableSsl;
                smtp.Send(mmMessage);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM