程序代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 邮件的发送
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SmtpClient client=null;//设置Stmp协议
MailAddress mailFrom=null;//发信人地址
MailAddress mailTo=null;//收信人地址
MailMessage mailMsg;//发送的内容
FileStream fs=null;//文件流
private void SetSmtpClient(string ServerHost,int Port)
{
client =new SmtpClient();
client.Host=ServerHost; //指定SMTP服务名 例如QQ邮箱为 smtp. 新浪cn邮箱为 smtp.等
client.Port=Port; //设置端口
client.Timeout=0;//设置超时
}
private void SetAddressFrom(string mailName,string mailPwd)
{
NetworkCredential define=new NetworkCredential(mailName,mailPwd);//创建服务器认证
mailFrom=new MailAddress (mailName,textBox4.Text.Trim());//实例化发件人的地址
client.Credentials=new NetworkCredential(mailFrom.Address,mailPwd);//指定发件人的信息,邮箱和密码
}
private bool CheckSize(string Path)
{
try
{
fs=new FileStream(Path,FileMode.Open);
string name=fs.Name;
int size=(int)(fs.Length/1024/1024);
if(size>10)
{
MessageBox.Show("文件长度不能大于10M!你选择的文件大小为"+ size.ToString()+"M","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
return true;
}
catch(IOException e)
{
MessageBox.Show(e.Message);
return false;
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
if (!CheckSize(textBox7.Text.Trim()))
{
return;
}
}
if (textBox1.Text == "")
{
MessageBox.Show("请输入SMTP服务器名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请输入发件人邮箱地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (textBox3.Text == "")
{
MessageBox.Show("请输入发件人邮箱密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (textBox5.Text == "")
{
MessageBox.Show("请输入收件人邮箱地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (MessageBox.Show("确定要发送当前的邮件吗", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
try
{
//验证Smtp服务器信息
SetSmtpClient("smtp."+textBox1.Text.Trim()+comboBox1.Text.Trim(),Convert.ToInt32(domainUpDown1.Text.Trim()));
}
catch (Exception Ex)
{
MessageBox.Show("邮件发送失败,请确定SMTP服务名是否正确!" + "\n" + "技术信息:\n" + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{ //验证发件人信息
SetAddressFrom(textBox2.Text.Trim()+comboBox2.Text.Trim(),textBox3.Text.Trim());
}
catch (Exception Ex)
{
MessageBox.Show("邮件发送失败,请确定发件邮箱地址和密码的正确性!" + "\n" + "技术信息:\n" + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
mailMsg = new MailMessage();
mailMsg.To.Clear();
//添加收件人
mailTo = new MailAddress(textBox5.Text.Trim()+comboBox3.Text.Trim());
mailMsg.To.Add(mailTo);
//发件人邮箱
mailMsg.From = mailFrom;
//邮件主题
mailMsg.Subject = textBox6.Text.Trim();
mailMsg.SubjectEncoding = Encoding.UTF8;
//邮件正文
mailMsg.Body = textBox8.Text.Trim();
mailMsg.BodyEncoding = Encoding.UTF8;
//清空历史附件
mailMsg.Attachments.Clear();
//添加附件
mailMsg.Attachments.Add(new Attachment(textBox7.Text.Trim(), MediaTypeNames.Application.Octet));
//邮件发送完处理的事件
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
//开始发送邮件
client.SendAsync(mailMsg, "000000000");
}
}
void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
if (e.Cancelled)
{
MessageBox.Show("发送以取消");
}
if (e.Error != null)
{
MessageBox.Show("邮件发送失败!" + "\n" + "技术信息:\n" + e.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("邮件成功发出!", "恭喜!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Ex)
{
MessageBox.Show("邮件发送失败!" + "\n" + "技术信息:\n" + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ValidInput()
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
textBox7.Text = dlg.FileName;
}
}
}
}
以前用C#写的玩的,楼主可以看看