Mail script with ASP.NET in C# language Print

  • 0

This is sample code for your developer's review for mailx.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a new MailMessage object
        MailMessage mailMessage = new MailMessage();

        // Set the sender and recipient email addresses
        mailMessage.From = new MailAddress("testactive@AAAAAAAAAAA.com");
        mailMessage.To.Add("AAAAAAAAAAA@gmail.com");

        // Set the subject and body of the email
        mailMessage.Subject = "Test Email from ASP.NET in C#";
        mailMessage.Body = "This is a test email sent from ASP.NET using C#.";

        // Create a new SmtpClient object and configure it
        SmtpClient smtpClient = new SmtpClient("localhost");
        smtpClient.Port = 25; // SMTP server port (typically 25)

        // If your SMTP server requires authentication, uncomment the lines below and provide your credentials
        smtpClient.Credentials = new System.Net.NetworkCredential("testactive@AAAAAAAAAAA.com", "AAAAAAAAAAA");

        try
        {
            // Send the email
            smtpClient.Send(mailMessage);
            Response.Write("Email sent successfully!");
        }
        catch (Exception ex)
        {
            Response.Write("Failed to send email. Error: " + ex.Message);
        }
    }
</script>

Was this answer helpful?

« Back