■ HttpClient 클래스를 사용해 멀티 파트 폼을 업로드하는 방법을 보여준다.
▶ ContentType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
namespace TestProject { /// <summary> /// 컨텐트 타입 /// </summary> public static class ContentType { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 텍스트 /// </summary> public static readonly string Text = "TEXT"; /// <summary> /// HTML /// </summary> public static readonly string HTML = "HTML"; #endregion } } |
▶ DocumentType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
namespace TestProject { /// <summary> /// 문서 타입 /// </summary> public static class DocumentType { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 개인 /// </summary> public static readonly string Personal = "PERSONAL"; /// <summary> /// 공식 /// </summary> public static readonly string Official = "OFFICIAL"; #endregion } } |
▶ RecipientType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
namespace TestProject { /// <summary> /// 수신자 타입 /// </summary> public static class RecipientType { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 수신 /// </summary> public static readonly string TO = "TO"; /// <summary> /// 참조 /// </summary> public static readonly string CC = "CC"; /// <summary> /// 숨은 참조 /// </summary> public static readonly string BCC = "BCC"; #endregion } } |
▶ Sender.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using Newtonsoft.Json; namespace TestProject { /// <summary> /// 발송자 /// </summary> public class Sender { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 성명 - Name /// <summary> /// 성명 /// </summary> [JsonProperty("name")] public string Name { get; set; } #endregion #region E 메일 주소 - EMailAddress /// <summary> /// E 메일 주소 /// </summary> [JsonProperty("emailAddress")] public string EMailAddress { get; set; } #endregion } } |
▶ Recipient.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using Newtonsoft.Json; namespace TestProject { /// <summary> /// 수신자 /// </summary> public class Recipient { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 성명 - Name /// <summary> /// 성명 /// </summary> [JsonProperty("name")] public string Name { get; set; } #endregion #region E 메일 주소 - EMailAddress /// <summary> /// E 메일 주소 /// </summary> [JsonProperty("emailAddress")] public string EMailAddress { get; set; } #endregion #region 수신자 타입 - RecipientType /// <summary> /// 수신자 타입 /// </summary> [JsonProperty("recipientType")] public string RecipientType { get; set; } #endregion } } |
▶ Mail.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
using System.Collections.Generic; using Newtonsoft.Json; namespace TestProject { /// <summary> /// 메일 /// </summary> public class Mail { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 사용자 ID - UserID /// <summary> /// 사용자 ID /// </summary> [JsonProperty("userId")] public string UserID { get; set; } #endregion #region 메일 ID - MailID /// <summary> /// 메일 ID /// </summary> [JsonProperty("mailId")] public string MailID { get; set; } #endregion #region 제목 - Subject /// <summary> /// 제목 /// </summary> [JsonProperty("subject")] public string Subject { get; set; } #endregion #region 컨텐트 - Content /// <summary> /// 컨텐트 /// </summary> [JsonProperty("contents")] public string Content { get; set; } #endregion #region 컨텐트 타입 - ContentType /// <summary> /// 컨텐트 타입 /// </summary> [JsonProperty("contentType")] public string ContentType { get; set; } #endregion #region 문서 타입 - DocumentType /// <summary> /// 문서 타입 /// </summary> [JsonProperty("docSecuType")] public string DocumentType { get; set; } #endregion #region 발송 시간 - SendTime /// <summary> /// 발송 시간 /// </summary> [JsonProperty("sendDateVal")] public string SendTime { get; set; } #endregion #region 발송자 - Sender /// <summary> /// 발송자 /// </summary> [JsonProperty("sender")] public Sender Sender { get; set; } #endregion #region 수신자 리스트 - RecipientList /// <summary> /// 수신자 리스트 /// </summary> [JsonProperty("recipients")] public List<Recipient> RecipientList { get; set; } = new List<Recipient>(); #endregion } } |
▶ AttachmentFile.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
namespace TestProject { /// <summary> /// 첨부 파일 /// </summary> public class AttachmentFile { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 파일 경로 - FilePath /// <summary> /// 파일 경로 /// </summary> public string FilePath { get; set; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 업로드하기 (비동기) - UploadAsync(url, mail, attachmentFileList) /// <summary> /// 업로드하기 (비동기) /// </summary> /// <param name="url">URL</param> /// <param name="mail">메일</param> /// <param name="attachmentFileList">첨부 파일 리스트</param> /// <returns>처리 결과 태스크</returns> private async static Task<string> UploadAsync(string url, Mail mail, List<AttachmentFile> attachmentFileList) { HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url); requestMessage.Headers.ExpectContinue = false; MultipartContent multipartContent = new MultipartContent("form-data", Guid.NewGuid().ToString()); string mailContentJSON = JsonConvert.SerializeObject(mail, Formatting.Indented); StringContent mailContent = new StringContent(mailContentJSON, Encoding.UTF8); mailContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "mail" }; mailContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); multipartContent.Add(mailContent); if(attachmentFileList != null) { foreach(AttachmentFile attachmentFile in attachmentFileList) { FileStream fileStream = new FileStream(attachmentFile.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); StreamContent fileContent = new StreamContent(fileStream); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "attachments", FileName = fileStream.Name }; fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png"); multipartContent.Add(fileContent); } } requestMessage.Content = multipartContent; HttpClient httpClient = new HttpClient(); try { HttpResponseMessage responseMessage = await httpClient.SendAsync ( requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None ); HttpContent responseContent = responseMessage.Content; string responseResult = await responseContent.ReadAsStringAsync(); return responseResult; } catch(Exception exception) { Console.WriteLine(exception.ToString()); return null; } } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; string url = "https://dscore.com:8000/api/v1/common/mail/outlook/send"; Mail mail = new Mail(); mail.UserID = "gildong.hong"; mail.MailID = "51EE6D39-009E-4EB8-A7DC-896892195FE3"; mail.Subject = "테스트"; mail.Content = "테스트 메일 입니다."; mail.ContentType = ContentType.Text; mail.DocumentType = DocumentType.Personal; mail.SendTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.sssZ"); mail.Sender = new Sender { Name = "홍길동", EMailAddress = "hong@daum.net" }; mail.RecipientList.Add(new Recipient { Name = "김철수", EMailAddress = "kim@daum.net", RecipientType = RecipientType.TO }); List<AttachmentFile> attachmentFileList = new List<AttachmentFile>(); attachmentFileList.Add(new AttachmentFile { FilePath = "d:\\sample.png" }); string result = UploadAsync(url, mail, attachmentFileList).Result; Console.WriteLine(result); Console.ReadKey(false); } #endregion } } |
※ 테스트를 생략했다.