C#

[C#] Mail 보내기

Jcoder 2021. 5. 4. 14:56
using MailMessage msg = new MailMessage("xxxx@naver.com", "xxxx@naver.com", "제목", "내용");
//msg.IsBodyHtml = true; //본문이 HTML 인 경우

// SmtpClient
using (var smtp = new SmtpClient())
{
	// 셋업(SMTP 서버, 포트)
	smtp.Host = "smtp.naver.com";
	smtp.Port = 587;
	smtp.EnableSsl = true; // SSL 사용
	smtp.SendCompleted += (s, e1) => {
		if (string.IsNullOrEmpty($"{e1.Error}"))
			MessageBox.Show($"메일 발송 성공", "성공", MessageBoxButtons.OK, MessageBoxIcon.Information);
		else
			MessageBox.Show($"{e1.Cancelled}, {e1.Error}, {e1.UserState}", "실패", MessageBoxButtons.OK, MessageBoxIcon.Error);
	};

	// 아웃룩, Live 또는 Hotmail의 계정과 암호를 지정
	smtp.Credentials = new NetworkCredential("계정", "비밀번호");
	// 메일 발송
	await smtp.SendMailAsync(msg);
}