问题
I have a little problem that I don't know to solve. I'm working with C#, Visual Studio 2010 and the error page that I have I am using Outlook libraries. In my project I added the following library: "Microsoft.Office.Interop.Outlook" If I run the website using the server that Visual Studio gives me, the page works perfectly. If I run the page through IIS, logging says me: "Could not retrieve the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))."
Does anyone know why that is? What should I do to fix it?
I want to create an outlook appointment using c#, save this appointment in one of my server folders and then send it attached in an email, how can I do it?
My code is this:
using Microsoft.Office.Interop;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Net.Mail;
Outlook.Application outlookApp = (Outlook.Application)new Outlook.Application();
Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
appointment.Subject = subject;
appointment.Body = comments;
appointment.Location = location;
appointment.StartUTC = dateInit;
appointment.EndUTC = dateEnd;
appointment.ReminderSet = true;
appointment.ReminderMinutesBeforeStart = advideInMinutes;
appointment.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
string appointmentPath = "c:\\webroot\\appointment\\appointmentName.msg";
appointment.SaveAs(appointmentPath);
MailMessage mail = new MailMessage();
mail.To.Add(new System.Net.Mail.MailAddress("mail@domain.com"));
mail.From = new System.Net.Mail.MailAddress("mail@domain.com", "MyName", System.Text.Encoding.UTF8);
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
Attachment appointmentAttahment = new Attachment(appointmentPath);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("authsmtp.mydomain.com");
smtp.Credentials = new System.Net.NetworkCredential(Settings.Items["mail@domain.com", "password");
smtp.EnableSsl = false;
smtp.Send(mail);
appointmentAttahment.Dispose();
回答1:
Looks like you are trying to automate Outlook from an IIS application. For that to work, you would need to install Outlook on your IIS server. Even if you would get that working, it is not a supported scenario.
http://support.microsoft.com/kb/257757:
"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."
Provided that you are using Exchange server you could use the Exchange Web Services (EWS)
Here is one example of how to create appointments using EWS: http://msdn.microsoft.com/en-us/library/exchange/dd633661(v=exchg.80).aspx
来源:https://stackoverflow.com/questions/15198239/failed-to-run-outlook-libraries-from-iis