ASP.NET 웹 페이지 - 웹메일 도우미


WebMail 도우미 - 많은 유용한 ASP.NET 웹 도우미 중 하나입니다.

WebMail 개체를 사용하면 웹 페이지에서 이메일을 쉽게 보낼 수 있습니다.


웹메일 도우미

WebMail Helper를 사용하면 SMTP(Simple Mail Transfer Protocol)를 사용하여 웹 응용 프로그램에서 이메일을 쉽게 보낼 수 있습니다.


시나리오: 이메일 지원

이메일 사용을 시연하기 위해 지원을 위한 입력 페이지를 만들고 사용자가 해당 페이지를 다른 페이지에 제출하도록 하고 지원 문제에 대한 이메일을 보낼 것입니다.


첫 번째: AppStart 페이지 편집

이 자습서에서 데모 응용 프로그램을 빌드했다면 이미 다음 콘텐츠가 포함된 _AppStart.cshtml이라는 페이지가 있습니다.

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}

WebMail 도우미를 시작하려면 AppStart 페이지에 다음 WebMail 속성을 추가하십시오.

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password-goes-here";
WebMail.From = "[email protected]";

}

속성 설명:

SmtpServer: 이메일을 보내는 데 사용할 SMTP 서버의 이름입니다.

SmtpPort: 서버가 SMTP 트랜잭션(이메일)을 보내는 데 사용할 포트입니다.

EnableSsl: 서버가 SSL(Secure Socket Layer) 암호화를 사용해야 하는 경우 True입니다.

UserName: 이메일을 보내는 데 사용되는 SMTP 이메일 계정의 이름입니다.

비밀번호: SMTP 이메일 계정의 비밀번호입니다.

보낸 사람: 보낸 사람 주소에 표시할 이메일입니다(종종 UserName과 동일).



두 번째: 이메일 입력 페이지 생성

그런 다음 입력 페이지를 만들고 이름을 Email_Input으로 지정합니다.

Email_Input.cshtml

<!DOCTYPE html>
<html>
<body>
<h1>Request for Assistance</h1>

<form method="post" action="EmailSend.cshtml">
<label>Username:</label>
<input type="text" name="customerEmail" />
<label>Details about the problem:</label>
<textarea name="customerRequest" cols="45" rows="4"></textarea>
<p><input type="submit" value="Submit" /></p>
</form>

</body>
</html>

입력 페이지의 목적은 정보를 수집한 다음 정보를 이메일로 보낼 수 있는 새 페이지에 데이터를 제출하는 것입니다.


세 번째: 이메일 보내기 페이지 만들기

그런 다음 이메일을 보내는 데 사용할 페이지를 만들고 이름을 Email_Send로 지정합니다.

Email_Send.cshtml

@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:"[email protected]", subject: "Help request from - " + customerEmail, body: customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}

WebMail 개체 참조 - 속성

Properties Description
SmtpServer The name the SMTP server that will send the emails
SmtpPort The port the server will use to send SMTP emails
EnableSsl True, if the server should use SSL encryption
UserName The name of the SMTP account used to send the email
Password The password of the SMTP account
From The email to appear in the from address

WebMail 개체 참조 - 메서드

Method Description
Send() Sends an email message to an SMTP server for delivery

Send() 메서드에는 다음 매개 변수가 있습니다.

Parameter Type Description
to String The Email recipients (separated by semicolon)
subject String The subject line
body String The body of the message

그리고 다음과 같은 선택적 매개변수:

Parameter Type Description
from String The email of the sender
cc String The cc emails (separated by semicolon)
filesToAttach Collection Filenames
isBodyHtml Boolean True if the email body is in HTML
additionalHeaders Collection Additional headers

기술 데이터

Name Value
Class System.Web.Helpers.WebMail
Namespace System.Web.Helpers
Assembly System.Web.Helpers.dll

웹메일 도우미 초기화

WebMail 도우미를 사용하려면 SMTP 서버에 액세스해야 합니다. SMTP는 이메일의 "출력" 부분입니다. 웹 호스트를 사용하는 경우 이미 SMTP 서버의 이름을 알고 있을 것입니다. 회사 네트워크에서 작업하는 경우 IT 부서에서 이름을 알려줄 수 있습니다. 집에서 일하는 경우 일반 이메일 공급자를 사용할 수 있습니다.

 이메일을 보내려면 다음이 필요합니다.

  • SMTP 서버의 이름
  • 포트 번호(대부분 25)
  • 이메일 사용자 이름
  • 이메일 비밀번호

웹 루트에서 _AppStart.cshtml 이라는 페이지를 생성(또는 편집)합니다 .

파일 안에 다음 코드를 넣습니다.

_AppStart.cshtml

@{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password";
WebMail.From = "[email protected]"
}

위의 코드는 웹 사이트(응용 프로그램)가 시작될 때마다 실행됩니다. WebMail 개체 에 초기 값을 제공합니다.

대체하십시오:

smtp.example.com 을 이메일을 보내는 데 사용할 SMTP 서버 이름으로 바꿉니다.

25 는 서버가 SMTP 트랜잭션(이메일)을 보내는 데 사용할 포트 번호입니다.

서버가 SSL(Secure Socket Layer) 암호화를 사용해야 하는 경우 true이고 false 입니다.

[email protected] 은 이메일을 보내는 데 사용되는 SMTP 이메일 계정의 이름입니다.

SMTP 이메일 계정의 비밀번호가 있는 비밀번호입니다 .

보낸 사람 주소에 표시할 이메일과 함께 john@example .

AppStart 파일에서 WebMail 개체 를 시작할 필요는 없지만 WebMail.Send() 메서드 를 호출하기 전에 이러한 속성을 설정해야 합니다 .