服务器注销后,SmtpClient无法连接(错误代码:10060)

时间:2019-09-19 05:20:37

标签: asp.net-mvc iis smtpclient gmail-imap

我们有一个程序可以发送电子邮件以供批准,并使用SSE提取答复。

在本地计算机上程序正在运行,没有遇到错误。但是,当程序在我们的服务器中发布时,我们遇到了一些SockectException,其本地错误代码为10060

Error Message

我们正在使用SSE使用此代码检查所有收件箱

public class MailRepository
{
    private readonly string mailServer, login, password;
    private readonly int port;
    private readonly bool ssl;

    public MailRepository(string mailServer, int port, bool ssl, string login, string password)
    {
        this.mailServer = mailServer;
        this.port = port;
        this.ssl = ssl;
        this.login = login;
        this.password = password;
    }

    public IEnumerable<IMessage> GetAllMails()
    {
        var messages = new List<IMessage>();

        using (var client = new ImapClient())
        {
            client.Connect(mailServer, port, ssl);

            // Note: since we don't have an OAuth2 token, disable
            // the XOAUTH2 authentication mechanism.
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            client.Authenticate(login, password);

            // The Inbox folder is always available on all IMAP servers...
            var inbox = client.Inbox;
            inbox.Open(FolderAccess.ReadOnly);
            var results = inbox.Search(SearchOptions.All, SearchQuery.All);
            foreach (var uniqueId in results.UniqueIds)
            {
                var message = inbox.GetMessage(uniqueId);
                var sender = message.From.FirstOrDefault();

                messages.Add(new IMessage() { 
                    htmlBody = message.HtmlBody,
                    body = message.TextBody,
                    subject = message.Subject,
                    sender = sender == null ? "": (sender as MailboxAddress).Address,
                    messageID = uniqueId
                });

                //Mark message as read
                //inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
            }

            client.Disconnect(true);
        }

        return messages;
    }
}

用法:

 var mail = new MailRepository("imap.gmail.com", 993, true, ConfigurationManager.AppSettings["approvalEmail"], "passwordhere")

该程序还会发送电子邮件

public string SendContract()
        {
            //var img = Regex.Match(Request["orderscontent"], @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;

            email mail = new email();

            mail.server = new SmtpClient("smtp.gmail.com");
            mail.server.Port = 587;
            mail.server.EnableSsl = true;
            mail.server.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["approvalEmail"], "passwordhere");
            //mail.server.Timeout = 10000;
            mail.From = new MailAddress(ConfigurationManager.AppSettings["approvalEmail"]);

            mail.To.Add(Request["email"]);
            mail.Subject = "DO Copy";
            mail.IsBodyHtml = true;

            mail.Attachments.Add(
                new Attachment(
                    new MemoryStream(Convert.FromBase64String(Request["orderscontent"])),
                    string.Format("DO_copy_{0:MMddyyyyHHmm}.pdf", DateTime.Now)
                )
            );

            mail.Send();

            return "Sent!";
        }

程序均返回Error Message。但是,当我们登录服务器时,我们注意到程序正在运行。但是,当服务器注销后,它会返回错误。因此,我不确定问题是来自服务器还是代码。

0 个答案:

没有答案
相关问题