using System;
using System.ComponentModel;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace Smtp
{
public class SmtpMail
{
private string toAddress = "";
private string mailUser = "";
private string userPassword = "";
private string displayName = "";
private string mailSubject = "";
private string sendMessage = "";
private int mailPort = 0;
private string mailHost = "";
public string Message
{
get
{
return sendMessage;
}
}
public SmtpMail(string to_address, string display_name,string mail_subject, string mail_user, string user_password,int mail_port,string mail_host)
{
toAddress = to_address;
displayName = display_name;
mailSubject = mail_subject;
mailUser = mail_user;
userPassword = user_password;
mailPort = mail_port;
mailHost = mail_host;
}
public void SendMail(string strBody)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(toAddress);
msg.From = new MailAddress(mailUser, displayName, System.Text.Encoding.UTF8);
msg.Subject = mailSubject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = strBody;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
msg.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential
(mailUser, userPassword);
client.Port = mailPort;
client.Host = mailHost;
client.EnableSsl = false;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
object userState = msg;
try
{
client.SendAsync(msg, userState);
}
catch (System.Net.Mail.SmtpException ex)
{
sendMessage = ex.ToString();
}
}
void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
MailMessage mail = (MailMessage)e.UserState;
string subject = mail.Subject;
if (e.Cancelled)
{
string cancelled = string.Format("[{0}] Send canceled.", subject);
sendMessage = cancelled;
}
if (e.Error != null)
{
string error = String.Format("[{0}] {1}", subject, e.Error.ToString());
sendMessage = error;
}
else
{
sendMessage = "Message has been sent successfully!";
}
}
}
}