c#使用Outlook发送邮件
本文关键字:使用 Outlook | 更新日期: 2023-09-27 18:13:22
我在控制台应用程序中工作,我使用下面的c#代码在按钮单击事件时自动发送邮件,
public void SendMail()
{
//// Create the Outlook application by using inline initialization.
Outlook.Application oApp = new Outlook.Application();
////Create the new message by using the simplest approach.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
//Add a recipient.
// TODO: Change the following recipient where appropriate.
Outlook.Recipients oRecips = oMsg.Recipients;
List<string> oTORecip = new List<string>();
List<string> oCCRecip = new List<string>();
oTORecip.Add("example@test.com");
oCCRecip.Add("example@test.com");
foreach (string t in oTORecip)
{
Outlook.Recipient oTORecipt = oRecips.Add(t);
oTORecipt.Type = (int)Outlook.OlMailRecipientType.olTo;
oTORecipt.Resolve();
}
foreach (string t in oCCRecip)
{
Outlook.Recipient oCCRecipt = oRecips.Add(t);
oCCRecipt.Type = (int)Outlook.OlMailRecipientType.olCC;
oCCRecipt.Resolve();
}
//Set the basic properties.
oMsg.Subject = "TestMail- " + DateTime.Today.ToString("MM/dd/yyyy");
oMsg.HTMLBody = "<html>" +
"<head>" +
"<title>TestMail</title>" +
"</head>" +
"<body style='background-color:#E6E6E6;'>" +
"<div style='font-family: Georgia, Arial; font-size:14px; '>Hi,<br /><br />" +
"PFA<br />" +
"This is Test Mail.Please Ignore.<br /><br /><br /><br /><br />" +
"Thanks & Regards<br />" +
"test"+
"</div>" +
"</body>" +
"</html>";
string date = DateTime.Today.ToString("MM-dd-yyyy");
//Add an attachment.
// TODO: change file path where appropriate
String sSource = "D:''Test''test_" + date + ".xlsx";
String sDisplayName = "MyFirstAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);
// If you want to, display the message.
// oMsg.Display(true); //modal
//Send the message.
oMsg.Save();
oMsg.Send();
//Explicitly release objects.
oTORecip = null;
oCCRecip = null;
oAttach = null;
oMsg = null;
oApp = null;
}
这段代码工作正常。我想用另一个账户发邮件。如何填写上述代码中的收件地址?这可能吗?我需要使用哪些c#代码来代表其他使用c#代码的帐户发送邮件?
设置MailItem。SendUsingAccount属性
http://msdn.microsoft.com/en-us/library/office/ff869311%28v=office.14%29.aspx您只能从Application.Session.Accounts集合中选择。
或可能的check
MailItem。SentOnBehalfOfNamehttp://msdn.microsoft.com/en-us/library/office/ff862145%28v=office.14%29.aspx
(交换)
有一些类似的帖子:
Outlook自动化-更改发件人帐户
发送延迟消息传递并使用Powershell更改默认帐户
编辑
下面的代码可以使用智能感知,检查您的DLL版本。使用Outlook PIA的v14和v12进行检查。
从http://www.microsoft.com/en-us/download/details.aspx?id=43664
using Microsoft.Office.Interop.Outlook;
//C:'Program Files'Microsoft Visual Studio 11.0'Visual Studio Tools for Office'PIA'Office14'Microsoft.Office.Interop.Outlook.dll
//14.0.0.0 or 12.0.0.0 - it works from Outlook 2007 and higher
namespace ConsoleApplication1
{
class Program
{
public void SendMail()
{
//// Create the Outlook application by using inline initialization.
Application oApp = new Application();
////Create the new message by using the simplest approach.
MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
oMsg.SendUsingAccount = oApp.Session.Accounts[2]; // it starts at 1
...
似乎你正在使用一些其他的汇编DLL,因为对我来说,声明"使用Microsoft.Office.Interop;"根本不起作用。