첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ ContentType.cs

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
    }
}

 

728x90

 

▶ DocumentType.cs

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

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
    }
}

 

300x250

 

▶ Sender.cs

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

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

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

namespace TestProject
{
    /// <summary>
    /// 첨부 파일
    /// </summary>
    public class AttachmentFile
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 파일 경로 - FilePath

        /// <summary>
        /// 파일 경로
        /// </summary>
        public string FilePath { get; set; }

        #endregion
    }
}

 

▶ Program.cs

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
    }
}

※ 테스트를 생략했다.

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요