.net c# socket 发送邮件(Demo)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > .net c# socket 发送邮件(Demo)

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

 2010/11/16 11:46:00  mengdejun  http://mengdejun.javaeye.com  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Mail;usingSystem.Text;namespacewhsvc{namespaceCMail{publicclassCMail{privateStringservername,body,attachpath,from_address,to_address,subject
  • 标签:c# socket 发送邮件
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);
                    }
                }
            }
        }
    }
}

? 小孟

发表评论
用户名: 匿名