我正在使用 azure queue storage 发送电子邮件。电子邮件存储在队列存储中,队列一次发送 20 封电子邮件。
//Checks for messages inn the queue
foreach (CloudQueueMessage msgin sendEmailQueue.GetMessages(20, TimeSpan.FromSeconds(50)))
{
ProcessQueueMessage(msg);
}
我遇到的问题是,当一封电子邮件被添加到队列中时,SMTP 详细信息不正确(即密码错误),该消息会因为发送失败而留在队列中,并阻止队列中的其他消息发送。
private void ProcessQueueMessage(CloudQueueMessage msg)
{
try
{
//We try to send an email
SendEmail(emailRowInMessageTable, htmlMessageBodyRef, textMessageBodyRef);
} catch (SmtpException e)
{
string err = e.Message;
//When an error occurs we check to see if the message failed to send certain no. of
times
if (msg.DequeueCount > 10)
{
//We delete the message from queue
sendEmailQueue.DeleteMessage(msg);
return;
} else
{
//delete from top of queue
sendEmailQueue.DeleteMessage(msg);
//insert into end of queue
sendEmailQueue.AddMessage(msg);
return;
}
}
}
我尝试的解决方案是在出现错误时从队列中删除消息,并将其添加回队列的末尾,从而发送正确的电子邮件。但是删除并将消息添加回队列会重置其 dequeue 属性,这并不理想,因为我使用 dequeue 属性来确保消息不会永远在队列中。
在这种情况下,最好的解决方案是什么?