首先添加命名空间
Using System.Net.Mail
下面我们看发送邮件的代码部分:(
注意:网易的邮件服务器是smtp.163.com)
MailAddress from
=
new
MailAddress(TBMailFrom.Text);
MailAddress to = new MailAddress(TBMailTo.Text);
MailMessage message = new MailMessage(from,to);
message.Subject = TBSubject.Text;
message.Body = TBDescript.Text;
if (FileUpload1.PostedFile.FileName != "" )
{
Attachment att = new Attachment(FileUpload1.PostedFile.FileName);
message.Attachments.Add(att);
}
SmtpClient client = new SmtpClient( " smtp.163.com " );
smtp.Credentials = new System.Net.NetworkCredential( " username " , " password " );
client.Send(message);
MailAddress to = new MailAddress(TBMailTo.Text);
MailMessage message = new MailMessage(from,to);
message.Subject = TBSubject.Text;
message.Body = TBDescript.Text;
if (FileUpload1.PostedFile.FileName != "" )
{
Attachment att = new Attachment(FileUpload1.PostedFile.FileName);
message.Attachments.Add(att);
}
SmtpClient client = new SmtpClient( " smtp.163.com " );
smtp.Credentials = new System.Net.NetworkCredential( " username " , " password " );
client.Send(message);
我们这里在发送邮件的时候只是设置了邮件收,发件人,邮件主题和邮件正文部门,在.net里面还可以设置发送邮件的文本格式,优先级等.我们这里就不说明了,相信看看就会明白的.(message.Priority;设置优先级.mssage.Headers;设置邮件的标头. message.CC;设置抄送.message.IsBodyHtml;设置是否以html格式发送邮件)
下面是对页面的设置:
1
<
body
>
2 < form id = " form1 " runat = " server " >
3 < div >
4 < table style = " width: 268px " >
5 < tr >< td >
6 < asp:Label ID = " Label4 " runat = " server " Text = " 发件人: " ></ asp:Label ></ td >
7 < td >
8 < asp:TextBox ID = " TBMailFrom " runat = " server " ></ asp:TextBox ></ td ></ tr >
9 < tr >
10 < td style = " width: 101px " >
11 < asp:Label ID = " Label1 " runat = " server " Text = " 收件人: " ></ asp:Label ></ td >
12 < td >
13 < asp:TextBox ID = " TBMailTo " runat = " server " ></ asp:TextBox ></ td >
14 </ tr >
15 < tr >
16 < td style = " width: 101px " >
17 < asp:Label ID = " Label2 " runat = " server " Text = " 邮件主题: " ></ asp:Label ></ td >
18 < td >
19 < asp:TextBox ID = " TBSubject " runat = " server " ></ asp:TextBox ></ td >
20 </ tr >
21 < tr >< td colspan = " 2 " >
22 < asp:FileUpload ID = " FileUpload1 " runat = " server " Width = " 259px " /></ td >
23 </ tr >
24 < tr >
25 < td colspan = " 2 " >
26 < asp:Label ID = " Label3 " runat = " server " Text = " 邮件正文: " ></ asp:Label ></ td >
27 </ tr >
28 < tr >
29 < td colspan = " 2 " >
30 < asp:TextBox ID = " TBDescript " runat = " server " Height = " 97px " TextMode = " MultiLine " Width = " 247px " ></ asp:TextBox ></ td >
31 </ tr >
32 < tr >
33 < td colspan = " 2 " >
34 < asp:Button ID = " BSend " runat = " server " OnClick = " BSend_Click " Text = " 发送 " /></ td >
35 </ tr >
36 </ table >
37 </ div >
38 </ form >
39 </ body >
2 < form id = " form1 " runat = " server " >
3 < div >
4 < table style = " width: 268px " >
5 < tr >< td >
6 < asp:Label ID = " Label4 " runat = " server " Text = " 发件人: " ></ asp:Label ></ td >
7 < td >
8 < asp:TextBox ID = " TBMailFrom " runat = " server " ></ asp:TextBox ></ td ></ tr >
9 < tr >
10 < td style = " width: 101px " >
11 < asp:Label ID = " Label1 " runat = " server " Text = " 收件人: " ></ asp:Label ></ td >
12 < td >
13 < asp:TextBox ID = " TBMailTo " runat = " server " ></ asp:TextBox ></ td >
14 </ tr >
15 < tr >
16 < td style = " width: 101px " >
17 < asp:Label ID = " Label2 " runat = " server " Text = " 邮件主题: " ></ asp:Label ></ td >
18 < td >
19 < asp:TextBox ID = " TBSubject " runat = " server " ></ asp:TextBox ></ td >
20 </ tr >
21 < tr >< td colspan = " 2 " >
22 < asp:FileUpload ID = " FileUpload1 " runat = " server " Width = " 259px " /></ td >
23 </ tr >
24 < tr >
25 < td colspan = " 2 " >
26 < asp:Label ID = " Label3 " runat = " server " Text = " 邮件正文: " ></ asp:Label ></ td >
27 </ tr >
28 < tr >
29 < td colspan = " 2 " >
30 < asp:TextBox ID = " TBDescript " runat = " server " Height = " 97px " TextMode = " MultiLine " Width = " 247px " ></ asp:TextBox ></ td >
31 </ tr >
32 < tr >
33 < td colspan = " 2 " >
34 < asp:Button ID = " BSend " runat = " server " OnClick = " BSend_Click " Text = " 发送 " /></ td >
35 </ tr >
36 </ table >
37 </ div >
38 </ form >
39 </ body >
好了,发送邮件的功能就实现了,试一下.是不是比较方便.