当前位置:编程学习 > asp >>

asp.net带附件功能的简单发送邮件

 

<div> 

    收件人:<asp:TextBox ID="txt_toman" runat="server"></asp:TextBox> 

        <br /> 

        主题:<asp:TextBox ID="txt_title" runat="server"></asp:TextBox> 

        <br /> 

        内容:<asp:TextBox ID="txt_content" runat="server" Height="66px"  

            TextMode="MultiLine" Width="194px"></asp:TextBox> 

        <br /> 

        附件:<asp:FileUpload ID="FileUpload1" runat="server" /> 

        <br /> 

        <asp:Button ID="btn_send" runat="server" onclick="btn_send_Click"  

            Text="   Send   " /> 

        <asp:Label ID="lbl_mag" runat="server" ForeColor="Red"></asp:Label> 

    </div> 

 

view plain

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Net.Mail; 

using System.Text; 

 

namespace WebApplication1 

    public partial class WebForm1 : System.Web.UI.Page 

    { 

        protected void Page_Load(object sender, EventArgs e) 

        { 

 

        } 

 

        protected void btn_send_Click(object sender, EventArgs e) 

        { 

            bool flag = SendMail("发信人地址", "发信人", "用户名", "密码", txt_title.Text, txt_content.Text, txt_toman.Text, FileUpload1); 

            if (flag) 

            { 

                lbl_mag.Text = "发送成功"; 

            } 

            else 

            { 

                lbl_mag.Text = "糟糕,发送失败啦!"; 

            } 

        } 

 

        /// <summary> 

        /// 简单邮件发送器 

        /// </summary> 

        /// <param name="user">发信人地址</param> 

        /// <param name="who">发信人</param> 

        /// <param name="name">发送用户名</param> 

        /// <param name="pwd">用户名密码</param> 

        /// <param name="title">邮件标题</param> 

        /// <param name="body">发送内容</param> 

        /// <param name="shoujian">收件人地址</param> 

        /// <param name="file">附件</param> 

        /// <returns>是否发送成功</returns> 

 

        public static bool SendMail(string user, string who, string name, string pwd, string title, string body, string shoujian, FileUpload file) 

        { 

 

            MailMessage Message = new MailMessage( 

                new MailAddress(user,   //第一个是发信人的地址, 

                                who, //第二个参数是发信人  

                               Encoding.UTF8),      //编码 

                                new MailAddress(shoujian));//收信人邮箱 

            if (file.HasFile) 

            { 

                //添加附件 

                if (file.PostedFile != null) 

                { 

                    Attachment attachment = new Attachment(file.PostedFile.InputStream, file.PostedFile.FileName); 

                    Message.Attachments.Add(attachment); 

                } 

            } 

            Message.SubjectEncoding = Encoding.UTF8; 

            Message.Subject = title;//标题 

            Message.BodyEncoding = Encoding.UTF8; 

            Message.IsBodyHtml = true; 

            Message.Body = body; //主体&n

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,