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" οnclick="btn_send_Click" 
            Text="   Send   " />
        <asp:Label ID="lbl_mag" runat="server" ForeColor="Red"></asp:Label>
    </div>

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; //主體
            Message.Priority = MailPriority.High;

            SmtpClient smtpClient = new SmtpClient("smtp.163.com", 25);
            //郵件信箱服務器                            //發送端口
            smtpClient.Credentials = new System.Net.NetworkCredential(name, pwd);
            //用戶名                            //用戶名密碼
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Timeout = 99999;
            try
            {
                smtpClient.Send(Message);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章