sitestudio / otest

Test SKGithubTest app
MIT License
0 stars 0 forks source link

Example #201

Open sitestudio opened 1 month ago

sitestudio commented 1 month ago

Provide example in C# of Sending an email via the Consent App Catalog Group

skgithubtest[bot] commented 1 month ago
#!/bin/bash

# Creating a directory for the C# email example
mkdir EmailExample
cd EmailExample

# Create the C# file for sending an email
cat <<EOL > SendEmail.cs
using System;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace EmailExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Define the email parameters
            var fromAddress = new MailAddress("your_email@example.com", "Your Name");
            var toAddress = new MailAddress("recipient@example.com", "Recipient Name");
            const string fromPassword = "your_password"; // Use secure methods to store passwords
            const string subject = "Test Email";
            const string body = "This is a test email sent via the Consent App Catalog Group.";

            // Create the email message
            var smtp = new SmtpClient
            {
                Host = "smtp.example.com", // Your SMTP server
                Port = 587, // Common SMTP port for TLS
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };

            // Send the email asynchronously
            try
            {
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    await smtp.SendMailAsync(message);
                    Console.WriteLine("Email sent successfully.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to send email: {ex.Message}");
            }
        }
    }
}
EOL

# Create a README file explaining how to run the C# program
cat <<EOL > README.md
# Email Example

This project demonstrates how to send an email asynchronously using C#. 

## Instructions to Run

1. Ensure you have .NET SDK installed on your machine.
2. Replace the email parameters in `SendEmail.cs` with valid credentials:
   - `your_email@example.com`
   - `your_password`
   - `smtp.example.com`

3. Open a terminal and navigate to the project directory.
4. Build and run the project using:

dotnet run

EOL