.net中SMTP的简单实现

本文详细介绍了SMTP协议中用于电子邮件发送的关键命令,包括HELO、MAIL FROM、RCPT TO、DATA及QUIT等,并通过示例代码展示了如何使用这些命令实现邮件发送。

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

                         SMTP命令
1.  。该命令用于启动发送主机和接收主机之间的通信。该命令附带一个标志主机的参数。随后,接收主机将自身的标志返回发送主机,这将使两台机器都进入了开始通信的就绪状态,如果成功,该命令返回250,这表明该命令已完成并没有出错。HELO
如:
Sending System: HELO wrox.com
Receiving System: 250 gg.mail.com
2.        该命令使接收系统能够指导邮件消息的发送者,如果在发送邮件消息时出错,则可以见错误消息发送给原始的电子邮件发送方。因为邮件在途中可以被多个主机截获,所以用该命令来标志电子邮件的最终目的地,大多数电子邮件服务器都在该参数上添加了一些限制,如确保用户的域名与电子邮件服务器的域相同,以此来防止匿名邮件和垃圾邮件。MAIL
如:
Sending System:MAIL FROM: noman@csquareonline.com
Receiving System: 250OK
3 .RCPT 利用该命令将用户的邮箱名称发送给邮件的接收方。对于一个特定的邮件消息,我们可以有多个接收者。
标志邮件消息的接受者:
Sending System:    RCPT TO:noman@tom.com
Receiving System: 250 OK
4. DATA 该命令指示随后的消息中包含消息文件的正文。 消息的结尾由发送系统所传输的一个行(该行只包含一个句点”.”)来指示。下面的程序清单显示了发送系统和接收系统之间的事务。注意:邮件的主题在该数据的内部指定,其方法为在发送DATA命令之后随即添加一个SUBJECT:行。SUBJECT:和回车/换行符之间的文本就构成了消息的主题:
Sending System:  DATA
Receiving System: 354 Ready to receive data…
Sending System:    SUBJECT:Subuect of the message<CR><LF> This is a test message.
<CR><LF>.<CR><LF>
Receiving System: 250 OK
5. QUIT 该命令指示发送系统已准备关闭发送方和接收方之间的通信连路。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.IO.IsolatedStorage;
namespace SMTPDemo
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class MyEmail : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label lbl_to;
  private System.Windows.Forms.TextBox txt_to;
  private System.Windows.Forms.Label lbl_from;
  private System.Windows.Forms.TextBox txt_from;
  private System.Windows.Forms.Label lbl_subject;
  private System.Windows.Forms.TextBox txt_subject;
  private System.Windows.Forms.Label lbl_content;
  private System.Windows.Forms.TextBox txt_content;
  private System.Windows.Forms.Button btn_send;
  private System.Windows.Forms.Button btn_cancel;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox txt_smtpServer;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.ListBox lst_status;
  private string receiveData;
  private string sendString;
  private byte[] dataToSend;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  public MyEmail()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();
   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.lbl_to = new System.Windows.Forms.Label();
   this.txt_to = new System.Windows.Forms.TextBox();
   this.lbl_from = new System.Windows.Forms.Label();
   this.txt_from = new System.Windows.Forms.TextBox();
   this.lbl_subject = new System.Windows.Forms.Label();
   this.txt_subject = new System.Windows.Forms.TextBox();
   this.lbl_content = new System.Windows.Forms.Label();
   this.txt_content = new System.Windows.Forms.TextBox();
   this.btn_send = new System.Windows.Forms.Button();
   this.btn_cancel = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.txt_smtpServer = new System.Windows.Forms.TextBox();
   this.label2 = new System.Windows.Forms.Label();
   this.lst_status = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   //
   // lbl_to
   //
   this.lbl_to.Location = new System.Drawing.Point(8, 40);
   this.lbl_to.Name = "lbl_to";
   this.lbl_to.TabIndex = 0;
   this.lbl_to.Text = "TO:";
   this.lbl_to.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // txt_to
   //
   this.txt_to.Location = new System.Drawing.Point(120, 48);
   this.txt_to.Name = "txt_to";
   this.txt_to.Size = new System.Drawing.Size(200, 20);
   this.txt_to.TabIndex = 1;
   this.txt_to.Text = "";
   //
   // lbl_from
   //
   this.lbl_from.Location = new System.Drawing.Point(8, 80);
   this.lbl_from.Name = "lbl_from";
   this.lbl_from.TabIndex = 2;
   this.lbl_from.Text = "FROM:";
   this.lbl_from.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // txt_from
   //
   this.txt_from.Location = new System.Drawing.Point(120, 88);
   this.txt_from.Name = "txt_from";
   this.txt_from.Size = new System.Drawing.Size(200, 20);
   this.txt_from.TabIndex = 3;
   this.txt_from.Text = "";
   //
   // lbl_subject
   //
   this.lbl_subject.Location = new System.Drawing.Point(8, 128);
   this.lbl_subject.Name = "lbl_subject";
   this.lbl_subject.TabIndex = 4;
   this.lbl_subject.Text = "SUBJECT:";
   this.lbl_subject.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // txt_subject
   //
   this.txt_subject.Location = new System.Drawing.Point(120, 128);
   this.txt_subject.Name = "txt_subject";
   this.txt_subject.Size = new System.Drawing.Size(200, 20);
   this.txt_subject.TabIndex = 5;
   this.txt_subject.Text = "";
   //
   // lbl_content
   //
   this.lbl_content.Location = new System.Drawing.Point(32, 160);
   this.lbl_content.Name = "lbl_content";
   this.lbl_content.TabIndex = 6;
   this.lbl_content.Text = "CONTENT:";
   this.lbl_content.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // txt_content
   //
   this.txt_content.AllowDrop = true;
   this.txt_content.Location = new System.Drawing.Point(16, 192);
   this.txt_content.Multiline = true;
   this.txt_content.Name = "txt_content";
   this.txt_content.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.txt_content.Size = new System.Drawing.Size(352, 48);
   this.txt_content.TabIndex = 7;
   this.txt_content.Text = "";
   //
   // btn_send
   //
   this.btn_send.Location = new System.Drawing.Point(88, 352);
   this.btn_send.Name = "btn_send";
   this.btn_send.TabIndex = 8;
   this.btn_send.Text = "Send";
   this.btn_send.Click += new System.EventHandler(this.btn_send_Click);
   //
   // btn_cancel
   //
   this.btn_cancel.Location = new System.Drawing.Point(208, 352);
   this.btn_cancel.Name = "btn_cancel";
   this.btn_cancel.TabIndex = 9;
   this.btn_cancel.Text = "Cancel";
   this.btn_cancel.Click += new System.EventHandler(this.btn_cancel_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 8);
   this.label1.Name = "label1";
   this.label1.TabIndex = 10;
   this.label1.Text = "SMTPSERVER:";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // txt_smtpServer
   //
   this.txt_smtpServer.Location = new System.Drawing.Point(120, 16);
   this.txt_smtpServer.Name = "txt_smtpServer";
   this.txt_smtpServer.Size = new System.Drawing.Size(200, 20);
   this.txt_smtpServer.TabIndex = 11;
   this.txt_smtpServer.Text = "";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(24, 248);
   this.label2.Name = "label2";
   this.label2.TabIndex = 12;
   this.label2.Text = "Status Message:";
   //
   // lst_status
   //
   this.lst_status.Location = new System.Drawing.Point(16, 272);
   this.lst_status.Name = "lst_status";
   this.lst_status.Size = new System.Drawing.Size(352, 69);
   this.lst_status.TabIndex = 13;
   //
   // MyEmail
   //
   this.AutoScale = false;
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(384, 382);
   this.Controls.Add(this.lst_status);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.txt_smtpServer);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.btn_cancel);
   this.Controls.Add(this.btn_send);
   this.Controls.Add(this.txt_content);
   this.Controls.Add(this.lbl_content);
   this.Controls.Add(this.txt_subject);
   this.Controls.Add(this.lbl_subject);
   this.Controls.Add(this.txt_from);
   this.Controls.Add(this.lbl_from);
   this.Controls.Add(this.txt_to);
   this.Controls.Add(this.lbl_to);
   this.Name = "MyEmail";
   this.Text = "MyOutLook";
   this.ResumeLayout(false);
  }
  #endregion
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new MyEmail());
  }
  private void btn_send_Click(object sender, System.EventArgs e)
  {
   TcpClient smtpServer=new TcpClient(this.txt_to.Text,25);
   this.lst_status.Items.Add("Connection Established with smtpserver.com");
   NetworkStream writeStream=smtpServer.GetStream();
   StreamReader readStream=new StreamReader(smtpServer.GetStream());
   receiveData=readStream.ReadLine();
   this.lst_status.Items.Add(receiveData);
   this.sendString="MAIL FROM:"+"<"+this.txt_from.Text+">/r/n";
   this.dataToSend=Encoding.ASCII.GetBytes(this.sendString);
   writeStream.Write(dataToSend,0,dataToSend.Length);
   receiveData=readStream.ReadLine();
   this.lst_status.Items.Add(receiveData);
   this.sendString="RCPT TO:"+"<"+this.txt_to.Text+">/r/n";
   dataToSend=Encoding.ASCII.GetBytes(this.sendString);
   writeStream.Write(dataToSend,0,dataToSend.Length);
   receiveData=readStream.ReadLine();
   this.lst_status.Items.Add(receiveData);
   this.sendString="DATA"+"/r/n";
   dataToSend=Encoding.ASCII.GetBytes(this.sendString);
   writeStream.Write(dataToSend,0,dataToSend.Length);
   receiveData=readStream.ReadLine();
   this.lst_status.Items.Add(receiveData);
   this.sendString="SUBJECT:"+this.txt_subject.Text+"/r/n"+this.txt_content.Text+"/r/n"+"."+"/r/n";
   dataToSend=Encoding.ASCII.GetBytes(this.sendString);
   writeStream.Write(dataToSend,0,dataToSend.Length);
   receiveData=readStream.ReadLine();
   this.lst_status.Items.Add(receiveData);
   this.sendString="QUIT"+"/r/n";
   dataToSend=Encoding.ASCII.GetBytes(this.sendString);
   writeStream.Write(dataToSend,0,dataToSend.Length);
   receiveData=readStream.ReadLine();
   this.lst_status.Items.Add(receiveData);
   writeStream.Close();
   readStream.Close();
   smtpServer.Close();
 
  }
  private void btn_cancel_Click(object sender, System.EventArgs e)
  {
 
  }
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值