C# 如何通过指定发件人地址使用 Microsoft.Office.Interop.Outlook.MailItem 发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11223462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
提问by Harikasai
I'm using Interop for sending e-mails via Outlook, but I am not able to specify the From e-mail address.
我正在使用 Interop 通过 Outlook 发送电子邮件,但我无法指定发件人电子邮件地址。
I want to send mails to multiple users originating from the same sender (from). I need to mention the from e-mail address. However I cannot find a property using Intellisense that allows me to specify it.
我想向来自同一个发件人(来自)的多个用户发送邮件。我需要提及发件人电子邮件地址。但是,我无法使用 Intellisense 找到允许我指定它的属性。
Please help.
请帮忙。
Microsoft.Office.Interop.Outlook.Application olkApp1 =
new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem olkMail1 =
(MailItem)olkApp1.CreateItem(OlItemType.olMailItem);
olkMail1.To = txtpsnum.Text;
olkMail1.CC = "";
olkMail1.Subject = "Assignment note";
olkMail1.Body = "Assignment note";
olkMail1.Attachments.Add(AssignNoteFilePath,
Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1,
"Assignment_note");
olkMail1.Save();
//olkMail.Send();
回答by Christophe Geers
The Sendmethod sends the mail using the default account. To specify a different account to send the mail, set the SendUsingAccountproperty to the desired Accountprior to calling the Send method.
该发送方法发送使用默认的帐户的邮件。要指定不同的帐户来发送邮件,请在调用 Send 方法之前将SendUsingAccount属性设置为所需的帐户。
var application = new Application();
var mail = (_MailItem) application.CreateItem(OlItemType.olMailItem);
mail.To = "[email protected]";
....
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mailItem.SendUsingAccount = account;
mail.Send();
More information can be found here:
更多信息可以在这里找到:
回答by ruhil
You are using outlook to send the mail. Since outlook must be configured to use the fromaddress of your mail, you cannot provide the fromaddress directly. However, you can select an account available on outlook. For example :
您正在使用 Outlook 发送邮件。由于 Outlook 必须配置为使用from您的邮件地址,因此您不能from直接提供地址。但是,您可以选择 Outlook 上可用的帐户。例如 :
using Outlook = Microsoft.Office.Interop.Outlook;
Outlook.Accounts accounts = olkApp1.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// When the e-mail address matches, send the mail.
if (account.SmtpAddress == "[email protected]")
{
olkMail1.SendUsingAccount = account;
((Outlook._MailItem)olkMail1).Send();
break;
}
}

