简体   繁体   中英

How do you change the sender of Outlook messages?

I've been successfully sending out appointment invites with outlook through c# in an asp.net application. I'm using the following code:

//send out the outlook notification
Outlook.Application outlookApp = new Outlook.Application();
Outlook.AppointmentItem newMeeting = outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;

if (newMeeting != null)
{
    newMeeting.MeetingStatus = Microsoft.Office.Interop.Outlook.OlMeetingStatus.olMeeting;
    newMeeting.Location = "TBD";
    newMeeting.Subject = "New SEB Consult";
    newMeeting.Body = "A new meeting has been scheduled. If you're a member of the team, please accept";
    newMeeting.Start = meetingDate;
    newMeeting.Duration = 60;
    Outlook.Recipient recipient = newMeeting.Recipients.Add("Smith John");
    recipient.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
    ((Outlook._AppointmentItem)newMeeting).Send();
}

This works, but my problem is that it's sending them from my email which I'm logged into with outlook on the same computer. I'd like to send them from a different email, so that they appear more like system notifications coming from my application rather than a personal email. I do have the username and password for the account, but the application is eventually going to be run on a remote server, so I can't just log into outlook with another email. Nothing I've been able to find changes the sender. Does anyone have any more information on how to change these credentials, or where it looks for the credentials?

You can't use OLE if you would like to control the emails. OLE is just to control the local outlook instance which is tied to the running account.

You must use the exchange API instead. With it you can create an appointment like described in this MSDN article: How to: Create appointments and meetings by using EWS in Exchange 2013

Appointment appointment = new Appointment(service);

// Set the properties on the appointment object to create the appointment.
appointment.Subject = "Tennis lesson";
appointment.Body = "Focus on backhand this week.";
appointment.Start = DateTime.Now.AddDays(2);
appointment.End = appointment.Start.AddHours(1);
appointment.Location = "Tennis club";
appointment.ReminderDueBy = DateTime.Now;

// Save the appointment to your calendar.
appointment.Save(SendInvitationsMode.SendToNone);

// Verify that the appointment was created by using the appointment's item ID.
Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
Console.WriteLine("\nAppointment created: " + item.Subject + "\n");

The library is open source and available at github .

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

You may consider using the EWS Managed API, EWS, and web services in Exchange instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM