发送电​​子邮件在ASP.NET当插入数据库记录 [英] Send email in ASP.NET when database record is inserted

查看:96
本文介绍了发送电​​子邮件在ASP.NET当插入数据库记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET FormView控件,它允许一个记录插入到SQL Server表。 InsertTemplate则>我怎么能有一个电子邮件从应用程序插入按钮被点击了&LT后发送一次记录被成功添加到表?

我希望能够指定为从,主题,正文等电子邮件。

我使用.NET 4.0和C#。

.aspx页面中:

 < ASP:FormView控件ID =formViewNewOrder=服务器的DataKeyNames =Order_ID上的DataSourceID =dsource1>
    <&InsertTemplate则GT;
    < B>订单编号:LT; / B> < ASP:文本框ID =txtInsertOrderNo=服务器文本='<%#绑定(ORDER_NUMBER)%>' />
    < BR />
    < BR />
    < ASP:LinkBut​​ton的ID =InsertButton=服务器的CausesValidation =真的CommandName =插入文本=插入的onclick =InsertButton_Click/>
            &安培; NBSP; &安培; NBSP; &安培; NBSP; &安培; NBSP;
    < ASP:LinkBut​​ton的ID =InsertCancelButton=服务器的CausesValidation =FALSE的CommandName =取消文本=取消的OnClick =InsertCancelButton_Click/>
    < / InsertTemplate则>
< / ASP:FormView控件>

code-背后:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data这;
要添加//命名空间?保护无效InsertButton_Click(对象发件人,EventArgs的发送)
{    //电子邮件code在这里?}


解决方案

 使用System.Net.Mail;
使用System.Net;私人无效的SendMail(字符串targetMail,
                       串shownTargetName,
                       字符串[] attachmentNames){
  VAR FROMADDRESS =新的MailAddress(support@e-volution-software.de,MailSendingProgram);
  VAR的toAddress =新的MailAddress(targetMail,shownTargetName);
  常量字符串fromPassword =12345isAbadPassword;
  主题=您的主题;
  身体=
        @
          在这里,你可以把会出现在身体的任何文本
          multilined甚至上述< HTML和GT;
        
  VAR SMTP =新SmtpClient {
    主机=smtp.1und1.de
    端口= 587,
    EnableSsl = TRUE,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials =假,
    证书=新的NetworkCredential(fromAddress.Address,fromPassword)
  };  使用(VAR消息= MAILMESSAGE新(FROMADDRESS,的toAddress){
                             主题=主体,
                             身体=人体}
        ){
    的foreach(在attachmentNames []字符串文件路径){
      附件attachMail =新的附件(文件路径);
      message.Attachments.Add(attachMail);
    }    尝试{
      smtp.Send(消息);
      的MessageBox.show(电子邮件发送!);
    } {抓
      的MessageBox.show(发送失败,请检查您的网络连接!);
    }
  }
}

I have a ASP.NET FormView which allows for inserting of a record into a SQL Server table. How can I have an email be sent from the application once the record is successfully added to the table after the "Insert" button is clicked in an <InsertItemTemplate>?

I would like to be able to specify the to, from, subject, body, etc. of the email.

I'm using .NET 4.0 and C#.

.aspx page:

<asp:FormView ID="formViewNewOrder" runat="server" DataKeyNames="Order_ID" DataSourceID="dsource1">
    <InsertItemTemplate>
    <b>Order Number:</b> <asp:TextBox ID="txtInsertOrderNo" runat="server" Text='<%# Bind("Order_Number") %>' />
    <br />
    <br />
    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" onclick="InsertButton_Click" />
            &nbsp; &nbsp; &nbsp; &nbsp; 
    <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"    Text="Cancel" OnClick="InsertCancelButton_Click" />
    </InsertItemTemplate>
</asp:FormView>

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
//Namespaces to be added?

protected void InsertButton_Click(object sender, EventArgs e)
{

    //Email code here ?

}

解决方案

using System.Net.Mail;
using System.Net;

private void SendMail( string targetMail, 
                       string shownTargetName, 
                       string[] attachmentNames) {
  var fromAddress = new MailAddress("support@e-volution-software.de", "MailSendingProgram");
  var toAddress = new MailAddress(targetMail, shownTargetName);
  const string fromPassword = "12345isAbadPassword";
  subject = "Your Subject";
  body = 
        @"
          Here you can put in any text that will appear in the body
          multilined and even in <html>
        ";
  var smtp = new SmtpClient {
    Host = "smtp.1und1.de",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  };

  using (var message = new MailMessage(fromAddress, toAddress) {
                             Subject = subject,
                             Body = body }
        ) {
    foreach(string filePath in attachmentNames[]) {
      Attachment attachMail = new Attachment(filePath);
      message.Attachments.Add(attachMail);
    }

    try {
      smtp.Send(message);
      MessageBox.Show("E-Mail sent!");
    } catch {
      MessageBox.Show("Sending failed, check your internet connection!");
    }
  }
}

这篇关于发送电​​子邮件在ASP.NET当插入数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆