3

我编写了 C# 代码来发送邮件(我的公司邮件)。我尝试使用 gmail 并且它可以正常工作,但我的公司邮件却不行。

我确定 smtp 服务器正在运行并且端口 465 已打开,因为我可以使用相同的帐户通过 Outlook 2k7 发送邮件,telnet smtp.domain 465 ok。

当我运行代码时,它会抛出异常“System.Net.Mail.SmtpException:操作超时。”

这是我的 C# 代码:

        var fromAddress = new MailAddress("ID@domain", "Display Name");
        var toAddress = new MailAddress("ID@domain", "Display Name");
        const string subject = "Test mail";
        const string body = "Test mail";

        var smtp = new SmtpClient
        {
            Host = "smtp.domain",
            Port = 465,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("ID", "pass"),
            Timeout=15000                
        };

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body,

        })
        {
            try
            {
                smtp.Send(message);
                MessageBox.Show("OK");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

任何人知道或有相同的问题已解决,请帮助我。非常感谢!

4

2 回答 2

0

尝试在您的 SmtpClient 实例中更改设置 EnableSsl = false。

于 2013-08-06T05:12:00.547 回答
0

您可以更改此代码并重用此代码:

        public static void sendEmail(string address, string subject, string message)
        {
            string email = "yourEmail";
            string password = "yourPass";

            var loginInfo = new NetworkCredential(email, password);
            var msg = new MailMessage();
            var smtpClient = new SmtpClient("smtp.gmail.com", portNumber);

            msg.From = new MailAddress(email);
            msg.To.Add(new MailAddress(address));
            msg.Subject = subject;
            msg.Body = message;
            msg.IsBodyHtml = true;

            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = loginInfo;
            smtpClient.Send(msg);
        }
于 2013-08-06T13:42:53.447 回答