/// <summary> /// Creates an IInbox /// </summary> /// <param name="serverConfiguration">The mail server configuration</param> /// <param name="inBoxImplementationType">The type of inbox implementation</param> /// <param name="user">The user</param> /// <returns>An IInbox instance</returns> public IInbox GetInbox(IMailServerConfiguration serverConfiguration, Type inBoxImplementationType, object user) { if (serverConfiguration == null) throw new NullArgumentException("serverConfiguration"); if (inBoxImplementationType == null) throw new NullArgumentException("inboxImplementationType"); if (user == null) throw new NullArgumentException("user"); string replyAddress = serverConfiguration.ReplyAddress; lock (_inboxFactoringLock) { try { IInbox inbox = null; if (!_inboxes.TryGetValue(replyAddress, out inbox)) { inbox = (IInbox)inBoxImplementationType.GetConstructor(new Type[0]).Invoke(null); inbox.InboxServerConfiguration = serverConfiguration; _inboxUsers[inbox] = 0; _inboxes[replyAddress] = inbox; inbox.BeginReceiving(); } _userInboxes[user] = inbox; _inboxUsers[inbox] = _inboxUsers[inbox] + 1; return inbox; } catch (Exception ex) { throw new FailedToGetInboxException(serverConfiguration, inBoxImplementationType, user, ex); } } }
/// <summary> /// Constructor /// </summary> public RaspMailHandler(IMailHandlerConfiguration configuration) { IMailServerConfiguration sendingServerConfiguration = configuration.SendingServerConfiguration; IMailServerConfiguration recievingServerConfiguration = configuration.RecievingServerConfiguration; Type outBoxImplementationType = configuration.OutBoxImplementationType; Type inBoxImplementationType = configuration.InBoxImplementationType; _inboxFactory = InboxFactory.GetInstance(); _inbox = _inboxFactory.GetInbox(recievingServerConfiguration, inBoxImplementationType, this); _outbox = (IOutbox)outBoxImplementationType.GetConstructor(new Type[0]).Invoke(null); _outbox.OutboxServerConfiguration = sendingServerConfiguration; if (_inbox != null) { _inbox.OnExceptionThrown += new MailboxExceptionThrown(CallbackExceptionThrown); _inbox.OnInboxStateChange += new OnInboxStateChangeDelegate(CallbackOnInboxStateChange); } if (_outbox != null) { _outbox.OnExceptionThrown += new MailboxExceptionThrown(CallbackExceptionThrown); } }
private static Dictionary <string, string> GetKeywords(IMailServerConfiguration serverConfiguration, Type inBoxImplementationType, object user) { Dictionary <string, string> keywords = KeywordFromType.GetKeyword(inBoxImplementationType); keywords.Add("mailaddress", serverConfiguration.ReplyAddress); KeywordFromString.GetKeyword(keywords, "user", user.ToString()); return(keywords); }
/// <summary> /// Constructor /// </summary> public Inbox(IMailServerConfiguration mailServerConfiguration) { if (mailServerConfiguration == null) { throw new MailServerConfigurationMissingException(); } _serverConfiguration = mailServerConfiguration; StartPollingQueue(); _asyncReceive = new ReceiveDelegate(Receive); }
/// <summary> /// Constructor /// </summary> /// <param name="outBoxImplementationType">The type of the outbox implementation</param> /// <param name="inBoxImplementationType">The type of the inbox implementation</param> /// <param name="sendingSeverConfiguration">The configuration of the sending server</param> /// <param name="recievingServerConfiguration">The configuration of the receiving server</param> public MailHandlerConfiguration (Type outBoxImplementationType, Type inBoxImplementationType, IMailServerConfiguration sendingSeverConfiguration, IMailServerConfiguration recievingServerConfiguration) { _outBoxImplementationType = outBoxImplementationType; _inBoxImplementationType = inBoxImplementationType; _sendingServerConfiguration = sendingSeverConfiguration; _recievingServerConfiguration = recievingServerConfiguration; }
private void SendEmail(IMailServerConfiguration mailServerConfiguration, IMailContent mailContent, bool isAsync) { MailMessage mailMessage = new MailMessage(); try { mailMessage.From = new MailAddress(mailServerConfiguration.MailAccount, mailContent.DisplayName); mailMessage.Subject = mailContent.Subject; mailMessage.Body = mailContent.Body; mailMessage.IsBodyHtml = mailContent.IsBodyHtml; mailMessage.Priority = mailContent.Priority; // Verifica se algum anexo foi especificado. if (mailContent.AttachmentList != null && mailContent.AttachmentList.Any()) { foreach (Attachment attachment in mailContent.AttachmentList) { mailMessage.Attachments.Add(attachment); } } // Adiciona todos os destinatários da mensagem. mailMessage.To.Add(string.Join(",", mailContent.ReceiverMailList)); if (mailContent.ReceiverCcList != null) { mailMessage.CC.Add(string.Join(",", mailContent.ReceiverCcList)); } if (mailContent.ReceiverBccList != null) { mailMessage.Bcc.Add(string.Join(",", mailContent.ReceiverBccList)); } SmtpClient client = new SmtpClient(mailServerConfiguration.SmtpServerAddress, mailServerConfiguration.SmtpPort); client.EnableSsl = mailServerConfiguration.UseSsl; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(mailServerConfiguration.MailAccount, mailServerConfiguration.MailAccountPassword); // Verifica se o email deve ser enviado de forma síncrona ou assíncrona. if (isAsync == true) { client.SendAsync(mailMessage, null); } else { client.Send(mailMessage); } } catch (Exception ex) { // Dispara o evento de erro. if (this.OnSendMailError != null) { this.OnSendMailError(this, new SendMailErrorEventArgs(ex)); } } finally { // Finaliza qualquer recurso alocado por arquivos anexos. if (mailMessage.Attachments != null) { mailMessage.Attachments.Dispose(); } } }
/// <summary> /// Sends a mail message. /// </summary> /// <param name="mailServerConfiguration">The mail account and smtp configuration.</param> /// <param name="mailContent">The mail message content.</param> public void SendEmail(IMailServerConfiguration mailServerConfiguration, IMailContent mailContent) { this.SendEmail(mailServerConfiguration, mailContent, false); }
/// <summary> /// Sends a mail message async. /// </summary> /// <param name="mailServerConfiguration">The mail account and smtp configuration.</param> /// <param name="mailContent">The mail message content.</param> public void SendEmailAsync(IMailServerConfiguration mailServerConfiguration, IMailContent mailContent) { this.SendEmail(mailServerConfiguration, mailContent, true); }
/// <summary> /// Constructor that takes the server configuration, inbox implementation, user and /// inner exception as parameters. The server configuraiton, inbox implementation and /// user is those used when attempting to get the inbox. The inner exception is the /// reason why it fails. /// </summary> /// <param name="serverConfiguration"></param> /// <param name="inBoxImplementationType"></param> /// <param name="user"></param> /// <param name="innerException"></param> public FailedToGetInboxException(IMailServerConfiguration serverConfiguration, Type inBoxImplementationType, object user, Exception innerException) : base(GetKeywords(serverConfiguration, inBoxImplementationType, user), innerException) { }