首页 > 解决方案 > 从没有 SMTP 服务器的共享电子邮件地址发送电子邮件

问题描述

我想以编程方式使用给定的共享邮箱名称发送电子邮件。我无权访问 smtp 服务器,因此无法使用 System.Net.Mail。

我正在使用 Outlook = Microsoft.Office.Interop.Outlook;

如何从共享邮箱电子邮件而不是默认电子邮件地址发送?

outlook.MailItem mail application.CreateItem(outlook.OlItemType.olMailItem) as outlook.MailItem;
try
   {          

    if (mail.Subject.Contains("Highway Alert")
    {

        mail.SendUsingAccount = "sharedmailboxemail@email.com"
        mail.Send();
        System.Diagnostics.Debug.WriteLine("Email Sent ");
    }
    else

标签: c#outlookcom

解决方案


MailItem.SendUsingAccount属性返回或设置一个Account对象,该对象表示要在其下MailItem发送邮件的帐户。例如:

private void SendUsingAccountExample()
{
    Outlook.MailItem mail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
    mail.Subject = "Our itinerary";
    mail.Attachments.Add(@"c:\travel\itinerary.doc", Outlook.OlAttachmentType.olByValue,
        Type.Missing, Type.Missing);
    Outlook.Account account = Application.Session.Accounts["Hotmail"];
    mail.SendUsingAccount = account;
    mail.Send();
}

有关详细信息,请参阅使用 Hotmail 帐户发送邮件项目

请记住,在这种情况下,应在 Outlook 中配置另一个帐户。

SentOnBehalfOfName属性仅在 Exchange 配置文件/帐户的情况下才有意义此外,您需要具有代表他人发送所需的权限。有关类似讨论, 请参阅SentOnBehalfOfName问题。

如果您在配置文件中配置了多个帐户,则可以使用SendUsingAccount属性,该属性允许 Account 对象表示要发送 MailItem 的帐户。

 Sub SendUsingAccount() 
  Dim oAccount As Outlook.account 
  For Each oAccount In Application.Session.Accounts 
   If oAccount.AccountType = olPop3 Then 
    Dim oMail As Outlook.MailItem 
    Set oMail = Application.CreateItem(olMailItem) 
    oMail.Subject = "Sent using POP3 Account" 
    oMail.Recipients.Add ("someone@example.com") 
    oMail.Recipients.ResolveAll 
    oMail.SendUsingAccount = oAccount 
    oMail.Send 
   End If 
  Next 
 End Sub 

推荐阅读