您的位置:首页 > 编程语言 > C#

C#,WinForm发送邮件

2013-11-12 15:50 169 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;

namespace SendMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{

string [] recs = rec.Text.Split(';');
bool isyes =  SendMail(server.Text, 25, this.txt_account.Text, recs, this.subject.Text, this.content.Text, true, this.txt_pwd.Text);

}
//if (isyes)
//{
//    MessageBox.Show("邮件发送成功");
//}
}
NetworkStream ns;
/// <summary>
///
/// </summary>
/// <param name="smtpServer">smtp服务器</param>
/// <param name="point">发送端口</param>
/// <param name="_from">发件人</param>
/// <param name="_tos">收件人</param>
/// <param name="_subject">主题</param>
/// <param name="_content">内容</param>
/// <param name="isESMTP">是否需要身份验证</param>
/// <param name="userPWD">发件人密码</param>
/// <returns></returns>
public bool SendMail(string smtpServer, int point, string _from, string[] _tos, string _subject, string _content, bool isESMTP, string userPWD)
{
try
{
TcpClient smtpClient = new TcpClient();
IPAddress ip = Dns.GetHostAddresses(smtpServer)[0];
smtpClient.Connect(ip, point);//连接服务器
ns = smtpClient.GetStream();
if (SMTPReceive() != 220)
return false;
Send("HELO/r/n");
if (SMTPReceive() != 250)
return false;
//身份验证
if (isESMTP)
{
Send("AUTH LOGIN/r/n");
if (SMTPReceive() != 334)
return false;
string uname=_from.Substring(0,_from.IndexOf('@'));
Send(ConvertToBase64(uname) + "/r/n");
if (SMTPReceive() != 334)
return false;
Send(ConvertToBase64(userPWD) + "/r/n");
if (SMTPReceive() != 235)
return false;
}

Send("Mail From:<" + _from + ">/r/n");//发件人
if (SMTPReceive() != 250)
return false;
if (_tos.Length < 0)
return false;
foreach (string to in _tos)
{
Send("RCPT TO:<" + to + ">/r/n");
if (SMTPReceive() != 250)
return false;
}
Send("DATA /r/n");
if (SMTPReceive() != 354)
return false;
StringBuilder content = new StringBuilder();
content.Append("From:" + _from + "/r/n");
if (_tos.Length <= 0)
return false;
foreach (string to in _tos)
{
content.AppendFormat("To:{0}/r/n", to);
}
//邮件标题
content.AppendFormat("Subject:{0}/r/n", _subject);
//邮件编码格式
content.AppendFormat("Charset=/"{0}/"/r/n", "gb2312");

content.Append("/r/n");
content.Append(_content);//追加发送的内容
content.Append("/r/n./r/n");
Send(content.ToString());
if (SMTPReceive() != 250)
return false;
else
return true;
}
catch (Exception exc)
{
return false;
}

}

/// <summary>
/// 将string型转换为Base64
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
string ConvertToBase64(string str)
{
byte[] data = Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(data);
}

/// <summary>
/// 发送消息至服务器
/// </summary>
/// <param name="msg"></param>
public void Send(string msg)
{
byte[] data = Encoding.UTF8.GetBytes(msg);
ns.Write(data, 0, data.Length);
ns.Flush();
}

public int SMTPReceive()
{
try
{
byte[] data = new byte[1024];
int size = ns.Read(data, 0, 1024);
string result = Encoding.UTF8.GetString(data, 0, size);
int index = result.IndexOf(' ');
if (index == -1)
index = result.Length;
result = result.Substring(0, index);
return int.Parse(result);
}
catch
{
return 0;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: