用Socket发送电子邮件

    技术2022-05-11  83

    using  System; using  System.Web; using  System.Web.Mail; using  System.Text; using  System.Net; using  System.Net.Sockets; namespace  Email {    /// <summary>    /// Class1 的摘要说明。    /// </summary>    public class SendEmail    {        public string FromEmail="";        public string ToEmail="";        public string Title="";        public string Content="";        public string smtpserver="";        public string errorstr="";             public SendEmail()        {                            }        public SendEmail(string _FromEmail,string _ToEmail,string _Title,string _Content, string _smtpserver)        {            FromEmail=_FromEmail;            ToEmail=_ToEmail;            Title=_Title;            Content=_Content;            smtpserver=_smtpserver;        }        public bool send()        {                        bool flg;            MailMessage Message=null;            Message = new MailMessage();            Message.BodyEncoding = Encoding.Default;            Message.BodyFormat = MailFormat.Html;            Message.Subject=Title;            Message.From = FromEmail;                        Message.To = ToEmail;            Message.Body  =Content.ToString();            try            {                            SmtpMail.SmtpServer = smtpserver;                                SmtpMail.Send(Message);                flg=true;                    }            catch(Exception e)            {                errorstr=e.ToString();                flg=false;            }                return flg;        }         public bool send2()  //发送电子邮件        {            bool flg;            Socket s = null;            IPAddress Ip;            IPEndPoint hostEndPoint;                        string sIp=smtpserver;            int conPort = 25;            string Cmd1="", Cmd2="", Cmd3="", Cmd4="", Cmd5="",  Cmd6="",  Cmd7="";                        Byte[] PostBytes = new Byte[128];            Byte[] RecvBytes = new Byte[256];            string strRetPage = null;            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            Ip = IPAddress.Parse(sIp);             hostEndPoint = new IPEndPoint(Ip,conPort);            Cmd1="Helo ";            Cmd2="mail from:"+ FromEmail +" ";                        Cmd3="rcpt to:"+ToEmail +" ";            Cmd4="data ";            string KK;            KK="MIME-Version: 1.0"+" ";            KK+="Content-Type: multipart/alternative;"+" ";            KK+="    boundary="----=_NextPart_000_000D_01C5B928.9630AD10""+" ";            KK+="X-Mailer: Microsoft CDO for Windows 2000"+" ";            KK+="Content-Class: urn:content-classes:message"+" ";            KK+="X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506"+" ";            KK+=" ";            KK+="------=_NextPart_000_000D_01C5B928.9630AD10"+" ";            KK+="Content-Type: text/html;"+" ";            KK+="    charset="gb2312""+" ";            KK+=" ";            KK+=Content+" ";                KK+=" ";            KK+="------=_NextPart_000_000D_01C5B928.9630AD10--"+" ";                        KK=KK.Replace(" . ",".");            string KK2;            KK2="From: <"+FromEmail+">"+" ";            KK2+="To: <"+ToEmail+">"+" ";            Cmd5=KK2+"Subject:"+Title+" "+KK;                        Cmd6=" . ";            Cmd7="quit ";                                s.Connect(hostEndPoint);            if (!s.Connected)            {                                    flg=false;            }            Encoding ASCII = Encoding.ASCII;            Encoding DEFAU = Encoding.Default;            try            {                PostBytes =  ASCII.GetBytes(Cmd1);                s.Send(PostBytes, PostBytes.Length, 0);                    Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);                strRetPage = DEFAU.GetString(RecvBytes, 0, bytes);                        PostBytes =  ASCII.GetBytes(Cmd2);                s.Send(PostBytes, PostBytes.Length, 0);                bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);                strRetPage = DEFAU.GetString(RecvBytes, 0, bytes);                PostBytes =  ASCII.GetBytes(Cmd3);                s.Send(PostBytes, PostBytes.Length, 0);                bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);                strRetPage = DEFAU.GetString(RecvBytes, 0, bytes);                PostBytes =  ASCII.GetBytes(Cmd4);                s.Send(PostBytes, PostBytes.Length, 0);                bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);                strRetPage = DEFAU.GetString(RecvBytes, 0, bytes);                PostBytes = DEFAU.GetBytes(Cmd5);                s.Send(PostBytes, PostBytes.Length, 0);                                            PostBytes =  ASCII.GetBytes(Cmd6);                s.Send(PostBytes, PostBytes.Length, 0);                bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);                strRetPage = DEFAU.GetString(RecvBytes, 0, bytes);                PostBytes =  ASCII.GetBytes(Cmd7);                s.Send(PostBytes, PostBytes.Length, 0);                bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);                strRetPage = DEFAU.GetString(RecvBytes, 0, bytes);                s.Close();                flg=true;            }            catch(Exception e)            {                errorstr=e.ToString();                flg=false;            }            return flg;        }    }}

     

     

    调用方法:

     

    using  System; using  System.Drawing; using  System.Collections; using  System.ComponentModel; using  System.Windows.Forms; using  System.Data; namespace  testMail {    /// <summary>    /// Form1 的摘要说明。    /// </summary>    public class Form1 : System.Windows.Forms.Form    {        private System.Windows.Forms.TextBox textBox1;        private System.Windows.Forms.TextBox textBox2;        private System.Windows.Forms.Label label1;        private System.Windows.Forms.Label label2;        private System.Windows.Forms.TextBox textBox4;        private System.Windows.Forms.Label label3;        private System.Windows.Forms.Label label4;        private System.Windows.Forms.TextBox textBox5;        private System.Windows.Forms.Label label5;        private System.Windows.Forms.Label label6;        private System.Windows.Forms.Button button2;        private System.Windows.Forms.TextBox textBox3;        private System.Windows.Forms.RadioButton radioButton1;        private System.Windows.Forms.RadioButton radioButton2;        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.Container components = null;        public Form1()        {            //            // Windows 窗体设计器支持所必需的            //            InitializeComponent();            //            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码            //        }        /// <summary>        /// 清理所有正在使用的资源。        /// </summary>        protected override void Dispose( bool disposing )        {            if( disposing )            {                if (components != null)                 {                    components.Dispose();                }            }            base.Dispose( disposing );        }        Windows 窗体设计器生成的代码        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()         {            Application.Run(new Form1());        }        private void button2_Click(object sender, System.EventArgs e)        {            Email.SendEmail a=new Email.SendEmail();            a.FromEmail=textBox1.Text;            a.ToEmail=textBox4.Text;            a.smtpserver=textBox2.Text;            a.Title=textBox5.Text;            a.Content=textBox3.Text;            bool k;            string kkkk;            if (radioButton1.Checked==true)            {                k=a.send();                kkkk="组件方式";                            }            else            {                kkkk="Socket方式";                k=a.send2();            }            if (k)            {                label6.Text=kkkk+"发送邮件到"+textBox4.Text+"成功!";            }             else            {                label6.Text="失败!,失败原因:"+a.errorstr.ToString();                            }                }    }}

    最新回复(0)