为什么发送电子邮件需要这么长时间?

Ich*_*ann 3 c# email console-application

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Threading;

namespace MGR_Backuper
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = "D:\\Programy\\WinRAR\\rar.exe";
            string command = "C:\\Users\\Pawel\\Desktop\\Praca magisterska back\\mgrback-%YEAR%-%MONTH%-%DAY%-%HOUR%-%MINUTES%-%SECONDS%.rar";
            string arg = "a \"%FILENAME%\" \"C:\\Users\\Pawel\\Desktop\\Praca magisterska\\*\" -m5 -ep1 -hppasspass -rr5 -inul -r0";
            var t1 = DateTime.Now;
            command = command.Replace("%YEAR%", "" + t1.Year);
            if (t1.Month < 10) command = command.Replace("%MONTH%", "0" + t1.Month);
            else command = command.Replace("%MONTH%", "" + t1.Month);

            if (t1.Day < 10) command = command.Replace("%DAY%", "0" + t1.Day);
            else command = command.Replace("%DAY%", "" + t1.Day);

            if (t1.Hour < 10) command = command.Replace("%HOUR%", "0" + t1.Hour);
            else command = command.Replace("%HOUR%", "" + t1.Hour);

            if (t1.Minute < 10) command = command.Replace("%MINUTES%", "0" + t1.Minute);
            else command = command.Replace("%MINUTES%", "" + t1.Minute);

            if (t1.Second < 10) command = command.Replace("%SECONDS%", "0" + t1.Second);
            else command = command.Replace("%SECONDS%", "" + t1.Second);

            arg = arg.Replace("%FILENAME%", command);
            var t2 = DateTime.Now;
            Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = file;
            proc.StartInfo.Arguments = arg;

                proc.Start();

                while (!proc.HasExited)
                    Thread.Sleep(50);


            var t3 = DateTime.Now;
            try
            {
                MailMessage mailMsg = new MailMessage();
                mailMsg.To.Add("email@email.com");

                // From
                MailAddress mailAddress = new MailAddress("email@email.com", "Sender");
                mailMsg.From = mailAddress;

                // Subject and Body
                mailMsg.Subject = "MGR";
                mailMsg.Body = "Backup magisterki";
             mailMsg.Attachments.Add(new Attachment(command));

                SmtpClient smtpClient = new SmtpClient("server.serv.com", 587);
                NetworkCredential credentials = new NetworkCredential("login", "password");
                smtpClient.Credentials = credentials;
                smtpClient.Timeout = Int32.MaxValue;
                smtpClient.Send(mailMsg);
            }
            catch (Exception ex)
            { Console.WriteLine(ex.ToString()+"\n"); }
            var t4 = DateTime.Now;
            Console.WriteLine("Czas kompresji: \t{0:0.00}s", (t3 - t2).TotalSeconds);
            Console.WriteLine("Czas wysy?ania: \t{0:0.00}s", (t4 - t3).TotalSeconds);
            Console.WriteLine("Czas ca?kowity: \t{0:0.00}s", (t4 - t1).TotalSeconds);
            Console.ReadKey(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的程序,用于通过将一些对我重要的数据发送到我的电子邮件地址来备份它。创建的存档大约为 1.5 MB。发送电子邮件大约需要 7-8 分钟。这不是因为连接不良,因为当我尝试通过电子邮件程序或网络邮件发送相同的文件时,它需要 5-6 秒。为什么以这种方式发送电子邮件这么慢?

编辑 :

我做了另一个测试

            var ts1 = DateTime.Now;
            smtpClient.Send(mailMsg);
            var ts2 = DateTime.Now;
Run Code Online (Sandbox Code Playgroud)

并确认这smtpClient.Send(mailMsg);是造成放缓的原因。知道为什么吗?

Ghy*_*hal 5

我遇到了同样的问题,似乎 SMTP 服务器在发送邮件之前正在做某种检查。

我通过使用新线程以异步方式发送邮件解决了我的问题,如下所示。

private void SendMail(string from, string to)
{
    //your logic to send the mail
}
Run Code Online (Sandbox Code Playgroud)

以异步方式发送邮件。

ThreadStart threadStart = delegate() { SendMail(from, to); };
Thread thread = new Thread(threadStart);
thread.Start();
Run Code Online (Sandbox Code Playgroud)

  • @Ichibann 它将在后台发送邮件,因此您的应用程序可以在发送时做其他事情,但是因为这是您的程序所做的最后一件事,所以在这种情况下它并没有真正的帮助...... (2认同)