邮件发送.net 的实现 vs2008 可以加附件
1using System;
2using System.Text;
3using System.Net.Mail;
4
5
6
7
8public partial class EmailTest : System.Web.UI.Page
9{
10 protected void Page_Load(object sender, EventArgs e)
11 {
12
13 }
14 protected void btnSendMail_Click(object sender, EventArgs e)
15 {
16 Encoding encoding = Encoding.GetEncoding("GB2312");
17
18 string address = txtMailAddress.Text.Trim();
19 string biaoti = txtMailTitle.Text.Trim();
20 string content = txtMailContent.Text.Trim();
21
22 MailAddress from = new MailAddress("YourUserName@163.com", "SiteMap工作室", encoding);
23
24 MailAddress to = new MailAddress(address);
25
26
27
28 MailMessage mail = new MailMessage(from ,to);
29
30
31 mail.Subject = biaoti ;
32
33 mail.Body = content;
34
35 mail.SubjectEncoding = encoding;
36
37 mail.BodyEncoding = encoding;
38
39 //附件问题
40 if (fileattach.HasFile)
41 {
42 //这两种方式都可以使用 Attachment fileAttachment= new Attachment(fileattach.PostedFile.InputStream, fileattach.FileName);
43 Attachment fileAttachment = new Attachment(fileattach.PostedFile.FileName);
44
45 mail.Attachments.Add(fileAttachment);
46 }
47
48 SmtpClient smtp = new SmtpClient("smtp.163.com");
49
50 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
51
52 smtp.UseDefaultCredentials = true;
53
54 smtp.Credentials = new System.Net.NetworkCredential("YourUserName@163.com", "password");
55
56 smtp.Send(mail);
57
58 Response.Write("邮件发送成功!");
59
60
61 }
62}
2using System.Text;
3using System.Net.Mail;
4
5
6
7
8public partial class EmailTest : System.Web.UI.Page
9{
10 protected void Page_Load(object sender, EventArgs e)
11 {
12
13 }
14 protected void btnSendMail_Click(object sender, EventArgs e)
15 {
16 Encoding encoding = Encoding.GetEncoding("GB2312");
17
18 string address = txtMailAddress.Text.Trim();
19 string biaoti = txtMailTitle.Text.Trim();
20 string content = txtMailContent.Text.Trim();
21
22 MailAddress from = new MailAddress("YourUserName@163.com", "SiteMap工作室", encoding);
23
24 MailAddress to = new MailAddress(address);
25
26
27
28 MailMessage mail = new MailMessage(from ,to);
29
30
31 mail.Subject = biaoti ;
32
33 mail.Body = content;
34
35 mail.SubjectEncoding = encoding;
36
37 mail.BodyEncoding = encoding;
38
39 //附件问题
40 if (fileattach.HasFile)
41 {
42 //这两种方式都可以使用 Attachment fileAttachment= new Attachment(fileattach.PostedFile.InputStream, fileattach.FileName);
43 Attachment fileAttachment = new Attachment(fileattach.PostedFile.FileName);
44
45 mail.Attachments.Add(fileAttachment);
46 }
47
48 SmtpClient smtp = new SmtpClient("smtp.163.com");
49
50 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
51
52 smtp.UseDefaultCredentials = true;
53
54 smtp.Credentials = new System.Net.NetworkCredential("YourUserName@163.com", "password");
55
56 smtp.Send(mail);
57
58 Response.Write("邮件发送成功!");
59
60
61 }
62}
前台代码
<h2>EmailTest</h2>
<p> </p>
<p>
邮件标题:<asp:TextBox ID="txtMailTitle" runat="server" Width="360px"></asp:TextBox>
</p>
<p>
给谁发信:<asp:TextBox ID="txtMailAddress" runat="server" Width="360px"></asp:TextBox></p>
<p>
正文内容:(添加附件)-<asp:FileUpload ID="fileattach" runat="server" />
</p>
<p>
<asp:TextBox ID="txtMailContent" runat="server" Height="300px" Width="460px" TextMode="MultiLine"
></asp:TextBox>
</p>
<p>
<asp:Button ID="btnSendMail" runat="server" Text="发送消息"
onclick="btnSendMail_Click" />
</p>