public IMessageBuilder SetMessage(IMessage message) { ThrowIf.IsArgumentNull(() => message); _message = message; return(this); }
public IMessageBuilder AddToRecipient(IRecipient recipient, JObject recipientVariables = null) { //check for recipient ThrowIf.IsArgumentNull(() => recipient); if (_recipientCount == Constants.MaximumAllowedRecipients) { throw new Exception("Messages cannot contain more than To 1000 recipients"); } //set the to value if (_message.To == null) { _message.To = new Collection <IRecipient>(); } //set the recipient variables if (recipientVariables != null) { if (_message.RecipientVariables == null) { _message.RecipientVariables = new JObject(); } _message.RecipientVariables[recipient.Email] = recipientVariables; } //add to the message _message.To.Add(recipient); _recipientCount++; return(this); }
/// <summary> /// Send a message using the Mailgun API service. /// </summary> /// <param name="workingDomain">The mailgun working domain to use</param> /// <param name="message">The message to send</param> /// <returns></returns> public async Task <HttpResponseMessage> SendMessageAsync(string workingDomain, IMessage message) { //check for parameters ThrowIf.IsArgumentNull(() => workingDomain); ThrowIf.IsArgumentNull(() => message); //build request using (var client = new HttpClient()) { var buildUri = new UriBuilder { Host = BaseAddress, Scheme = UseSSl ? "https" : "http", Path = string.Format("{0}/messages", workingDomain) }; //add authentication client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + ApiKey))); //set the client uri return(await client.PostAsync(buildUri.ToString(), message.AsFormContent()).ConfigureAwait(false)); } }
/// <summary> /// Send a message using the Mailgun API service. /// </summary> /// <param name="workingDomain">The mailgun working domain to use</param> /// <param name="message">The message to send</param> /// <returns></returns> public async Task <HttpResponseMessage> SendMessageAsync(string workingDomain, IMessage message) { //check for parameters ThrowIf.IsArgumentNull(() => workingDomain); ThrowIf.IsArgumentNull(() => message); //build request using (var client = httpClientFactory?.CreateClient("Mailgun.MessageService") ?? new HttpClient()) { var buildUri = new UriBuilder { Host = BaseAddress, Scheme = UseSSl ? "https" : "http", Path = string.Format("{0}/messages", workingDomain) }; //add authentication client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + ApiKey))); //set the client uri return(await client.PostAsync(buildUri.ToString(), message.AsFormContent()) .ConfigureAwait(false)); //TODO may be read answer and throw exceptions on errors. //or introduce some new API that will do that } }
public IMessageBuilder SetTextBody(string textBody) { ThrowIf.IsArgumentNull(() => textBody); _message.Text = textBody; return(this); }
public IMessageBuilder SetHtmlBody(string htmlBody) { ThrowIf.IsArgumentNull(() => htmlBody); _message.Html = htmlBody; return(this); }
public IMessageBuilder SetSubject(string subject) { //check for recipient ThrowIf.IsArgumentNull(() => subject); _message.Subject = subject; return(this); }
public Task SendAsync(IdentityMessage message, IRecipient fromAddress) { ThrowIf.IsArgumentNull(() => message); ThrowIf.IsArgumentNull(() => fromAddress); //set from address _from = fromAddress; return(_options != null?SendWithOptions(message) : SendWithSimpleParameters(message)); }
public IMessageBuilder SetFromAddress(IRecipient sender) { //check for recipient ThrowIf.IsArgumentNull(() => sender); //add to the message _message.From = sender; return(this); }
/// <summary> /// Simple constructor with only the required domain and api key options /// </summary> /// <param name="domain">The Mailgun domain to use</param> /// <param name="apikey">The Mailgun Apikey to use</param> /// <param name="from">The from address to send messages from</param> /// <param name="baseUrlOverride"> Override the mailgun base URL</param> public MailgunMessageService(string domain, string apikey, string from, string baseUrlOverride = null) { ThrowIf.IsArgumentNull(() => domain); ThrowIf.IsArgumentNull(() => apikey); ThrowIf.IsArgumentNull(() => from); _from = new Recipient { Email = from }; _domain = domain; _messageService = new MessageService(apikey, true, baseUrlOverride); }
public IMessageBuilder AddAttachment(IFileAttachment file) { ThrowIf.IsArgumentNull(() => file); if (_message.FileAttachments == null) { _message.FileAttachments = new Collection <IFileAttachment>(); } //Add _message.FileAttachments.Add(file); return(this); }
public IMessageBuilder AddInlineImage(FileInfo file) { ThrowIf.IsArgumentNull(() => file); if (_message.Inline == null) { _message.Inline = new Collection <FileInfo>(); } //Add _message.Inline.Add(file); return(this); }
public IMessageBuilder AddTag(string tag) { ThrowIf.IsArgumentNull(() => tag); //create if not exist if (_message.Tags == null) { _message.Tags = new Collection <string>(); } //Add _message.Tags.Add(tag); return(this); }
public IMessageBuilder AddCustomHeader(string headerName, string headerData) { ThrowIf.IsArgumentNull(() => headerName); ThrowIf.IsArgumentNull(() => headerData); if (_message.CustomHeaders == null) { _message.CustomHeaders = new Dictionary <string, string>(); } //add the custom header _message.CustomHeaders.Add(headerName, headerData); return(this); }
public IMessageBuilder AddCustomData(string customName, JObject data) { ThrowIf.IsArgumentNull(() => customName); ThrowIf.IsArgumentNull(() => data); //create if not exist if (_message.CustomData == null) { _message.CustomData = new Dictionary <string, JObject>(); } //Add _message.CustomData.Add(customName, data); return(this); }
public IMessageBuilder AddCustomParameter(string parameterName, string data) { ThrowIf.IsArgumentNull(() => parameterName); ThrowIf.IsArgumentNull(() => data); //create if not exist if (_message.CustomParameters == null) { _message.CustomParameters = new Dictionary <string, string>(); } //Add _message.CustomParameters.Add(parameterName, data); return(this); }
public IMessageBuilder SetReplyToAddress(IRecipient recipient) { //check for recipient ThrowIf.IsArgumentNull(() => recipient); if (_message.CustomHeaders == null) { _message.CustomHeaders = new Dictionary <string, string>(); } //add the reply to header _message.CustomHeaders.Add("reply-to", string.IsNullOrEmpty(recipient.DisplayName) ? recipient.Email : string.Format("{0} <{1}>", recipient.DisplayName, recipient.Email)); return(this); }
/// <summary> /// Send a mail message async /// </summary> /// <param name="message">The message to send</param> /// <returns></returns> public static async Task <MailReceipt> SendAsync(MailMessage message) { //check for parameters ThrowIf.IsArgumentNull(() => message); var client = new HttpClient(); client.Timeout = new TimeSpan(0, 0, ObjectiaClient.GetTimeout()); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("User-Agent", "objectia-csharp/" + Constants.VERSION); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ObjectiaClient.GetApiKey()); client.DefaultRequestHeaders.Add("Accept", "application/pdf"); var payload = message.ToContent(); var response = await client.PostAsync(Constants.API_BASE_URL + "/v1/mail/send", payload); return(await ParseResponse <MailReceipt>(response)); }
private Task SendWithSimpleParameters(IdentityMessage message) { ThrowIf.IsArgumentNull(() => message); //Build a mailgun message from the message object var builder = new MessageBuilder() .AddToRecipient(new Recipient { Email = message.Destination }) .SetFromAddress(_from) .SetSubject(message.Subject) .SetTextBody(message.Body); if (_replyTo != null) { builder.SetReplyToAddress(_replyTo); } return(_messageService.SendMessageAsync(_domain, builder.GetMessage())); }
public IMessageBuilder AddBccRecipient(IRecipient recipient) { //check for recipient ThrowIf.IsArgumentNull(() => recipient); if (_bccCount == Constants.MaximumAllowedRecipients) { throw new Exception("Messages cannot contain more than 1000 Bcc recipients"); } //set the Bcc value if (_message.Bcc == null) { _message.Bcc = new Collection <IRecipient>(); } //add to the message _message.To.Add(recipient); _bccCount++; return(this); }
public static async Task <byte[]> CreateAsync(PDFOptions options) { //check for parameters ThrowIf.IsArgumentNull(() => options); var client = new HttpClient(); client.Timeout = new TimeSpan(0, 0, ObjectiaClient.GetTimeout()); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("User-Agent", "objectia-csharp/" + Constants.VERSION); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ObjectiaClient.GetApiKey()); client.DefaultRequestHeaders.Add("Accept", "application/pdf"); var payload = options.ToContent(); var response = await client.PostAsync(Constants.API_BASE_URL + "/v1/pdf/create", payload); return(await GetRawBody(response)); /* var client = new RestClient(Constants.API_BASE_URL); * client.Timeout = ObjectiaClient.GetTimeout() * 1000; * client.UseJson(); * * var request = new RestRequest("/v1/pdf/create", Method.POST); * request.AddHeader("Authorization", "bearer " + ObjectiaClient.GetApiKey()); * request.AddHeader("User-Agent", "objectia-csharp/" + Constants.VERSION); * request.AddHeader("Content-Type", "application/json"); * request.AddHeader("Accept", "application/pdf"); * * var payload = options.ToJSON(); * request.AddJsonBody(payload.ToString()); * * var response = await client.ExecuteAsync(request); * return GetRawBody(response);*/ }
/// <summary> /// Send an Identity message using the Mailgun mailer /// </summary> /// <param name="message"></param> /// <returns></returns> public Task SendAsync(IdentityMessage message) { ThrowIf.IsArgumentNull(() => message); return(_options != null?SendWithOptions(message) : SendWithSimpleParameters(message)); }
private Task SendWithOptions(IdentityMessage message) { ThrowIf.IsArgumentNull(() => message); var builder = new MessageBuilder(); builder. SetSubject(message.Subject). AddToRecipient(new Recipient { Email = message.Destination }); //set the default from if (_options.DefaultFrom != null) { builder.SetFromAddress(_options.DefaultFrom); } //override replyTo if it was set by SendAsync if (_from != null) { builder.SetFromAddress(_from); } //do customization based on service options builder.SetClickTracking(_options.TrackingClicks); builder.SetDkim(_options.UseDkim); builder.SetOpenTracking(_options.TrackingOpen); builder.SetTestMode(_options.TestMode); //add body if (_options.UseHtmlBody) { builder.SetHtmlBody(message.Body); } else { builder.SetTextBody(message.Body); } //set the default replyTo if (_options.DefaultReplyTo != null) { builder.SetReplyToAddress(_options.DefaultReplyTo); } //override replyTo if it was set by SendAsync if (_replyTo != null) { builder.SetReplyToAddress(_replyTo); } //add tags and headers if (_options.DefaultHeaders != null && _options.DefaultHeaders.Count > 0) { foreach (var kvp in _options.DefaultHeaders) { builder.AddCustomHeader(kvp.Key, kvp.Value); } } if (_options.DefaultTags != null && _options.DefaultTags.Count > 0) { _options.DefaultTags.ToList().ForEach(t => builder.AddTag(t)); } //send the message return(_messageService.SendMessageAsync(_options.Domain, builder.GetMessage())); }