电子邮件发件人冻结
我制作了一个应用程序,可以压缩一些文件并通过电子邮件发送它们。大约有 70 个文件(总大小约为 800kb)。
压缩过程冻结了我的应用程序(但没关系,因为它需要大约一秒钟)
问题出在电子邮件过程上。在调试时,我发现整个电子邮件准备过程相当快,除了
smtp.Send(消息)
完全冻结了我的应用程序:5 秒后,应用程序仍在运行,但从任务栏中消失,即使在发送电子邮件后,应用程序仍然没有响应。
发送电子邮件功能:
public void SendMail(string FromGmailEmail, string GmailPassword, string ToEmail, string Subject, string Body, string[] AttachmentsPaths)
{
var fromAddress = new MailAddress(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);
}
我在发送电子邮件时打开一个新线程。
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();
}
旁注:对于某些输出告诉我:
mscorlib.dll 中发生了“System.IO.IOException”类型的第一次机会异常
mscorlib.dll中
smtp.Send(消息)
(但这是我遇到的最少的问题)
编辑:原来我在他发送文件时正在编辑文件。我知道会发生这种情况,这就是为什么我添加了一个名为“IsEmailing”的布尔变量来在发送电子邮件时锁定文件。事实证明,在“smtp.Send(message);”之后,文件仍在上传。
解决方案:在发送之前且仅在发送之前压缩附件。这样,zip 只会发生一次,因此 .zip 文件无法修改。
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 MailAddress(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用它在新线程上发送邮件,它工作正常...
其中 sendMail 是我自己的邮件功能。
I use this to send the mail on a new thread and it works ok...
Where sendMail is my own mailing function.