开发者

Email Sender Freezes

I've Made an application that zips some files and sends them over by email. There are approximately 70 files (their total size is about 800kb).

The zip process freezes my application (but its ok because it takes about a second)

The problem is with the email process. While debugging, I found out that the entire email preparation process is quite fast, except

smtp.Send(message)

which completely freezes my application : After 5 second, the application is still running but the disappears from task-bar and even after the email has been sent, the application continue to not respond.

Send Email Function :

    public void SendMail(string FromGmailEmail, string GmailPassword, string ToEmail, string Subject, string Body, string[] AttachmentsPaths)
    {
        var fromAddress = new Mail开发者_JAVA技巧Address(FromGmailEmail, "None");
        var toAddress = new MailAddress(ToEmail, "None");
        string fromPassword = GmailPassword;
        string subject = Subject;
        string body = Body;

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        var message = new MailMessage(fromAddress, toAddress);
        message.Subject = subject;
        message.Body = body;

        try
        {
            for (int i = 0; i < AttachmentsPaths.Length; i++)
                message.Attachments.Add(new Attachment(AttachmentsPaths[i]));
        }
        catch (FileNotFoundException)
        {
        }
        smtp.Timeout = int.MaxValue;
        smtp.Send(message);
    }

I am opening a new thread when sending an email.

public void OpenEmailThread(string FromGmailEmail, string GmailPassword, string ToEmail, string Subject, string Body, string[] AttachmentsPaths)
    {
        Thread thread = new Thread(() => SendMail(FromGmailEmail, GmailPassword, ToEmail, Subject, Body, AttachmentsPaths));
        thread.Name = "EmailThread";
        thread.Start();
    }

Side Note : And for some the output tells me :

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll

on

smtp.Send(message)

(but that's the least of my problems)

EDIT : Turns out I was editing a file while he was sending it. I knew it would happen and that's why I added a bool variable called "IsEmailing" to lock the file while I was emailing. Turns out the files are STILL being uploaded after "smtp.Send(message);".

Solution : Zip the attachments before sending AND ONLY BEFORE SENDING. that way, the zip will occur only once so the .zip file cannot be modified.


I use this to send the mail on a new thread and it works ok...

        public void SendEmail(string from, string to, string subject, string body, string attachPath)
    {
        Thread threadSendMails;
        threadSendMails = new Thread(delegate()
        {

            sendEmail(from, to, subject, body, attachPath);

        });

        threadSendMails.IsBackground = true;
        threadSendMails.Start();

    }

Where sendMail is my own mailing function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜