.net c# socket 发送邮件(Demo)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace whsvc 
{
    namespace CMail
    {
        public class CMail
        {
            private String servername, body, attachpath, from_address, to_address, subject, encdoing = "GB2312";
            private MailMessage message = null;
            private bool isBcc = false;
            private SmtpClient client = null;
            private String username, password;
             public String Username
            {
                set { this.username = value; }
                get { return this.username; }
            }
            public String Password
            {
                set { this.password = value; }
                get { return this.password; }
            }
           public String Body
            {
                set { this.body = value; }
                get { return this.body; }
            }
            public String Server
            {
                set { this.servername = value; }
                get { return this.servername; }
            }
            public String FileName
            {
                set { this.attachpath = value; }
                get { return this.attachpath; }
            }
            public String From
            {
                set { this.from_address = value; }
                get { return this.from_address; }
            }
            public String To
            {
                set { this.to_address = value; }
                get { return this.to_address; }
            }
            public String Subject
            {
                set { this.subject = value; }
                get { return this.subject; }
            }
            public String Encoding
            {
                set { this.encdoing = value; }
                get { return this.encdoing; }
            }


            public CMail(String username, String password, String servername)
            {
                this.username = username;
                this.password = password;
                this.servername = servername;
            }
            public CMail()
            {

            }
            private bool parse()
            {
                bool success = false;
                if (from_address.Length != 0)
                {
                    if (to_address.Length != 0)
                    {
                        message = new MailMessage(this.from_address, this.to_address, subject, body);
                        message.BodyEncoding = System.Text.Encoding.Default;
                        if (this.attachpath != null) 
                        {
                            if (this.attachpath.Length != 0)
                            {
                                try
                                {
                                    message.Attachments.Add(new Attachment(this.attachpath));
                                }
                                catch (Exception e)
                                {
                                    throw new Exception(e.Message);
                                }
                            }
                        
                        }
                        message.Priority = MailPriority.Normal;
                        if (this.servername.Length != 0)
                        {
                            client = new SmtpClient(this.servername);
                            if (this.username.Length != 0 && this.password.Length != 0)
                            {
                                client.Credentials = new NetworkCredential(this.username, this.password);
                                success = true;
                            }
                            else
                            {
                                throw new Exception("用户名或密码不能为空");
                            }
                        }
                        else
                        {
                            throw new Exception("服务器不能为空");
                        }
                    }
                    else
                    {
                        throw new Exception("收件人不能为空");
                    }
                }
                else
                {
                    throw new Exception("发件人不能为空");
                }
                return success;
            }
            public void send()
            {
                if (parse())
                {
                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
        }
    }
}

  小孟

你可能感兴趣的:(C++,c,.net,socket,C#)