✉️ C#으로 SMTP를 이용한 HTML 이메일 발송하기
- 📕 Programing/C#
- 2025. 3. 25.
C#을 사용해서 SMTP 방식으로 HTML 형식의 이메일을 발송하는 방법을 소개해드리겠습니다.
✅ SMTP란?
SMTP(Simple Mail Transfer Protocol)는 이메일을 전송하기 위한 표준 프로토콜입니다.
C#에서는 System.Net.Mail 네임스페이스를 통해 SMTP 서버를 사용하여 이메일을 쉽게 전송할 수 있습니다.
🛠️ 개발 환경
- .NET Framework 또는 .NET Core (버전에 상관없이 사용 가능)
- C# (콘솔 프로젝트로 구현)
- SMTP 서버 정보 (예: Gmail SMTP, 회사 메일 서버 등)
📄 HTML 이메일 발송 코드
아래는 C#에서 SMTP를 사용해 HTML 형식의 이메일을 보내는 코드입니다.
using System;
using System.Net;
using System.Net.Mail;
class Program
{
static void Main(string[] args)
{
// SMTP 서버 설정
string smtpAddress = "smtp.yourserver.com"; // 예: smtp.gmail.com
int portNumber = 587; // 보통 587 (TLS) 또는 465 (SSL)
bool enableSSL = true;
// 보내는 사람 정보
string emailFrom = "your_email@example.com";
string password = "your_password";
// 받는 사람 정보
string emailTo = "recipient@example.com";
string subject = "이것은 HTML 이메일입니다 ✨";
// HTML 본문 내용
string body = @"
<html>
<body>
<h2 style='color: navy;'>안녕하세요!</h2>
<p>이 메일은 <strong>C# SMTP</strong>를 통해 <span style='color: green;'>HTML 형식</span>으로 전송된 것입니다.</p>
<p>감사합니다 😊</p>
</body>
</html>";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true; // HTML 형식으로 전송
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
try
{
smtp.Send(mail);
Console.WriteLine("HTML 메일 전송 성공!");
}
catch (Exception ex)
{
Console.WriteLine("메일 전송 실패: " + ex.Message);
}
}
}
}
}
✅ 첨부파일 추가 방법
mail.Attachments.Add(new Attachment("파일경로"));
이 코드 한 줄이면 끝이에요!
📦 첨부파일 포함한 전체 예제
using System;
using System.Net;
using System.Net.Mail;
class Program
{
static void Main(string[] args)
{
// SMTP 서버 설정
string smtpAddress = "smtp.yourserver.com";
int portNumber = 587;
bool enableSSL = true;
// 보내는 사람 정보
string emailFrom = "your_email@example.com";
string password = "your_password";
// 받는 사람 정보
string emailTo = "recipient@example.com";
string subject = "HTML 메일 + 첨부파일";
string body = @"
<html>
<body>
<h3>안녕하세요!</h3>
<p>첨부파일이 포함된 HTML 메일입니다.</p>
</body>
</html>";
// 첨부파일 경로
string attachmentPath = @"C:\Users\yourname\Documents\sample.pdf";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// 첨부파일 추가
Attachment attachment = new Attachment(attachmentPath);
mail.Attachments.Add(attachment);
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
try
{
smtp.Send(mail);
Console.WriteLine("첨부파일 포함 메일 전송 성공!");
}
catch (Exception ex)
{
Console.WriteLine("메일 전송 실패: " + ex.Message);
}
}
}
}
}
⚠️ 주의할 점
- 첨부할 파일 경로는 전체 경로여야 해요 (상대 경로도 가능하긴 함).
- 파일이 존재하지 않거나 권한이 없으면 예외가 발생하니, 예외 처리를 잘 해두는 게 좋아요.
- 여러 개의 첨부파일도 가능합니다:
mail.Attachments.Add(new Attachment("파일1경로"));
mail.Attachments.Add(new Attachment("파일2경로"));
'📕 Programing > C#' 카테고리의 다른 글
프로퍼티 (0) | 2025.03.23 |
---|---|
[개념] 박싱(Boxing)과 언박싱(Unboxing) (0) | 2025.03.15 |
CLR (Common Language Runtime) 이란? (1) | 2025.03.14 |
WinForm) ControlBox (0) | 2024.10.27 |
JObject의 key 값을 추출하는 방법 (0) | 2024.09.30 |