다음 유틸리티 클래스를 사용하여 전자 메일을 보내고 있습니다. 수신자 [email protected]이 존재하지 않거나 전자 메일이 한 가지 이유로 다른 사람에게 도달하지 않으면 내 응용 프로그램을 통보 받다.
smtp 클라이언트가 smtp 서버에 연결하지 못했을 때만 작동합니다.이 경우 예외가 있습니다. 그렇지 않은 경우 전자 메일이 클라이언트에 도달하지 못하는지를 확인하는 유일한 방법은 클라이언트 계정을 확인하는 것입니다.전자 메일이 수신자에게 도착하지 못한 경우를 확인하는 방법
public static class EmailUtil
{
/// <summary>
/// Fires exception if string is null or empty
/// </summary>
/// <param name="param">param value</param>
/// <param name="paramName">is the parameter name to be shown in the exception message</param>
private static void CheckStringParam(string parameter, string paramName)
{
if (String.IsNullOrEmpty(parameter))
{
throw new ArgumentException(String.Format("{0} can't be null or empty", paramName));
}
}
public static void SendEmail(EmailArgument emailArg)
{
CheckStringParam(emailArg.FromEmail, "emailArg.FromEmail");
CheckStringParam(emailArg.Subject, "emailArg.Subject");
CheckStringParam(emailArg.Body, "emailArg.Body");
string body = emailArg.Body;
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(emailArg.FromEmail);
foreach(string recipient in emailArg.ToEmails)
{
if (String.IsNullOrEmpty(recipient))
{
throw new ArgumentException("One of the values in the emailArg.ToEmails array is null or empty");
}
mailMsg.To.Add(new MailAddress(recipient));
}
mailMsg.IsBodyHtml = emailArg.IsHtml;
if (emailArg.PutHtmlTags)
{
body = String.Format("{0}" + body + "{1}", "<HTML><Body>", "</Body></HTML>");
}
mailMsg.Body = body;
mailMsg.BodyEncoding = emailArg.BodyEncoding;
// Get client info from the config file , it is tested with Web.config, need to be tested with App.config
SMTPConfiguration smtpConfig = (SMTPConfiguration)System.Configuration.ConfigurationManager.GetSection("SMTPConfigurationGroup/SMTPServer");
SmtpClient client = new SmtpClient(smtpConfig.Host, smtpConfig.Port);
client.EnableSsl = smtpConfig.EnableSSL;
client.Credentials = new System.Net.NetworkCredential(smtpConfig.Username, smtpConfig.Password);
// The notifications of failure are sent only to the client email.
// Exceptions are not guranteed to fire if the Receiver is invalid; for example gmail smtp server will fire exception only when [email protected] is not there.
// Exceptions will be fired if the server itself timeout or does not responding (due to connection or port problem, extra).
mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mailMsg);
}
}
주소를 기반으로 오류 코드를 반환하기 위해 프로토콜을 사용할 수있는 방법이 있는지 궁금합니다. – IrishChieftain
@IrishChieftain : 연결할 서버가 모를 가능성이 있습니다. 그것을 알기 위해서는 이메일 주소가있는 도메인의 SMTP 서버에 연결해야합니다. 그렇더라도 주소가 유효한지 서버가 알 수있는 것은 아닙니다. SMTP는 서버가 대상에 대한 모든 것을 알지 못하는 상태에서 작동하도록 설계되었습니다. 단지 더 많이 알아야하는 것으로 전달하기에 충분할 필요가 있습니다. –
그리고 대부분의 서버는 특정 SMTP 서버에서 주소가 유효하지 않은 것으로 알려진 경우에도 "OK"를 반환합니다. 또한 스팸에 대한 제한된 대응책이기도합니다. – JasonTrue