, , ,

Sample code to send email from code behind in ASP.NET and Kentico

Posted by

Here’s an example of how you can send an email from the code-behind in ASP.NET and Kentico:

ASP.NET:

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

public partial class YourPage : System.Web.UI.Page
{
    protected void SendEmailButton_Click(object sender, EventArgs e)
    {
        // Sender and recipient email addresses
        string senderEmail = "[email protected]";
        string recipientEmail = "[email protected]";

        // Create a new MailMessage object
        MailMessage mail = new MailMessage(senderEmail, recipientEmail);

        // Set the email subject and body
        mail.Subject = "Hello from ASP.NET!";
        mail.Body = "This is the email body.";

        // Create a new SmtpClient object
        SmtpClient smtpClient = new SmtpClient("your-smtp-server");

        // Set the SMTP credentials if required
        smtpClient.Credentials = new NetworkCredential("username", "password");

        // Send the email
        smtpClient.Send(mail);

        // Optional: Show a success message or redirect to a thank you page
        SuccessLabel.Text = "Email sent successfully!";
    }
}

In this example, the SendEmailButton_Click event handler is triggered when a button is clicked on your ASP.NET page. It creates a new MailMessage object and sets the sender, recipient, subject, and body of the email. Then, a SmtpClient object is created with the SMTP server information. If your SMTP server requires authentication, you can provide the username and password. Finally, the Send method is called on the SmtpClient object to send the email.

Kentico:

using CMS.EmailEngine;

protected void SendEmailButton_Click(object sender, EventArgs e)
{
    // Sender and recipient email addresses
    string senderEmail = "[email protected]";
    string recipientEmail = "[email protected]";

    // Create a new EmailMessage object
    EmailMessage email = new EmailMessage()
    {
        From = senderEmail,
        Recipients = recipientEmail,
        Subject = "Hello from Kentico!",
        PlainTextBody = "This is the email body."
    };

    // Send the email
    EmailSender.SendEmail(email);

    // Optional: Show a success message or redirect to a thank you page
    SuccessLabel.Text = "Email sent successfully!";
}

In this example, the SendEmailButton_Click event handler is triggered when a button is clicked on your Kentico web form. It creates a new EmailMessage object and sets the sender, recipient, subject, and body of the email. The EmailSender.SendEmail method is used to send the email.

Make sure to replace the placeholder values with your actual email addresses, SMTP server information, and any necessary authentication credentials.

Note: Ensure that you have the required permissions and configurations for sending emails from your ASP.NET or Kentico application. Additionally, you may need to adjust the code to fit your specific email requirements, such as adding email attachments or configuring additional email properties.