728x90
View
<input type="button" value="send" onclick="fn_SendEmail();"/>
<script>
function fn_SendEmail() {
if (confirm("Are you sure?")) {
$.ajax({
type: "post",
url: "SendEmail",
dataType: "json",
data: '',
success: function (data) {
alert("메일을 전송했습니다.");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("메일 전송에 실패했습니다.");
return false;
}
});
}
}
</script>
Controller
[HttpPost]
public JsonResult SendEmail()
{
int rs = 0;
string _host = "smtp.office365.com";
int _port = 587; //기본 smtp port : 25 , office365 : 587
string _userId = "userId"; //사용자 인증계정
string _userPassword = "password"; //사용자 인증 비밀번호
string fromEmail = "userId"; // 발송 메일 주소, Office365는 인증계정과 발송메일주소가 동일해야함;
string fromName = "Sanggeun"; // 발송인 정보
string to = "csgct@naver.com"; //수신 메일 주소
string cc = ""; //참조
string subject = "이메일 제목";
string body = "메일 본문입니다.";
bool IsHtml = false; //body가 html일 때 true
MailMessage message = SetMailMessage(fromEmail, fromName, to, cc, subject, body, IsHtml);
Email email = new Email(_host, _port, _userId, _userPassword, message);
email.Send();
return Json(new { RS = rs });
}
public MailMessage SetMailMessage(string fromEmail, string fromName, string to, string cc, string subject, string body, bool IsHtml)
{
MailMessage message = new MailMessage();
MailAddress address = new MailAddress(fromEmail, fromName);
message.From = address;
message.To.Add(to);
if (!string.IsNullOrEmpty(cc))
{
message.CC.Add(cc);
}
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = IsHtml;
return message;
}
Email.cs
public class Email
{
public string _host;
public int _port;
public string _userId;
public string _userPassword;
public MailMessage _mailMassage;
public Email()
{
this._host = ConfigurationManager.AppSettings["SmtpHost"];
this._port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]);
this._userId = string.IsNullOrEmpty(ConfigurationManager.AppSettings["SmtpUserId"]) ? "" : ConfigurationManager.AppSettings["SmtpUserId"];
this._userPassword = string.IsNullOrEmpty(ConfigurationManager.AppSettings["SmtpUserPassword"]) ? "" : ConfigurationManager.AppSettings["SmtpUserPassword"];
}
public Email(string _host, int _port, string _userId, string _userPassword, MailMessage _mailMassage)
{
this._host = _host;
this._port = _port;
this._userId = _userId;
this._userPassword = _userPassword;
this._mailMassage = _mailMassage;
}
public void Send()
{
try
{
using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(_host, _port))
{
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true; //ssl을 사용하여 암호화 할지 여부
smtp.Credentials = new System.Net.NetworkCredential(_userId, _userPassword);
smtp.Send(_mailMassage);
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString(), ex.InnerException);
}
finally
{
_mailMassage.Dispose();
}
}
}
'IT > ASP.NET' 카테고리의 다른 글
에러처리 (0) | 2022.07.01 |
---|---|
[C#] Compute SHA256 Hash with Salt (0) | 2021.11.01 |
[C#]SQL to Linq 변환 - "IN"절 사용 (0) | 2021.01.11 |
[MVC5] json 길이 변경 설정 (최대값) (0) | 2020.12.23 |