|
附上具体代码using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
namespace 利用TCPClient类直接发送邮件
{
public partial class Form1 : Form
{
TcpClient smtpSrv;
NetworkStream netStream;
string CRLF = "\r\n";
public Form1()
{
InitializeComponent();
}
private void WriteStream(string strCmd)
{
//CRLF表示结束符
strCmd += CRLF;
//将字符串转换为字节数组
byte[] bw = System.Text.Encoding.Unicode.GetBytes(strCmd);
//将字节数组写入网络连接
netStream.Write(bw, 0, bw.Length);
}
private string AuthStream(string strCmd)
{
try
{
//将字符串转化为字节数组
byte[] by = System.Text.Encoding.Default.GetBytes(strCmd);
//将字节数组转化为Base64编码形式的字符串
strCmd = Convert.ToBase64String(by);
}
catch (Exception err) { return err.ToString(); }
return strCmd;
}
private void ButtonSend_Click(object sender,System.EventArgs e)
{
//将信息列表框清空,该信息列表框用来存放SMTP邮件服务器返回的信息
listBoxMsg.Items.Clear();
try
{
//用来存放客户端向服务器端发送的字符串
string data;
//将进度条控件对象显示出来,反应邮件的发送速度
progressBar1.Visible = true;
labelp.Visible = true;
//设置进度条的最小值,以防多次发送邮件时使用
progressBar1.Value = 0;
//建立与SMTP服务器的连接
smtpSrv = new TcpClient(TextBoxSrv.Text, 25);
//获取一个Networkstream对象,以便通过建立好的连接接收和发送数据
netStream = smtpSrv.GetStream();
//产生一个StreamReader对象,用来从流中读取数据
StreamReader rdStrm = new StreamReader(smtpSrv.GetStream());
//向服务器发送EHLO Local请求建立连接
WriteStream("EHLO Local");
//读取服务器返回信息,写入列表信息框
listBoxMsg.Items.Add(rdStrm.ReadLine());
//进度条值递增
progressBar1.Value++;
//向服务器发送AUTH LOGIN请求认证
WriteStream("AUTH LOGIN");
listBoxMsg.Items.Add(rdStrm.ReadLine());
progressBar1.Value++;
data = TextBoxUser.Text;
//转换成Base64编码形式
data = AuthStream(data);
//向服务器发送用户名
WriteStream(data);
listBoxMsg.Items.Add(rdStrm.ReadLine());
progressBar1.Value++;
data = TextBoxPwd.Text;
data = AuthStream(data);
//向服务器发送口令
WriteStream(data);
listBoxMsg.Items.Add(rdStrm.ReadLine());
progressBar1.Value++;
//开始发送邮件的信封
//发送发送人的邮件地址
data = "MAIL FROM:<" + TextBoxSend.Text + ">";
WriteStream(data);
listBoxMsg.Items.Add(rdStrm.ReadLine());
progressBar1.Value++;
//发送接收人的邮件地址
data = "RCPT TO:<" + TextBoxRev.Text + ">";
WriteStream(data);
listBoxMsg.Items.Add(rdStrm.ReadLine());
progressBar1.Value++;
//开始发送数据
WriteStream("DATA");
listBoxMsg.Items.Add(rdStrm.ReadLine());
progressBar1.Value++;
//开始发送邮件的首部字段
//发送日期
data = "DATE:" + DateTime.Now;
WriteStream(data);
progressBar1.Value++;
//发送邮件发送者的信息
data = "FROM:" + TextBoxSend.Text;
WriteStream(data);
progressBar1.Value++;
//发送邮件接收者的信息
data = "TO:" + TextBoxRev.Text;
WriteStream(data);
progressBar1.Value++;
//发送邮件的主题
data = "SUBJECT:" + TextBoxSubject.Text;
WriteStream(data);
progressBar1.Value++;
//发送回复地址
data = "REPLY-TO:" + TextBoxSend.Text;
WriteStream(data);
progressBar1.Value++;
//发送一个空行,表示首部结束,开始正文发送
WriteStream("");
progressBar1.Value++;
//发送邮件的正文
WriteStream(TextBoxMailText.Text);
progressBar1.Value++;
//发送"."表示文件内容结束
WriteStream(".");
progressBar1.Value++;
listBoxMsg.Items.Add(rdStrm.ReadLine());
//发送断开连接的命令
WriteStream("QUIT");
progressBar1.Value++;
listBoxMsg.Items.Add(rdStrm.ReadLine());
netStream.Close();
progressBar1.Visible = false;
labelp.Visible = false;
//邮件发送成功
MessageBox.Show("邮件发送成功", "成功");
}
catch(Exception ex)
{
//捕捉操作中的异常,并显示
MessageBox.Show(ex.ToString(), "操作错误!");
}
}
}
}
求助啊
|
|