Created
January 9, 2025 10:23
-
-
Save bjoerntx/d7b391b005f6cd55150c06b4a7a9ff9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Mail; | |
public static class SmtpMail | |
{ | |
/// <summary> | |
/// Sends an email with the specified recipient, subject, body, and attachment. | |
/// </summary> | |
/// <param name="recipient">The recipient's email address.</param> | |
/// <param name="subject">The subject of the email.</param> | |
/// <param name="body">The body content of the email.</param> | |
/// <param name="attachment">The email attachment.</param> | |
public static void Send(string recipient, string subject, string body, Attachment attachment) | |
{ | |
// Create a new email message with sender and recipient details. | |
MailMessage emailMessage = new MailMessage("sender@yourdomain.com", recipient) | |
{ | |
Subject = subject, // Set the email subject. | |
IsBodyHtml = false, // Specify that the body is plain text (not HTML). | |
Body = body // Set the email body content. | |
}; | |
// Add the specified attachment to the email. | |
emailMessage.Attachments.Add(attachment); | |
// Configure and send the email using an SMTP client. | |
using (SmtpClient client = new SmtpClient()) | |
{ | |
client.Host = "smtp.yourprovider.com"; | |
client.UseDefaultCredentials = false; | |
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; | |
client.Credentials = new System.Net.NetworkCredential("username", "password"); | |
client.Port = 587; | |
client.EnableSsl = true; | |
// Send the email. | |
client.Send(emailMessage); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment