VS2005下实现的一个简单邮件发送程序
今天上午自己写的一个非常简单的邮件发送程序,是用vs2005写的感觉跟vs2003有些不一样的东西,写的不好大家别骂。
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Net.Mail;
9
namespace MailSample_1
10
{
11
public partial class Send : Form
12
{
13
public Send()
14
{
15
InitializeComponent();
16
}
17
private void SendMail()
18
{
19
try
20
{
21
MailAddress from = new MailAddress(txtFrom.Text);//发件人地址
22
MailAddress to = new MailAddress(txtTo.Text);//收件人地址
23
MailMessage Email = new MailMessage(from,to);
24
Email.Subject = txtSubject.Text;//邮件标题
25
if(txtAttachment.Text!=null&&!txtAttachment.Text.Equals(""))
26
{
27
Attachment att = new Attachment(txtAttachment.Text);//实例化一个附件对象
28
Email.Attachments.Add(att);//向邮件添加附件
29
}
30
Email.Body = txtMessage.Text;//邮件正文
31
Email.Priority = MailPriority.Normal;//邮件的优先级别
32
Email.BodyEncoding = Encoding.Unicode;//邮件的编码方式
33
Email.IsBodyHtml = true;//是否按html格式发送
34
SmtpClient client = new SmtpClient("smtp.ziton.com.cn");//设置发件服务器
35
client.Credentials = new System.Net.NetworkCredential("用户名","密码");//邮箱的用户名和密码
36
client.DeliveryMethod = SmtpDeliveryMethod.Network;
37
client.Send(Email);
38
MessageBox.Show("邮件已发出");
39
}
40
catch (Exception ex)
41
{
42
MessageBox.Show(ex.ToString());
43
}
44
}
45
46
private void button1_Click(object sender, EventArgs e)
47
{
48
this.SendMail();
49
}
50
51
private void 发件服务器配置ToolStripMenuItem_Click(object sender, EventArgs e)
52
{
53
MessageBox.Show("正在建设中。。。。");
54
}
55
56
private void butAttachment_Click(object sender, EventArgs e)
57
{
58
DialogResult dr = AttachmentDialog.ShowDialog();
59
if (dr.Equals(DialogResult.OK))
60
{
61
txtAttachment.Text = AttachmentDialog.FileName;
62
}
63
}
64
65
private void Send_Load(object sender, EventArgs e)
66
{
67
txtFrom.Focus();
68
}
69
}
70
}
71
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8
using System.Net.Mail;9
namespace MailSample_110
{11
public partial class Send : Form12
{13
public Send()14
{15
InitializeComponent();16
}17
private void SendMail()18
{19
try20
{21
MailAddress from = new MailAddress(txtFrom.Text);//发件人地址22
MailAddress to = new MailAddress(txtTo.Text);//收件人地址23
MailMessage Email = new MailMessage(from,to); 24
Email.Subject = txtSubject.Text;//邮件标题25
if(txtAttachment.Text!=null&&!txtAttachment.Text.Equals(""))26
{27
Attachment att = new Attachment(txtAttachment.Text);//实例化一个附件对象28
Email.Attachments.Add(att);//向邮件添加附件29
}30
Email.Body = txtMessage.Text;//邮件正文31
Email.Priority = MailPriority.Normal;//邮件的优先级别32
Email.BodyEncoding = Encoding.Unicode;//邮件的编码方式33
Email.IsBodyHtml = true;//是否按html格式发送34
SmtpClient client = new SmtpClient("smtp.ziton.com.cn");//设置发件服务器 35
client.Credentials = new System.Net.NetworkCredential("用户名","密码");//邮箱的用户名和密码36
client.DeliveryMethod = SmtpDeliveryMethod.Network; 37
client.Send(Email);38
MessageBox.Show("邮件已发出");39
}40
catch (Exception ex)41
{ 42
MessageBox.Show(ex.ToString());43
} 44
}45

46
private void button1_Click(object sender, EventArgs e)47
{48
this.SendMail();49
}50

51
private void 发件服务器配置ToolStripMenuItem_Click(object sender, EventArgs e)52
{53
MessageBox.Show("正在建设中。。。。");54
}55

56
private void butAttachment_Click(object sender, EventArgs e)57
{58
DialogResult dr = AttachmentDialog.ShowDialog();59
if (dr.Equals(DialogResult.OK))60
{61
txtAttachment.Text = AttachmentDialog.FileName;62
}63
}64

65
private void Send_Load(object sender, EventArgs e)66
{67
txtFrom.Focus();68
}69
}70
}71



浙公网安备 33010602011771号