c#调用outlook批量发邮件,给出完整代码
- User
c#调用outlook批量发邮件,给出完整代码
- Assistant
以下是一个使用C#调用Outlook批量发邮件的完整代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Outlook = Microsoft.Office.Interop.Outlook;
namespace BatchEmail { class Program { static void Main(string[] args) { string[] recipients = { "recipient1@example.com", "recipient2@example.com", "recipient3@example.com" }; string subject = "Test Email"; string body = "This is a test email sent from C#.";
Outlook.Application outlookApp = new Outlook.Application(); Outlook.MailItem oMail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); foreach (string recipient in recipients) { Outlook.Recipient oRecip = oMail.Recipients.Add(recipient); oRecip.Resolve(); } oMail.Subject = subject; oMail.Body = body; oMail.Save(); oMail.Send(); Console.WriteLine("Email sent successfully!"); Console.ReadLine(); } } - User
需要单独发,不是一封发给多个人
- Assistant
好的,那么以下是一个用C#调用Outlook单独发送邮件的完整代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Outlook = Microsoft.Office.Interop.Outlook;
namespace SendEmail { class Program { static void Main(string[] args) { string recipient = "recipient@example.com"; // 收件人邮箱地址 string subject = "Test Email"; // 邮件主题 string body = "This is a test email sent from C#."; // 邮件正文
Outlook.Application outlookApp = new Outlook.Application(); Outlook.MailItem oMail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); oMail.To = recipient; // 设置收件人地址 oMail.Subject = subject; // 设置主题 oMail.Body = body; // 设置正文 oMail.Save(); oMail.Send(); Console.WriteLine("Email sent successfully!"); Console.ReadLine(); } }}
在上面的代码中,我们仅定义了一个收件人recipient,并将它设置为To属性。然后我们设置了主题和正文,将邮件保存并发送。
最后,我们向控制台发送了一条消息,表示邮件已成功发送。
- User
如果outlook有登录多个邮箱,怎么选择对应的邮箱来发?
- Assistant
如果Outlook有登录多个邮箱,可以通过指定邮件账户的方式来选择对应的邮箱来发邮件。以下是修改后的代码,你可以根据需要进行修改:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Outlook = Microsoft.Office.Interop.Outlook;
namespace SendEmail { class Program { static void Main(string[] args) { string recipient = "recipient@example.com"; // 收件人邮箱地址 string subject = "Test Email"; // 邮件主题 string body = "This is a test email sent from C#. "; // 邮件正文 string accountName = "example@example.com"; // 邮件账户名称,这里以example@example.com为例
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.