2013-06-05 1 views
0

У меня есть ASP.NET FormView, который позволяет вставлять запись в таблицу SQL Server. Как я могу отправить электронное письмо из приложения после того, как запись будет успешно добавлена ​​в таблицу после нажатия кнопки «Вставить» в <InsertItemTemplate>?Отправить письмо в ASP.NET при вводе записи базы данных

Я хотел бы иметь возможность указать, из, темы, тела и т. Д. Электронной почты.

Я использую .NET 4.0 и C#.

.aspx страница:

<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-за:

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 ? 

} 
+0

после какой-то код, пожалуйста – skhurams

+0

я предполагаю, что вы знаете, как отправить электронную почту, и вы не в состоянии выясните, как проверить, нажата ли кнопка в InsertItemTemplate, затем прочитайте это http://www.knowlegezone.com/68/article/Tech nology/Software/Asp-Net/ASP-net-FormView-EditItemTemplate-And-InsertItemTemplate-Tutorial – skhurams

+0

Привет @skhurams, нет, я не знаю, как отправить электронное письмо в asp.net, вот что я ищу. –

ответ

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

private void SendMail(string targetMail, 
         string shownTargetName, 
         string[] attachmentNames) { 
    var fromAddress = new MailAddress("[email protected]", "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!"); 
    } 
    } 
} 
+0

Спасибо, я использую решение, очень похожее на это, и оно работает. Спасибо за руководство. –