public MailGunResponse SendMessage(MailGunMessage msg) { if (msg == null) throw new ArgumentNullException("msg"); if (!msg.To.Any()) throw new ArgumentException("Cannot send MailGunMessage without a recipient"); var requestContent = new MultipartFormDataContent(); int attachmentCount = 0; foreach (var attachmentPath in msg.Attachments) { attachmentCount++; if (File.Exists(attachmentPath)) { string fileName = Path.GetFileName(attachmentPath); var fileLength = new FileInfo(attachmentPath).Length; if (attachmentCount > 5) { msg.Text = (msg.Text ?? string.Empty) + " {Attachment " + fileName + " NOT SENT. Maximum number of attachments reached.}"; if (!string.IsNullOrEmpty(msg.Html)) { msg.Html += " {Attachment " + fileName + " NOT SENT. Maximum number of attachments reached.}"; } } else if (fileLength < maxFileSizeBytes) // Actaully send the attachment. { var fileBytes = File.ReadAllBytes(attachmentPath); var attachmentContent = new ByteArrayContent(fileBytes); attachmentContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); requestContent.Add(attachmentContent, "attachment", fileName); } else { msg.Text = (msg.Text ?? string.Empty) + " {Attachment " + fileName + " NOT SENT. File size too large.}"; if (!string.IsNullOrEmpty(msg.Html)) { msg.Html += " {Attachment " + fileName + " dropped. File size too large.}"; } } } } // This puts the form data of the email at the *end* of the HTTP Request. I'm not really a fan of this, but we need to fiddle w/ the message content while adding in the attachments. foreach (var msgItem in msg.ToKvpList()) requestContent.Add(new StringContent(msgItem.Value), msgItem.Key); var responseTask = httpClient.PostAsync(MailGunMethods.MESSAGES, requestContent); MailGunResponse mailgunResponse = new MailGunResponse(); try { using (HttpResponseMessage responseMessage = responseTask.Result) { mailgunResponse.ParseResponseCode((int)responseMessage.StatusCode); mailgunResponse.Message = responseMessage.Content.ReadAsStringAsync().Result; } } catch (HttpRequestException requestException) { mailgunResponse.Message = "Exception during MailGun SendMessage: " + requestException.Message; } return mailgunResponse; }
public MailGunException(MailGunResponse mailgunResponse) { this.mailgunResponse = mailgunResponse; }