簡體   English   中英

如何使用 C# 放置電子郵件的發件人,我的 Outlook 應用程序中有 3 個電子郵件

[英]How to put the email's sender using C#, I have 3 email in my outlook application

我正在創建一個程序,該程序將使用 C# 自動從 Outlook 發送電子郵件。我的 Outlook 應用程序中有 3 封電子郵件,是否可以在 3 封電子郵件之間進行選擇作為電子郵件的發件人? 不知道怎么樣請幫忙!

這是我的代碼,當我點擊發送按鈕時,它會自動發送一封電子郵件。

try
{
    Outlook._Application _mpp = new Outlook.Application();
    Outlook.MailItem mail = (Outlook.MailItem)_mpp.CreateItem(Outlook.OlItemType.olMailItem);
    mail.To = txtTo.Text;
    mail.Subject = txtSubject.Text;
    mail.Body = txtMessage.Text;
    mail.Importance = Outlook.OlImportance.olImportanceNormal;
    ((Outlook._MailItem)mail).Send();
    MessageBox.Show("Your message has been successfuly sent", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

您可以使用SendUsingAccount使用您的 Outlook 帳戶之一發送郵件:

try
{
    Outlook._Application _mpp = new Outlook.Application();
    Outlook.MailItem mail = (Outlook.MailItem)_mpp.CreateItem(Outlook.OlItemType.olMailItem);
    mail.To = txtTo.Text;
    mail.Subject = txtSubject.Text;
    mail.Body = txtMessage.Text;
    mail.Importance = Outlook.OlImportance.olImportanceNormal;

/*************/
Outlook.Accounts accounts = _mpp.Session.Accounts;

string senderAddress = "wanted-sender-mail-address@xy.com";
foreach(Outlook.Account account in accounts)
{
    if(senderAddress.Equals(account.SmtpAddress, StringComparison.InvariantCultureIgnoreCase))
    {
        mail.SendUsingAccount = account;
        break;
    }
}
/*************/

    ((Outlook._MailItem)mail).Send();
    MessageBox.Show("Your message has been successfuly sent", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM