public void Send(EmailMessage message) { var client = new MailJetClient(PublicKey, PrivateKey); Models.SentMessageData result = null; try { result = client.SendMessage(message); } catch (Exception ex) { GlobalConfiguration.Configuration.Logger.Error(ex); var sentFail = new SentFail(); sentFail.Message = GetFirstGoodErrorMessage(ex); sentFail.Stack = ex.ToString(); sentFail.MessageId = message.MessageId; sentFail.Recipients = message.Recipients; sentFail.Subject = message.Subject; var failQueueName = message.SentFailQueueName ?? GlobalConfiguration.Configuration.SentFailQueueName; Bus.Send(failQueueName, sentFail); } if (result == null) { return; } if (!message.DoNotTrack) { var sentMessage = new SentMessage(); sentMessage.Body = message.Body; sentMessage.MessageId = message.MessageId; sentMessage.Recipients = message.Recipients; sentMessage.Subject = message.Subject; sentMessage.SmtpInfo = "mailjetapi"; sentMessage.Sender = message.Sender; sentMessage.EntityId = message.EntityId; sentMessage.EntityName = message.EntityName; if (message.SenderAlias != null) { sentMessage.Sender = message.SenderAlias; } var queueName = message.SentMessageQueueName ?? GlobalConfiguration.Configuration.SentMessageQueueName; if (message.SentMessage == null) { Bus.Send(queueName, sentMessage); } else { Bus.Send(queueName, message.SentMessage); } } //foreach (var item in result.Sent) //{ // var pending = new PendingMessage(); // pending.MessageId = message.MessageId; // pending.MailJetMessageId = item.MessageID; // pending.Email = item.Email; // pending.SendDate = DateTime.Now; // pending.Subject = message.Subject; // pending.Recipients = message.Recipients; // StatusChecker.Current.Add(pending); //} }
public string CheckPendingMessage(string messageId) { PendingMessage pending = null; PendingList.TryGetValue(messageId, out pending); if (pending == null) { return(null); } if (pending.CheckCount > 10) { return(pending.MessageId); } // Every 20 minutes or first time if (!pending.LastCheckDate.HasValue || pending.LastCheckDate.Value.AddMinutes(20) < DateTime.Now) { var client = new MailJetClient(PublicKey, PrivateKey); pending.CheckCount++; pending.LastCheckDate = DateTime.Now; Models.MessageHistory result = null; try { result = client.GetMessageHistory(pending.MailJetMessageId); } catch (Exception ex) { GlobalConfiguration.Configuration.Logger.Error($"Get history fail with email {pending.Email}/{pending.Subject}"); GlobalConfiguration.Configuration.Logger.Error(ex); SentFail(pending, "mailjet:historyFail", "Status code error"); } if (result == null) { return(null); } if (result.Count == 0) { return(null); } var item = result.Data.Last(); if (item.EventType == Models.MessageStatus.Clicked || item.EventType == Models.MessageStatus.Opened || item.EventType == Models.MessageStatus.Sent) { GlobalConfiguration.Configuration.Logger.Debug($"email {pending.Email}/{pending.Subject} was sent with status {item.EventType}"); return(pending.MessageId); } else if (item.EventType == Models.MessageStatus.Bounced) { GlobalConfiguration.Configuration.Logger.Warn($"email {pending.Email}/{pending.Subject} was bounces"); SentFail(pending, "mailjet:bounce", item.Comment); return(pending.MessageId); } else if (item.EventType == Models.MessageStatus.Blocked) { GlobalConfiguration.Configuration.Logger.Warn($"email {pending.Email}/{pending.Subject} was blocked"); SentFail(pending, "mailjet:blocked"); } else if (item.EventType == Models.MessageStatus.Queued) { return(null); } else if (item.EventType == Models.MessageStatus.Spam) { GlobalConfiguration.Configuration.Logger.Warn($"email {pending.Email}/{pending.Subject} marked as spam"); SentFail(pending, "mailjet:spam"); return(pending.MessageId); } else if (item.EventType == Models.MessageStatus.Unsub) { GlobalConfiguration.Configuration.Logger.Warn($"email {pending.Email}/{pending.Subject} need unsubscribe"); SentFail(pending, "mailjet:unsubscribe"); return(pending.MessageId); } } return(null); }