■ MailMessage 클래스를 사용해 EML 파일을 저장하는 방법을 보여준다.
▶ test.eml (생성 파일)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-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 |
▶ MailMessageExtension.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 |
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 } } |
▶ 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 |
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 } } |