How to pass procedure while sending mail

When sending an email using a programming language, you can pass a procedure (or a function) as a parameter to the email sending method. Here are some examples in different programming languages:

Python (using smtplib and email libraries)

import smtplib
from email.mime.text import MIMEText

def send_email(subject, body, to_email, from_email, password):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_email
    msg['To'] = to_email

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(from_email, password)
    server.sendmail(from_email, to_email, msg.as_string())
    server.quit()

# Pass the procedure as a parameter
send_email('Hello World', 'This is a test email', '[email protected]', '[email protected]', 'your_password')

Java (using JavaMail API)

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
    public static void sendEmail(String subject, String body, String toEmail, String fromEmail, String password) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);
        message.setText(body);

        Transport.send(message);
    }

    // Pass the procedure as a parameter
    public static void main(String[] args) {
        sendEmail("Hello World", "This is a test email", "[email protected]", "[email protected]", "your_password");
    }
}

C# (using System.Net.Mail namespace)

using System.Net.Mail;

public class SendEmail {
    public static void SendEmail(string subject, string body, string toEmail, string fromEmail, string password) {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(fromEmail);
        mail.To.Add(toEmail);
        mail.Subject = subject;
        mail.Body = body;

        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential(fromEmail, password);

        client.Send(mail);
    }

    // Pass the procedure as a parameter
    public static void Main(string[] args) {
        SendEmail("Hello World", "This is a test email", "[email protected]", "[email protected]", "your_password");
    }
}

In each example, the sendEmail procedure takes five parameters: subject, body, toEmail, fromEmail, and password. You can pass these parameters when calling the procedure to send an email.

Note that you should replace the placeholders (your_password, smtp.gmail.com, etc.) with your actual email account credentials and settings.