How to add the content of a read only textbox to a email body?

44 Views Asked by At

I have a textbox where it is read-only property enabled and has a text typed in the properties. I want to add the content of a read-only textbox into the body of an email. However the content of the textbox doesn't display in the body of the email received.

Is it possible?

private void notifyBtn_Click(object sender, EventArgs e)
{
    string currentTime = DateTime.Now.ToString();

    // Get the name of the student from the TextBox
    string nameStudent = studName.Text;  

    // Email configuration
    string senderEmail = "***************@gmail.com";
    string recipientEmail = "***************@gmail.com";
    string subject = "Memorandum of Agreement Submission";
    string body = "Klasmeyt " + nameStudent + "has submitted his memorandum of agreement document. \nTime: " + currentTime;

    // SMTP configuration
    string smtpAddress = "smtp.gmail.com";
    int portNumber = 587; // Or your SMTP port
    bool enableSSL = true;
    string emailFrom = "***************@gmail.com";
    string password = "***************";

    // Create SMTP client
    using (MailMessage mail = new MailMessage())
    {
        mail.From = new MailAddress(emailFrom);
        mail.To.Add(recipientEmail);
        mail.Subject = subject;
        mail.Body = body;

        // Configure SMTP client
        using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
        {
            smtp.Credentials = new NetworkCredential(emailFrom, password);
            smtp.EnableSsl = true;

            try
            {
                // Send email
                smtp.Send(mail);
                MessageBox.Show("Notification email sent successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to send notification email. Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
0

There are 0 best solutions below