首先添加命名空间
Using System.Net.Mail
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");
client.Send(message);下面是对页面的设置:
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>
<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>好了,发送邮件的功能就实现了
