첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
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.Net.Mail;
using System.Reflection;

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

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

        /// <summary>
        /// 저장하기
        /// </summary>
        /// <param name="mailMessage">메일 메시지</param>
        /// <param name="filePath">파일 경로</param>
        public static void Save(this MailMessage mailMessage, string filePath)
        {
            Assembly assembly = typeof(SmtpClient).Assembly;

            Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");

            using(FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor
                (
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new Type[] { typeof(Stream) }, 
                    null
                );

                object mailWriter = mailWriterContructor.Invoke(new object[] { fileStream });

                MethodInfo sendMethodInfo = typeof(MailMessage).GetMethod
                (
                    "Send",
                    BindingFlags.Instance | BindingFlags.NonPublic
                );

                sendMethodInfo.Invoke
                (
                    mailMessage,
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new object[] { mailWriter, true },
                    null
                );

                MethodInfo closeMethodInfo = mailWriter.GetType().GetMethod
                (
                    "Close",
                    BindingFlags.Instance | BindingFlags.NonPublic
                );

                closeMethodInfo.Invoke
                (
                    mailWriter,
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new object[] { },
                    null
                );
            }
        }

        #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

댓글을 달아 주세요