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

TestProject.zip
0.00MB
test.eml
0.00MB

▶ test.eml (생성 파일)

X-Sender: sender@domain.com
X-Receiver: receiver@domain.com
MIME-Version: 1.0
From: sender@domain.com
To: receiver@domain.com
Date: 6 Aug 2021 20:41:09 +0900
Subject: =?utf-8?B?7YWM7Iqk7Yq4IOuplOydvA==?=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

7YWM7Iqk7Yq4IOuplOydvCDsnoXri4jri6Qu

 

728x90

 

▶ MailMessageExtension.cs

using System;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;

namespace TestProject
{
    /// <summary>
    /// 메일 메시지 확장
    /// </summary>
    public static class MailMessageExtension
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 저장하기 - Save(mailMessage, stream)

        /// <summary>
        /// 저장하기
        /// </summary>
        /// <param name="mailMessage">메일 메시지</param>
        /// <param name="stream">스트림</param>
        public static void Save(this MailMessage mailMessage, Stream stream)
        {
            using(SmtpClient smtpClient = new SmtpClient())
            {
                string temporaryDirectoryPath = Path.Combine
                (
                    Path.GetTempPath(),
                    Assembly.GetExecutingAssembly().GetName().Name,
                    "temp",
                    Guid.NewGuid().ToString()
                );

                if(!Directory.Exists(temporaryDirectoryPath))
                {
                    Directory.CreateDirectory(temporaryDirectoryPath);
                }

                smtpClient.UseDefaultCredentials   = true;
                smtpClient.DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                smtpClient.PickupDirectoryLocation = temporaryDirectoryPath;

                smtpClient.Send(mailMessage);

                string filePath = Directory.GetFiles(temporaryDirectoryPath).Single();

                using(FileStream fileStream = new FileStream(filePath, FileMode.Open))
                {
                    fileStream.CopyTo(stream);
                }

                if(Directory.Exists(temporaryDirectoryPath))
                {
                    Directory.Delete(temporaryDirectoryPath, true);
                }
            }
        }

        #endregion
        #region 저장하기 - Save(mailMessage, filePath)

        /// <summary>
        /// 저장하기
        /// </summary>
        /// <param name="mailMessage">메일 메시지</param>
        /// <param name="filePath">파일 경로</param>
        public static void Save(this MailMessage mailMessage, string filePath)
        {
            using(FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                mailMessage.Save(fileStream);
            }
        }

        #endregion
    }
}

 

300x250

 

▶ Program.cs

using System.Net.Mail;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            MailMessage mailMessage = new MailMessage();

            mailMessage.Subject = "테스트 메일";
            mailMessage.From    = new MailAddress("sender@domain.com");
            mailMessage.Body    = "테스트 메일 입니다.";

            mailMessage.To.Add(new MailAddress("receiver@domain.com"));

            mailMessage.Save(@"d:\test.eml");
        }

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

댓글을 달아 주세요