发送带有附件的电子邮件时如何解决超时错误?

时间:2019-06-20 09:32:09

标签: c# email-attachments chilkat

我正在使用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.

1 个答案:

答案 0 :(得分:0)

或者,您可以使用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();