【问题标题】:How to fix timeout error while sending email with attachment?发送带附件的电子邮件时如何解决超时错误?
【发布时间】:2023-04-05 09:27:01
【问题描述】:

我正在使用 chilkat 试用版通过服务帐户发送电子邮件。 访问https://www.example-code.com/csharp/smtp_gmailOAuth2.asp

使用 Chilkat mailman.SendEmail() 发送附件大小超过 100KB 的电子邮件时出现空闲超时错误。但我需要以 MB 格式发送文件。

        readSmtpResponse:
        Failed to read beginning of SSL/TLS record.
        b: 0
        dbSize: 0
        nReadNBytes: 0
        idleTimeoutMs: 30000
        Failed to receive more TLS application data.
        tlsApp: Socket operation timeout.
        elapsedMs: Elapsed time: 30031 millisec
        idleTimeoutMs: 30000
      --readSmtpResponse
      SMTP failed when receiving the DATA terminator response.
      smtpRcvFinalResponse: Socket operation timeout.

【问题讨论】:

    标签: c# email-attachments chilkat


    【解决方案1】:

    您也可以使用System.Net.Mail。在任何用例中都适合我。如果你真的需要使用 chilkat,请忽略我的帖子。

    using System;
    using System.Net;
    using System.Net.Mail;
    
    namespace BTCSharp
    {
        public abstract class Mail
        {
            static SmtpClient Client = new SmtpClient();
            private static string From;
    
            public static void Login(string xHost, int xPort, string xUsername, string xPassword, bool xSsl)
            {
                //LOGIN
                Client.Host = xHost;
                Client.Port = xPort;
                Client.DeliveryMethod = SmtpDeliveryMethod.Network;
    
                //CREDENTIALS
                Client.UseDefaultCredentials = false;
                Client.Credentials = new NetworkCredential(xUsername, xPassword);
                Client.Timeout = 30000;
                Client.EnableSsl = xSsl;
                if (xUsername.Contains("@")) From = xUsername;
            }
            public static void Login(string xHost, int xPort, string xAddress, string xUsername, string xPassword, bool xSsl)
            {
                //LOGIN WHERE USERNAME DIFFERENT TO MAIL ADDRESS
                Login(xHost, xPort, xUsername, xPassword, xSsl);
                From = xAddress;
            }
    
            public static bool Send(string xFrom, string xTo, string xSubject, string xBody, params Attachment[] xAttachments)
            {
                //SEND MAIL
                if (Client.Credentials == null) { BTCSharp.Client.Console("[Mail] Error: login needed"); return false; }
                try
                {
                    MailMessage mail = new MailMessage(xFrom, xTo, xSubject, xBody);
                    foreach (Attachment attachment in xAttachments) mail.Attachments.Add(attachment);
                    Client.Send(mail);
                    return true;
                }
                catch (Exception e)
                {
                    //FOR GMAIL VISIT: https://myaccount.google.com/lesssecureapps
                    Console.WriteLine("[Mail] Error: " + e.Message);
                    return false;
                }
            }
    
            public static void Send(string xTo, string xSubject, string xBody, params Attachment[] xAttachments)
            {
                //SEND MAIL
                Send(From, xTo, xSubject, xBody, xAttachments);
            }
        }
    }
    

    使用方法:

    Attachment attachment = new Attachment(path);
                    Mail.Login("mail.server", 25, "mail@adress.com", "username", "pw", false);
                    Mail.Send("mail.receiver@gmail.com", "text", attachment);
                    attachment.Dispose();
    

    【讨论】:

    • 感谢您的回复,我需要使用服务帐户发送邮件,而 chilkat 拥有该功能。您的代码是否适用于服务帐号?
    • @Harish Kumar 应该是。您需要服务帐户的邮件服务器。应该像harish.company.25.smtp 和端口号一样平滑。邮件服务器大多有一个标准端口,如25。您应该向管理员询问这些信息。
    • 不工作,出现失败异常,提示连接关闭
    • 您的登录信息可能有问题。我建议您在像 googlemail 这样的简单电子邮件提供商上重现您的用例。如果它在那里工作,你就知道问题出在哪里。
    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 2020-01-12
    • 2013-04-14
    • 2015-08-28
    • 2016-07-01
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多