Just in Chronicles

Life as a Voyage

Posts Tagged ‘Anti-spam Relay

Mail Sending Failure with System.Net.Mail.SmtpClient.Send()

References:

While sending e-mails using System.Net.Mail.SmtpClient object, developers often meet this error. This occurs because the SMTP server requires authentication to avoid spam relay. The error message is like:

Message: Mailbox unavailable. The server response was: <mailaddress@mailserviceprovider.com> No such user here

Basically, all we need to do before sending e-mails using web applications is giving the SMTP server security by authenticating a valid user. In order to do so, generally the following method is used.

...
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("SMTPServerURL");
smtp.UseDefaultCredentials = true;
smtp.Send(message);
...

The UseDefaultCredentials property works in most cases. So, we don’t need put extra code lines for authentication. However, depending on SMTP server settings, the UseDefaultCredentials property won’t work at all. In this case, we have to adopt another method for authentication.

...
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("SMTPServerURL");
smtp.Credentials = new System.Net.NetworkCredential("Username", "Password");
smtp.Send(message);
...

Like above, System.Net.NetworkCredential class gives the System.Net.Mail.SmtpClient authentication.

By using this method, mail sending failure message doesn’t occur any longer.


닷넷라이브러리의 System.Net.Mail.SmtpClient 클라스를 통해 이메일을 보내는 경우에 위와 같은 에러가 나는 경우가 있다. 이런 에러는 보통 사용하고 있는 SMTP 서버가 스팸 릴레이를 방지하기 위해 사용자 인증을 요구하는 경우에 나타나는데, 이럴 땐 별다른 방법이 없다. 사용자 인증을 해주면 된다.

사용자 인증을 하는 방법은 크게 두가지가 있다. 첫번째 방법이 보통 가장 일반적인 방법으로 거의 모든 경우에서 쓰이고, 그 방법이 안먹히면 두번째로 직접 사용자 인증을 해주는 방법이 있다.

위의 예제코드를 보면 대략 이해가 갈 듯.