0

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 = new LinkedResource("C:\myImage.png", "image/jpg");

それは、Sriram Sakthivel が書いたよりも良い方法ですか ???

4

1 に答える 1