Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

c#调用outlook批量发邮件

  1. User

    c#调用outlook批量发邮件

  2. Assistant

    下面是一个简单的示例代码,它可以使用Outlook API发送批量邮件。请注意,您需要在安装Outlook并且已将Outlook库添加到项目引用中才能运行此代码。

    using Outlook = Microsoft.Office.Interop.Outlook;
    
    public void SendBatchEmails()
    {
        // Create an instance of the Outlook application
        Outlook.Application outlookApp = new Outlook.Application();
    
        // Loop through a list of email addresses
        List<string> recipients = new List<string>();
        recipients.Add("recipient1@example.com");
        recipients.Add("recipient2@example.com");
        recipients.Add("recipient3@example.com");
    
        foreach (string recipient in recipients)
        {
            // Create a new email message
            Outlook.MailItem email = (Outlook.MailItem) outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
    
            // Set the recipient email address
            email.To = recipient;
    
            // Set the email subject and body
            email.Subject = "Test email";
            email.Body = "This is a test email.";
    
            // Send the email
            email.Send();
        }
    
        // Close the Outlook application
        outlookApp.Quit();
    }
    

    请注意,此代码中的SMTP服务器未配置。如果您要使用SMTP服务器发送邮件而不是使用Outlook API,还需要在代码中配置SMTP服务器。

  3. User

    给出完整的代码

  4. Assistant

    下面是一个使用SMTP服务器发送批量邮件的完整示例代码。

    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Mail;
    
    public void SendBatchEmails()
    {
        // SMTP server configuration
        string smtpServerAddress = "smtp.example.com";  // Replace with your SMTP server address
        int smtpServerPort = 587;  // Replace with your SMTP server port
        string smtpServerUsername = "username";  // Replace with your SMTP username
        string smtpServerPassword = "password";  // Replace with your SMTP password
    
        // Email configuration
        string emailFromAddress = "from@example.com";  // Replace with your email address
        string emailFromName = "From Name";  // Replace with your name
        List<string> emailToAddresses = new List<string>();  // Replace with your recipients' email addresses
        emailToAddresses.Add("recipient1@example.com");
        emailToAddresses.Add("recipient2@example.com");
        emailToAddresses.Add("recipient3@example.com");
        string emailSubject = "Test email";  // Replace with your email subject
        string emailBody = "This is a test email.";  // Replace with your email body
    
        // Create a new SMTP client
        SmtpClient smtpClient = new SmtpClient(smtpServerAddress, smtpServerPort);
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = new NetworkCredential(smtpServerUsername, smtpServerPassword);
    
        // Loop through the list of recipients and send emails to each recipient
        foreach (string emailToAddress in emailToAddresses)
        {
            try
            {
                // Create a new email message
                MailMessage email = new MailMessage();
                email.From = new MailAddress(emailFromAddress, emailFromName);
                email.To.Add(emailToAddress);
                email.Subject = emailSubject;
                email.Body = emailBody;
    
                // Send the email
                smtpClient.Send(email);
    
                Console.WriteLine("Email sent to " + emailToAddress);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to send email to " + emailToAddress + ". Error message: " + ex.Message);
            }
        }
    
        // Close the SMTP client
        smtpClient.Dispose();
    }
    

    请注意,此代码中使用SMTP服务器发送邮件,并且需要配置正确的SMTP服务器地址、端口、用户名和密码。您还可以根据需要修改其他电子邮件配置,如电子邮件发件人、收件人、主题和正文。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.