用Socket发邮件的代码(可以群发)
2007-05-30 11:22
387 查看
qunFa.aspx文件的代码:
<%...@ Page language="c#" Codebehind="qunFa.aspx.cs" AutoEventWireup="false" Inherits="liuwei.hanmail.qunFa" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>qunFa</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<asp:ListBox id="ListBoxMsg" style="Z-INDEX: 118; LEFT: 136px; POSITION: absolute; TOP: 344px"
runat="server" Width="440px" Height="184px"></asp:ListBox>
<asp:Label id="Label11" style="Z-INDEX: 108; LEFT: 120px; POSITION: absolute; TOP: 320px" runat="server">信息查看</asp:Label>
<asp:Label id="Label10" style="Z-INDEX: 117; LEFT: 112px; POSITION: absolute; TOP: 16px" runat="server">SMTP服务器地址</asp:Label>
<asp:Label id="Label9" style="Z-INDEX: 100; LEFT: 128px; POSITION: absolute; TOP: 48px" runat="server">用户名</asp:Label>
<asp:TextBox id="TextBoxSrv" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 24px"
runat="server" Width="240px"></asp:TextBox>
<asp:Label id="Label8" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 48px" runat="server">口令</asp:Label>
<asp:TextBox id="TextBoxUser" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 48px"
runat="server" Width="152px"></asp:TextBox>
<asp:TextBox id="TextBoxPwd" style="Z-INDEX: 104; LEFT: 416px; POSITION: absolute; TOP: 48px"
runat="server" Width="168px" TextMode="Password"></asp:TextBox>
<asp:Label id="Label4" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 80px" runat="server">发信人</asp:Label>
<asp:Label id="Label5" style="Z-INDEX: 106; LEFT: 360px; POSITION: absolute; TOP: 80px" runat="server">收信人</asp:Label>
<asp:TextBox id="TextBoxSend" style="Z-INDEX: 107; LEFT: 192px; POSITION: absolute; TOP: 80px"
runat="server"></asp:TextBox>
<asp:TextBox id="TextBoxRev" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 80px"
runat="server" Width="168px"></asp:TextBox>
<asp:Label id="Label6" style="Z-INDEX: 110; LEFT: 128px; POSITION: absolute; TOP: 112px" runat="server">主题</asp:Label>
<asp:TextBox id="TextBoxSubject" style="Z-INDEX: 112; LEFT: 192px; POSITION: absolute; TOP: 104px"
runat="server" Width="392px"></asp:TextBox>
<asp:TextBox id="TextBoxMailText" style="Z-INDEX: 113; LEFT: 144px; POSITION: absolute; TOP: 160px"
runat="server" Width="440px" TextMode="MultiLine" Height="120px"></asp:TextBox>
<asp:Label id="Label7" style="Z-INDEX: 114; LEFT: 128px; POSITION: absolute; TOP: 136px" runat="server">邮件内容</asp:Label>
<asp:Button id="ButtonSend" style="Z-INDEX: 115; LEFT: 464px; POSITION: absolute; TOP: 296px"
runat="server" Text="发送"></asp:Button>
<asp:Label id="LabelErr" style="Z-INDEX: 116; LEFT: 696px; POSITION: absolute; TOP: 136px"
runat="server"></asp:Label></FORM>
</body>
</HTML>
qunFa.aspx.cs的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Sendmail
...{
/**//// <summary>
/// qunFa 的摘要说明。
/// </summary>
public class qunFa : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label LabelErr;
protected System.Web.UI.WebControls.Button ButtonSend;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox TextBoxMailText;
protected System.Web.UI.WebControls.TextBox TextBoxSubject;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.TextBox TextBoxRev;
protected System.Web.UI.WebControls.TextBox TextBoxSend;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBoxPwd;
protected System.Web.UI.WebControls.TextBox TextBoxUser;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.TextBox TextBoxSrv;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.ListBox ListBoxMsg;
protected System.Web.UI.WebControls.Label Label10;
TcpClient smtpSrv;
NetworkStream netStrm;
string CRLF=" ";
private void Page_Load(object sender, System.EventArgs e)
...{
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.ButtonSend.Click += new System.EventHandler(this.ButtonSend_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ButtonSend_Click(object sender, System.EventArgs e)
...{
this.ListBoxMsg.Items.Clear();
string[] userList=this.TextBoxRev.Text.Split(',');
for(int i=0;i<userList.Length;i++)
Qun(userList[i]);
}
public void Qun(string userList)
...{
try
...{
string data;
smtpSrv=new TcpClient(this.TextBoxSrv.Text,25);
netStrm=smtpSrv.GetStream();
StreamReader rdStrm=new StreamReader(smtpSrv.GetStream());
this.WriteStream("EHLO Local");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
this.WriteStream("AUTH LOGIN");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data=this.TextBoxUser.Text;
data=AuthStream(data);
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data=this.TextBoxPwd.Text;
data=AuthStream(data);
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data="MAIL FROM:<"+this.TextBoxSend.Text+">";
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data="RCPT TO:<"+userList+">";
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
this.WriteStream("DATA");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data="Date:"+DateTime.Now;
this.WriteStream(data);
data="From:"+this.TextBoxSend.Text;
this.WriteStream(data);
data="TO:"+userList;
this.WriteStream(data);
data="SUBJECT:"+this.TextBoxSubject.Text;
this.WriteStream(data);
data="Reply-TO:"+this.TextBoxSend.Text;
this.WriteStream(data);
this.WriteStream("");
this.WriteStream(this.TextBoxMailText.Text);
this.WriteStream(".");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
this.WriteStream("QUIT");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
netStrm.Close();
rdStrm.Close();
this.LabelErr.Text="发送成功!";
}
catch(Exception ex)
...{
this.LabelErr.Text=ex.ToString();
}
}
private void WriteStream(string strCmd)
...{
strCmd+=CRLF;
byte[] bw=System.Text.Encoding.Default.GetBytes(strCmd);
netStrm.Write(bw,0,bw.Length);
}
private string AuthStream(string strCmd)
...{
try
...{
byte[] by=System.Text.Encoding.Default.GetBytes(strCmd);
strCmd=Convert.ToBase64String(by);
}
catch(Exception err)
...{
return err.ToString();
}
return strCmd;
}
}
}
注:多个收件人之间用逗号隔开
<%...@ Page language="c#" Codebehind="qunFa.aspx.cs" AutoEventWireup="false" Inherits="liuwei.hanmail.qunFa" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>qunFa</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<asp:ListBox id="ListBoxMsg" style="Z-INDEX: 118; LEFT: 136px; POSITION: absolute; TOP: 344px"
runat="server" Width="440px" Height="184px"></asp:ListBox>
<asp:Label id="Label11" style="Z-INDEX: 108; LEFT: 120px; POSITION: absolute; TOP: 320px" runat="server">信息查看</asp:Label>
<asp:Label id="Label10" style="Z-INDEX: 117; LEFT: 112px; POSITION: absolute; TOP: 16px" runat="server">SMTP服务器地址</asp:Label>
<asp:Label id="Label9" style="Z-INDEX: 100; LEFT: 128px; POSITION: absolute; TOP: 48px" runat="server">用户名</asp:Label>
<asp:TextBox id="TextBoxSrv" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 24px"
runat="server" Width="240px"></asp:TextBox>
<asp:Label id="Label8" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 48px" runat="server">口令</asp:Label>
<asp:TextBox id="TextBoxUser" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 48px"
runat="server" Width="152px"></asp:TextBox>
<asp:TextBox id="TextBoxPwd" style="Z-INDEX: 104; LEFT: 416px; POSITION: absolute; TOP: 48px"
runat="server" Width="168px" TextMode="Password"></asp:TextBox>
<asp:Label id="Label4" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 80px" runat="server">发信人</asp:Label>
<asp:Label id="Label5" style="Z-INDEX: 106; LEFT: 360px; POSITION: absolute; TOP: 80px" runat="server">收信人</asp:Label>
<asp:TextBox id="TextBoxSend" style="Z-INDEX: 107; LEFT: 192px; POSITION: absolute; TOP: 80px"
runat="server"></asp:TextBox>
<asp:TextBox id="TextBoxRev" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 80px"
runat="server" Width="168px"></asp:TextBox>
<asp:Label id="Label6" style="Z-INDEX: 110; LEFT: 128px; POSITION: absolute; TOP: 112px" runat="server">主题</asp:Label>
<asp:TextBox id="TextBoxSubject" style="Z-INDEX: 112; LEFT: 192px; POSITION: absolute; TOP: 104px"
runat="server" Width="392px"></asp:TextBox>
<asp:TextBox id="TextBoxMailText" style="Z-INDEX: 113; LEFT: 144px; POSITION: absolute; TOP: 160px"
runat="server" Width="440px" TextMode="MultiLine" Height="120px"></asp:TextBox>
<asp:Label id="Label7" style="Z-INDEX: 114; LEFT: 128px; POSITION: absolute; TOP: 136px" runat="server">邮件内容</asp:Label>
<asp:Button id="ButtonSend" style="Z-INDEX: 115; LEFT: 464px; POSITION: absolute; TOP: 296px"
runat="server" Text="发送"></asp:Button>
<asp:Label id="LabelErr" style="Z-INDEX: 116; LEFT: 696px; POSITION: absolute; TOP: 136px"
runat="server"></asp:Label></FORM>
</body>
</HTML>
qunFa.aspx.cs的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Sendmail
...{
/**//// <summary>
/// qunFa 的摘要说明。
/// </summary>
public class qunFa : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label LabelErr;
protected System.Web.UI.WebControls.Button ButtonSend;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox TextBoxMailText;
protected System.Web.UI.WebControls.TextBox TextBoxSubject;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.TextBox TextBoxRev;
protected System.Web.UI.WebControls.TextBox TextBoxSend;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBoxPwd;
protected System.Web.UI.WebControls.TextBox TextBoxUser;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.TextBox TextBoxSrv;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.ListBox ListBoxMsg;
protected System.Web.UI.WebControls.Label Label10;
TcpClient smtpSrv;
NetworkStream netStrm;
string CRLF=" ";
private void Page_Load(object sender, System.EventArgs e)
...{
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.ButtonSend.Click += new System.EventHandler(this.ButtonSend_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ButtonSend_Click(object sender, System.EventArgs e)
...{
this.ListBoxMsg.Items.Clear();
string[] userList=this.TextBoxRev.Text.Split(',');
for(int i=0;i<userList.Length;i++)
Qun(userList[i]);
}
public void Qun(string userList)
...{
try
...{
string data;
smtpSrv=new TcpClient(this.TextBoxSrv.Text,25);
netStrm=smtpSrv.GetStream();
StreamReader rdStrm=new StreamReader(smtpSrv.GetStream());
this.WriteStream("EHLO Local");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
this.WriteStream("AUTH LOGIN");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data=this.TextBoxUser.Text;
data=AuthStream(data);
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data=this.TextBoxPwd.Text;
data=AuthStream(data);
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data="MAIL FROM:<"+this.TextBoxSend.Text+">";
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data="RCPT TO:<"+userList+">";
this.WriteStream(data);
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
this.WriteStream("DATA");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
data="Date:"+DateTime.Now;
this.WriteStream(data);
data="From:"+this.TextBoxSend.Text;
this.WriteStream(data);
data="TO:"+userList;
this.WriteStream(data);
data="SUBJECT:"+this.TextBoxSubject.Text;
this.WriteStream(data);
data="Reply-TO:"+this.TextBoxSend.Text;
this.WriteStream(data);
this.WriteStream("");
this.WriteStream(this.TextBoxMailText.Text);
this.WriteStream(".");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
this.WriteStream("QUIT");
this.ListBoxMsg.Items.Add(rdStrm.ReadLine());
netStrm.Close();
rdStrm.Close();
this.LabelErr.Text="发送成功!";
}
catch(Exception ex)
...{
this.LabelErr.Text=ex.ToString();
}
}
private void WriteStream(string strCmd)
...{
strCmd+=CRLF;
byte[] bw=System.Text.Encoding.Default.GetBytes(strCmd);
netStrm.Write(bw,0,bw.Length);
}
private string AuthStream(string strCmd)
...{
try
...{
byte[] by=System.Text.Encoding.Default.GetBytes(strCmd);
strCmd=Convert.ToBase64String(by);
}
catch(Exception err)
...{
return err.ToString();
}
return strCmd;
}
}
}
注:多个收件人之间用逗号隔开
相关文章推荐
- 用Socket发邮件的代码(可以群发)
- 用Socket发邮件的代码(可以群发)
- javaMail发邮件的模板代码,如果你用网易邮箱的账号发送邮件失败的话,可以看看
- 亲测 ABAP可以发邮件代码
- 用java mail 发邮件的代码,支持群发
- 分享一个从国外网站看到的socket程序,程序并不强悍,但能从代码中看到老外是怎么写代码的,咱可以取长补短
- c#代码发送邮件,还可以实现群发
- java写的Socket客户端和服务端通信,经验证可以执行的代码,并添加了很多注释
- ios 检测网络 无须代码就可以设置
- socket代码学习笔记
- 招商银行设计缺陷可以查询其他用户信用卡消费&还款记录(附测试代码)
- java与c/c++进行socket通信的一些问题(1)-JSP教程,Java技巧及代码
- 来段代码提提神-Java实现SocketServer和client链接
- Linux 下socket通信终极指南(附TCP、UDP完整代码)
- Jmail发邮件代码,2005-6-2版
- 小猫统计:对于固定值字段,查询时可以显示中文,亦可以显示代码
- 基于X.509证书和SSL协议的身份认证过程实现(OpenSSL可以自己产生证书,有TCP通过SSL进行实际安全通讯的实际编程代码)good
- IntelliJ2017:如何将一段代码变成可以便捷输入的模板
- 可视化编程 Tips 之"无需代码设置圆角" "简单处理让我们可以无需代码设置边框颜色"
- //1.编写代码模拟三次密码输入的场景。//2.编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。