如何从文件系统发送带有文件的异步电子邮件

本文关键字:文件 异步 电子邮件 文件系统 | 更新日期: 2023-09-27 18:28:54

我有这样的情况,我正在发送一封图像为LinkedResource的电子邮件。

我已经使用ImageStream来读取该文件,它是有效的。

问题是:

当响应到达客户端时,它首先尝试访问此图像,但由于我已经异步发送了电子邮件,它没有完成发送,我收到了一个无法访问文件的错误。

有更好的方法吗

FileStream ImageStream = new FileStream(QRPath, FileMode.Open);
 System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(ImageStream, System.Net.Mime.MediaTypeNames.Image.Jpeg);
 imageResource.ContentId = "AttchImg";
                htmlView.LinkedResources.Add(imageResource);
SendMail(EmailMessage,true);

private void SendMail(MailMessage EmailMessage,bool IsAsync)
    {
        string FromEmail = ConfigurationSettings.AppSettings["EmailSender"];
        string FromPassword = ConfigurationSettings.AppSettings["EmailSenderPass"];
        EmailMessage.From = new System.Net.Mail.MailAddress(FromEmail);
        if (IsAsync)
        {
            try
            {
                AsyncMethodCaller caller = new AsyncMethodCaller(SendMailInSeperateThread);
                AsyncCallback callbackHandler = new AsyncCallback(AsyncCallback);
                caller.BeginInvoke(EmailMessage, callbackHandler, null);
            }
            catch (Exception ex)
            {
                //Logging
            }
        }
        else
        {
            SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = ConfigurationSettings.AppSettings["EmailSenderSmtpHost"];
            smtp.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["Port"]);//;587;
            //For google use smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(FromEmail, FromPassword);
            smtp.Timeout = 20000;
            smtp.Send(EmailMessage);
        }
    }

    private void SendMailInSeperateThread(MailMessage message)
    {
        try
        {
            SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = ConfigurationSettings.AppSettings["EmailSenderSmtpHost"];
            smtp.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["Port"]);//;587;
            //For google use smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            string FromEmail = ConfigurationSettings.AppSettings["EmailSender"];
            string FromPassword = ConfigurationSettings.AppSettings["EmailSenderPass"];
            smtp.Credentials = new NetworkCredential(FromEmail, FromPassword);
            smtp.Timeout = 20000;
            smtp.Send(message);
            //smtp.Dispose();
            //message.AlternateViews[0].LinkedResources.Dispose();
            //message.AlternateViews[1].LinkedResources.Dispose();
            //message.Dispose();
            // If you have a flag checking to see if an email was sent, set it here
            // Pass more parameters in the delegate if you need to...
        }
        catch (Exception e)
        {
            // This is very necessary to catch errors since we are in
            // a different context & thread
            //Elmah.ErrorLog.GetDefault(null).Log(new Error(e));
        }
    }
    private void AsyncCallback(IAsyncResult ar)
    {
        try
        {
            AsyncResult result = (AsyncResult)ar;
            AsyncMethodCaller caller = (AsyncMethodCaller)result.AsyncDelegate;
            caller.EndInvoke(ar);
        }
        catch (Exception e)
        {
            //Elmah.ErrorLog.GetDefault(null).Log(new Error(e));
            //Elmah.ErrorLog.GetDefault(null).Log(new Error(new Exception("Emailer - This hacky asynccallback thing is puking, serves you right.")));
        }
    }

我刚刚发现你可以做到:

LinkedResource imageResource=新的LinkedResource("C:''myImage.png","image.jpg");

这比斯里拉姆·萨克西维尔写的更好吗???

如何从文件系统发送带有文件的异步电子邮件

尝试使用其他FileStream重载之一。

FileStream ImageStream = new FileStream(QRPath, FileMode.Open, FileAccess.Read, FileShare.Read);