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;
            }
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值