Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c# - How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address

I'm using Interop for sending e-mails via Outlook, but I am not able to specify the From e-mail address.

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.

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using outlook to send the mail. Since outlook must be configured to use the from address of your mail, you cannot provide the from address directly. However, you can select an account available on outlook. For example :

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...