C # A mail sending class written

Source: Internet
Author: User
Tags mailmessage smtpclient

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 canceled = string. Format ("[{0}] Send canceled.", subject );
SendMessage = canceled;
}
If (e. Error! = Null)
{
String error = String. Format ("[{0}] {1}", subject, e. Error. ToString ());
SendMessage = error;
}
Else
{
SendMessage = "Message has been sent successfully! ";
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.