如何使用与登录用户不同的电子邮件帐户在C#中发送电子邮件

时间:2012-02-15 19:30:53

标签: c# email outlook office-interop

我有一个工作表单,可以从默认登录帐户发送电子邮件。我正在尝试添加一项功能,用户可以从下拉列表中选择一个电子邮件帐户,并通过alt地址发送电子邮件,如info@companyname.com或support@companyname.com。我正在尝试使用“SendUsingAccount”选项,但没有运气。我在网上看,但大多发现使用VB的例子,我不熟悉。我的代码如下:

Microsoft.Office.Interop.Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();
var mic = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));
Inspector oAddSig = null;

mic.sendusingaccount = ??????????????     //here is where i need the help
mic.Subject = "Announcing Participant Website Enhancements!";
mic.Importance = OlImportance.olImportanceHigh;
mic.BodyFormat = OlBodyFormat.olFormatHTML;
//mic.Attachments.Add(PDFAttachment + yearending.Text + ".pdf");
object emailBody = ToolBox.GetStringFromTextFile((string)EmailBody);
oAddSig = mic.GetInspector;
mic.HTMLBody = emailBody + mic.HTMLBody;
//mic.Display(true);
mic.Send();

3 个答案:

答案 0 :(得分:3)

我总是使用System.Net.Mail命名空间中的SMTP客户端。它有一个名为“发送”的方法,允许您提供消息来自“来自”的地址

检查出来: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

只要你知道你的交换服务器的地址(因为你试图在你的例子中使用outlook就应该没问题),一切都应该是A-OK。可能比上面的方法容易一点。

答案 1 :(得分:2)

如果您有一个下拉列表,您可以创建一个字符串,其值为该dd,并且asign sendusingaccount =您的字符串

    Microsoft.Office.Interop.Outlook.Application objOutlook = new     Microsoft.Office.Interop.Outlook.Application(); 
    string email = yourdropdown.selectedvalue;
    var mic = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem)); 
    Inspector oAddSig = null; 
    mic.sendusingaccount = email 

我希望这可以帮到你

答案 2 :(得分:1)

    Outlook.Account account =
    Application.Session.Accounts["Hotmail"];
mic.SendUsingAccount = account;

可能是您可以在会话中查看不同的帐户...

您可以查看: MSDN

相关问题