私のアプリケーションでは、Outlookで電子メールの送信ウィンドウを開く必要があります。
アプリケーションは、請求書のリストを表示します。ユーザーが請求書番号をクリックしたときに、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
アプリケーションサーバーにOutlookがインストールされていません。サーバーにOutlookをインストールする必要がありますか?または例外は他の理由によるものですか?それを取り除く方法は?
編集:コードの追加
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を削除します。あなたはそれを必要としません。
ユーザーが請求書番号をクリックしたときに、メールとともに送信するメッセージをユーザーに尋ねます。
standard .net smtp objectsを使用してこれを入力し、メールメッセージを作成したら、pdfを添付してメールサーバーに送信します。
サーバーにOutlookをインストールする必要も、相互運用機能の種類も必要ありません。
ExchangeまたはIMAPサーバーを使用している場合は、ユーザーのOutlookの[送信済み]ボックスにメールが表示されます。