简体   繁体   English

收件人电子邮件地址重叠

[英]Recipient email address overlapping

I recently deployed a C# script that utilizes System.Net.Mail functionality. 我最近部署了一个利用System.Net.Mail功能的C#脚本。 The mailing system will work by sending a photobooth screenshot as an attachment to each respective email addresses once users filled up the form ( hence each users are supposed to receive an unique image attachments ). 一旦用户填写了表格,邮件系统将通过将photobooth屏幕快照作为附件发送到每个电子邮件地址来工作(因此,每个用户都应该收到唯一的图像附件)。

The script works flawlessly but I am suffering from one issue: once the internet connection became slow or suffer from some random down time, the email addresses will overlap and current guests will receive attachments from previous users. 该脚本可以正常工作,但我遇到一个问题:一旦互联网连接变慢或出现随机中断时间,电子邮件地址就会重叠,当前来宾将收到以前用户的附件。

I am wondering if there are any method that can create/compose a new mail without interrupting the current sending processes. 我想知道是否有任何方法可以创建/撰写新邮件而不会中断当前的发送过程。 I am using Gmail btw. 我正在使用Gmail。

Below is the code I'm using: 以下是我正在使用的代码:

MailMessage mail = new MailMessage();

mail.Attachments.Clear();

mail.From = new MailAddress("@gmail.com");
mail.To.Add(email);
mail.Subject = "";
mail.Body = "";

mail.Attachments.Add(new Attachment("screenshot.jpg"));
//mail.Attachments.Add (new Attachment("screenshot.jpg"));

SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("@gmail.com", "") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = 
    delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
};
smtpServer.Send(mail);
//Debug.Log("success");
sent = true;

Best solution would be to decouple the processes of creating and sending the emails. 最好的解决方案是使创建和发送电子邮件的过程脱钩。

Create each mail message and drop it in a queue. 创建每个邮件并将其放入队列中。 You can then have the mail sending process monitoring the queue and delivering any messages there as quickly as it can. 然后,您可以让邮件发送过程监视队列,并尽可能快地在该队列中传递任何消息。

在单独的线程上发送邮件,例如使用任务。

    Task.Factory.StartNew(() => smtpServer.Send(mail));

This is a web application, correct? 这是一个Web应用程序,对吗? And is this line of code pulling from a physical file on the disk?: 这行代码是从磁盘上的物理文件中提取的吗?

mail.Attachments.Add (new Attachment ("screenshot.jpg"));

If so then there's your problem. 如果是这样,那是你的问题。 Web applications are multi-threaded. Web应用程序是多线程的。 So lots of users could be modifying that file at the same time. 因此,许多用户可能会同时修改该文件。 In the time it takes the application to go from the step of modifying that file to the step of reading that file, another user may have modified it. 在应用程序从修改该文件的步骤转到读取该文件的步骤的过程中,其他用户可能已对其进行了修改。

You have a couple of options: 您有两种选择:

  1. Create a unique file name for each time a user goes through this process. 每次用户执行此过程时,请创建一个唯一的文件名。 Do you need to keep the files afterward? 之后需要保留文件吗? I'm guessing not, since you overwrite them. 我猜不是,因为您覆盖了它们。 So use something like Path.GetTempFileName() to create a temporary file and pass that from the saving step to the reading/emailing step instead of hard-coding the same file name. 因此,请使用Path.GetTempFileName()类的东西来创建一个临时文件,然后将其从保存步骤传递到读取/发出消息的步骤,而不是硬编码相同的文件名。
  2. Never save the file to disk, and instead just pass the byte[] of the file's data from the uploading step to the sending step, building the Attachment from the byte array . 永远不要将文件保存到磁盘,而只是将文件数据的byte[]从上传步骤传递到发送步骤, 从而从byte数组构建Attachment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM