邮件发送.net 的实现 vs2008 可以加附件

 1 using  System;
 2 using  System.Text;
 3 using  System.Net.Mail;
 4
 5
 6
 7
 8 public   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>&nbsp;</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="发送消息"
            οnclick="btnSendMail_Click" />
    </p>
   

转载于:https://www.cnblogs.com/zhji-sitemap/archive/2008/07/14/1242578.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值