程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#編寫的一個郵件的發送類

C#編寫的一個郵件的發送類

編輯:C#入門知識

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!";
            }
        }
    }
}

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved