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

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:22   1867   0
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);
                    }
                }
            }
        }
    }
}

小孟

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP