简体   繁体   中英

smtp (gmail) fails in classic asp, but works in web service

I'm using .Net 4 c#, Windows Server 2008 R2, IIS 7 with the following code in my web service that I'm using to test sending an email. It works. I then use the same code in my classic asp app that calls .net c# via COM+ and it fails with

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it xx.xxx.xxx.xxx:587"

I have researched this for days and can't find anything that will help. Any help would be greatly appreciated. Thank you.

        public static class Mail
        {
        public static void SendEmail(string subject, string body)
        {
        var fromAddress = new MailAddress("user@gmail.com", "From Name");
        var toAddress = new MailAddress("user@email.com", "To Name"); 
        const string fromPassword = "password";

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
        }
    }

If you really are using "user@gmail.com", I am guessing the server refuses to accept the credentials because this user is unknown. Try using an e-mail adress or username that actually exists on the server.

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