用Socket发送电子邮件

本文介绍了一个 C# 程序用于发送 HTML 格式的电子邮件,包括使用 System.Web.Mail 和 Socket 编程两种方式实现。该程序允许用户自定义发件人、收件人、邮件主题及内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
                
            }

        
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红火吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值