C#使用SMTP发送电子邮件

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;
 
namespace EMailSender
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <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 );
}
 
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(56, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 0;
this.label1.Text = "From:";
// 
// textBox1
// 
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox1.Location = new System.Drawing.Point(96, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
// 
// label2
// 
this.label2.Location = new System.Drawing.Point(56, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 16);
this.label2.TabIndex = 2;
this.label2.Text = "To:";
// 
// textBox2
// 
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox2.Location = new System.Drawing.Point(96, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
// 
// label3
// 
this.label3.Location = new System.Drawing.Point(32, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 24);
this.label3.TabIndex = 4;
this.label3.Text = "Subject:";
// 
// textBox3
// 
this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox3.Location = new System.Drawing.Point(96, 80);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(168, 21);
this.textBox3.TabIndex = 5;
this.textBox3.Text = "";
// 
// label4
// 
this.label4.Location = new System.Drawing.Point(16, 112);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 32);
this.label4.TabIndex = 6;
this.label4.Text = "SMTP Server:";
// 
// textBox4
// 
this.textBox4.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox4.Location = new System.Drawing.Point(96, 112);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(168, 21);
this.textBox4.TabIndex = 7;
this.textBox4.Text = "";
// 
// textBox5
// 
this.textBox5.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox5.Location = new System.Drawing.Point(8, 192);
this.textBox5.Multiline = true;
this.textBox5.Name = "textBox5";
this.textBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox5.Size = new System.Drawing.Size(328, 168);
this.textBox5.TabIndex = 9;
this.textBox5.Text = "";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(96, 160);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 10;
this.button1.Text = "Send";
this.button1.Click += new System.EventHandler(this.SendBtn_click);
// 
// button2
// 
this.button2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button2.Location = new System.Drawing.Point(184, 160);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 11;
this.button2.Text = "Exit";
this.button2.Click += new System.EventHandler(this.ExitBtn_click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 374);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
 this.button2,
 this.button1,
 this.textBox5,
 this.textBox4,
 this.label4,
 this.textBox3,
 this.label3,
 this.textBox2,
 this.label2,
 this.textBox1,
 this.label1});
this.Name = "Form1";
this.Text = "EmailSender";
this.ResumeLayout(false);
 
}
#endregion
 
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
 
private void SendBtn_click(object sender, System.EventArgs e)
{
TcpClient tosmtpserver=new TcpClient(textBox4.Text,25);
string SendString;
byte[] BufferString;
NetworkStream networkstream= tosmtpserver.GetStream();
StreamReader streamreader=new StreamReader(networkstream);
try
{
SendString="HELLO Server "+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
 
SendString="MAIL FROM: "+" < "+textBox1.Text+" > "+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
 
SendString="RCPT TO: "+" < "+textBox2.Text+" > "+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
 
SendString="DATA "+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
 
SendString="SUBJECT: "+textBox3.Text+"\r\n"+textBox5.Text+"\r\n"+"."+"\r\n";
BufferString=System.Text.Encoding.Default.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
 
SendString="QUIT"+"\r\n";
BufferString=System.Text.Encoding.ASCII.GetBytes(SendString.ToCharArray());
networkstream.Write(BufferString,0,BufferString.Length);
 
networkstream.Close();
streamreader.Close();
}
catch(InvalidOperationException err)
{MessageBox.Show("{0}",err.ToString());}
}
 
private void ExitBtn_click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值