У моего приложения есть требование открыть окно отправки электронной почты в Outlook.Откройте диалоговое окно отправки электронной почты в Outlook с помощью C#
Приложение отображает список счетов-фактур. Когда пользователь нажимает на номер счета-фактуры, мне нужно открыть окно отправки электронной почты в Outlook и приложить заявление PDF. Затем пользователь может изменить сообщение и нажать «Отправить».
Как это сделать?
Я попытался следующие:
using Outlook = Microsoft.Office.Interop.Outlook;
Приложение отлично работает на среде разработки, но РМКО получаю исключение:
**System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))**
Detailed:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at InvoiceSearchTool.Controllers.emailController.CreateMessageWithAttachment(String invoiceNumber, String recipient, String messageBody) in C:\Projects\KeleInvoice\InvoiceSearchTool\Controllers\emailController.cs:line 38
У меня нет перспектив, установленных на сервере приложений. мне нужен istall outlook на сервере? или исключение по какой-то другой причине? как избавиться от него?
EDIT: Добавление кода
public static void CreateMessageWithAttachment(string invoiceNumber, string recipient, string messageBody)
{
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));
Models.DYNAMICS_EXTEntities _db = new Models.DYNAMICS_EXTEntities();
#region set email recipients
{
ObjectParameter[] parameters = new ObjectParameter[1];
parameters[0] = new ObjectParameter("InvoiceNumber", invoiceNumber);
List<Models.EmailAddress> emailList = _db.ExecuteFunction<Models.EmailAddress>("uspGetEmailAddress", parameters).ToList<Models.EmailAddress>();
if (emailList.Count() > 0)
{
if(!(string.IsNullOrEmpty(emailList[0].Email.ToString().Trim())))
recipient = emailList[0].Email.ToString().Trim();
else
recipient = " ";
}
else
recipient = " ";
email.Recipients.Add(recipient);
}
#endregion
//email subject
email.Subject = "Invoice # " + invoiceNumber;
#region set email Text
{
Models.EmailText emailText = _db.ExecuteFunction<Models.EmailText>("uspEmailText").SingleOrDefault();
messageBody = emailText.EmailTextLine1.ToString().Trim();
email.Body = messageBody;
}
#endregion
#region email attachment
{
string fileName = invoiceNumber.Trim();
string filePath = HostingEnvironment.MapPath("~/Content/reports/");
filePath = filePath + fileName + ".pdf";
fileName += ".pdf";
int iPosition = (int)email.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = email.Attachments.Add(filePath, iAttachType, iPosition, fileName);
}
#endregion
email.Display();
}
catch (Exception e)
{
InvoiceSearchTool.Models.udtExceptionTable exception = new udtExceptionTable();
exception.MethodName = "email";
exception.Exception = e.ToString();
exception.Date = DateTime.Now;
DYNAMICS_EXTEntities db = new DYNAMICS_EXTEntities();
db.AddToudtExceptionTables(exception);
db.SaveChanges();
}
}
Если у вас есть требование отправить электронную почту через Outlook, но у вас не установлен Outlook на компьютере, на который вы отправляете свои электронные письма, в чем смысл этого? Вам нужны, по крайней мере, COM-объекты, ссылки на Interop. – Olaf
Не могли бы мы просто скопировать dll в bin и использовать его вот так? – 14578446
Нет, он должен быть зарегистрирован. – Olaf