c#비동기적으로 메시지를 보내는 클래스

5923 단어
먼저 메일 정보의 기본 클래스를 다음과 같이 정의해야 한다.
 
  
///
/// Base message class used for emails
///

public class Message
{
#region Constructor
///
/// Constructor
///

public Message()
{
}
#endregion

#region Properties
///


/// Whom the message is to
///

public virtual string To { get; set; }

///


/// The subject of the email
///

public virtual string Subject { get; set; }

///


/// Whom the message is from
///

public virtual string From { get; set; }

///


/// Body of the text
///

public virtual string Body { get; set; }

#endregion
}


그리고 메일의 발송 클래스를 정의하고 Netmail 방식으로 메일을 발송하며 메일을 발송할 때 사용합니다.net에서 자체적으로 가지고 있는 스레드 탱크는 다중 스레드를 통해 비동기적으로 발송되며 코드는 다음과 같다.
 
  
 ///
/// Utility for sending an email
///

public class EmailSender : Message
{
#region Constructors

///


/// Default Constructor
///

public EmailSender()
{
Attachments = new List();
EmbeddedResources = new List();
Priority = MailPriority.Normal;
}

#endregion

#region Public Functions

///


/// Sends an email
///

/// The body of the message
public void SendMail(string Message)
{
Body = Message;
SendMail();
}

///


/// Sends a piece of mail asynchronous
///

/// Message to be sent
public void SendMailAsync(string Message)
{
Body = Message;
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}

///


/// Sends an email
///

public void SendMail()
{
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
{
char[] Splitter = { ',', ';' };
string[] AddressCollection = To.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if(!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.To.Add(AddressCollection[x]);
}
if (!string.IsNullOrEmpty(CC))
{
AddressCollection = CC.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if (!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.CC.Add(AddressCollection[x]);
}
}
if (!string.IsNullOrEmpty(Bcc))
{
AddressCollection = Bcc.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if (!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.Bcc.Add(AddressCollection[x]);
}
}
message.Subject = Subject;
message.From = new System.Net.Mail.MailAddress((From));
AlternateView BodyView = AlternateView.CreateAlternateViewFromString(Body, null, MediaTypeNames.Text.Html);
foreach (LinkedResource Resource in EmbeddedResources)
{
BodyView.LinkedResources.Add(Resource);
}
message.AlternateViews.Add(BodyView);
//message.Body = Body;
message.Priority = Priority;
message.SubjectEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
message.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
message.IsBodyHtml = true;
foreach (Attachment TempAttachment in Attachments)
{
message.Attachments.Add(TempAttachment);
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Server, Port);
if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
{
smtp.Credentials = new System.Net.NetworkCredential(UserName, Password);
}
if (UseSSL)
smtp.EnableSsl = true;
else
smtp.EnableSsl = false;
smtp.Send(message);
}
}

///


/// Sends a piece of mail asynchronous
///

public void SendMailAsync()
{
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}

#endregion

#region Properties

///


/// Any attachments that are included with this
/// message.
///

public List Attachments { get; set; }

///


/// Any attachment (usually images) that need to be embedded in the message
///

public List EmbeddedResources { get; set; }

///


/// The priority of this message
///

public MailPriority Priority { get; set; }

///


/// Server Location
///

public string Server { get; set; }

///


/// User Name for the server
///

public string UserName { get; set; }

///


/// Password for the server
///

public string Password { get; set; }

///


/// Port to send the information on
///

public int Port { get; set; }

///


/// Decides whether we are using STARTTLS (SSL) or not
///

public bool UseSSL { get; set; }

///


/// Carbon copy send (seperate email addresses with a comma)
///

public string CC { get; set; }

///


/// Blind carbon copy send (seperate email addresses with a comma)
///

public string Bcc { get; set; }

#endregion
}

좋은 웹페이지 즐겨찾기