■ SmtpClient 클래스의 DeliveryMethod/PickupDirectoryLocation 속성을 사용해 EML 파일을 저장하는 방법을 보여준다. (기능 개선)
▶ test.eml (생성 파일)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
-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 |
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
}
}
—————————————————————————————————-
▶ 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 } } |