Send email in ASP.NET when database record is inserted

后端 未结 1 1834
有刺的猬
有刺的猬 2021-01-27 19:59

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 ad

相关标签:
1条回答
  • 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!");
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题