.NetFramwork 2.0에서 추가 된 내용으로 기존의 WebMail방식이 바뀌었다.
SmtpClient 클래스는 배달용 SMTP 서버로 전자 메일을 보내는 데 사용됩니다. 다음 표에서 보여 주는 클래스는 SmtpClient를 사용하여 보낼 수 있는 전자 메일 메시지를 생성하는 데 사용됩니다.
이 클래스를 사용하여 메일을 보내기 위해서는 기본적으로 IIS에 SMTP 가상 서버가 구성되어 있어야한다. 안되어 있을시 메일을 보내고자 할 경우에는 오류가 발생한다.
IIS 6.0에서 SMTP 가사 서버 설치 및 구성 방법을 제시한다. ==> http://msdn.microsoft.com/ko-kr/library/8b83ac7t.aspx
[예제코드]
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
public class SimpleAsynchronousExample
{
static bool mailSent = false;
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
} else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
public static void Main(string[] args)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(args[0]);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("ben@contoso.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
}
SmtpClient 클래스
참고: 이 클래스는 .NET Framework 버전 2.0에서 새로 추가되었습니다.
응용 프로그램에서 SMTP(Simple Mail Transfer Protocol)를 사용하여 전자 메일을 보낼 수 있도록 합니다.
네임스페이스: System.Net.Mail
어셈블리: System(system.dll)
SmtpClient 클래스는 배달용 SMTP 서버로 전자 메일을 보내는 데 사용됩니다. 다음 표에서 보여 주는 클래스는 SmtpClient를 사용하여 보낼 수 있는 전자 메일 메시지를 생성하는 데 사용됩니다.
클래스 |
설명 |
SmtpClient |
응용 프로그램에서 SMTP를 사용하여 전자메일을 보낼 수 있도록 합니다. |
MailMessage |
전자 메일 메시지를 나타냅니다. |
MailAddress |
보낸 사람 및 받는 사람의 전자 메일 주소를 나타냅니다. |
Attachment |
첨부 파일을 나타냅니다. 이 클래스를 사용하면 전자 메일 메시지에 파일, 스트림 또는 텍스트를 첨부할 수 있습니다. |
AlternateView |
전자메일 메시지를 보기 위한 대체 형식을 나타냅니다. 이 클래스를 사용하면 텍스트버전의 전자메일을 제공할 수 있습니다. 이것은 HTML 콘텐츠를 표시할 수 없는 전자메일 프로그램을 사용하는 사용자에게 유용합니다. |
이 클래스를 사용하여 메일을 보내기 위해서는 기본적으로 IIS에 SMTP 가상 서버가 구성되어 있어야한다. 안되어 있을시 메일을 보내고자 할 경우에는 오류가 발생한다.
IIS 6.0에서 SMTP 가사 서버 설치 및 구성 방법을 제시한다. ==> http://msdn.microsoft.com/ko-kr/library/8b83ac7t.aspx
[예제코드]
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
public class SimpleAsynchronousExample
{
static bool mailSent = false;
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
} else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
public static void Main(string[] args)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(args[0]);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("ben@contoso.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
}