Thursday, January 18, 2007

How to enable SMTP Authentication using System.Web.Mail

Active Directory에서 그룹 소속원에게 메일을 보낼 수 있다.
하지만 외부에 메일 주소가 노출되면 스팸에 시달리는 것은 시간문제다.

그래서 메일 서버의 그룹 정보에 "인증필요"로 해서 외부의 메일은 거부하게 한다.

여러 시스템이 존재하는 경우 다른 서버에서 메일을 보내오는 경우가 있는 데
이 경우면 기존 설정에 위배되기 때문에 문제가 발생한다.

해서 인증을 넣어 메일을 보내야 한다.(여기서 인증은 아웃룩 쓸때 암호를 넣는 것과 같다.)

C#의 경우
using System;using System.Web.Mail;namespace SMTPAuthentication{ public class SMTPAuthenticationExample {
public static void SendMail()
{
string smtpServer = "smtp.domain.com";
string userName = "johnDoe";
string password = "pass";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
MailMessage msg = new MailMessage();
if (userName.Length > 0) {
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25) ;
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort) ;
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
msg.To = "someone@domain.com";
msg.From = "me@domain.com";
msg.Subject = "Subject";
msg.Body = "Message";
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg); } }}

Telnet의 경우
꼭 아이디와 비번은 Base64로 인코딩해야 한다.
220 ALAN.CP.com Microsoft ESMTP MAIL Service
ehlo
250 ALAN.CP.com Hello [10.10.1.1]
more... verbs
250 OK
auth login
334 VXNlcm5hbWU6
Z3V5dA==
334 UGFzc3dvcmQ6
UGaF1bGluZTEz
235 2.7.0 Authentication successful.

mail from: guyt@exchguy.com
250 2.1.0 guyt@exchguy.com....Sender OK
rcpt to: administrator@cp.com
250 2.1.5 administrator@cp.com
data
354 Start mail input; end with .
Please send cheque soonest
.
250 2.6.0 Queued mail for delivery

PL/SQL의 경우 구글 검색 결과
utl_smtp.helo(l_connection, 'ALAN.CP.com');

-- SMTP AUTH Login
utl_smtp.command ( l_connection, 'ehlo' );
utl_smtp.command ( l_connection, 'auth login' );
utl_smtp.command ( l_connection, utl_raw.cast_to_varchar2 (utl_encode.base64_encode@hddrts (utl_raw.cast_to_raw ( v_username ))));
utl_smtp.command ( l_connection,utl_raw.cast_to_varchar2 (utl_encode.base64_encode@hddrts (utl_raw.cast_to_raw ( v_password ))));

utl_smtp.mail(l_connection, p_from);
utl_smtp.rcpt(l_connection, p_to);

그 외는 검색하면 나올 것이다.

No comments: