通过asp发送邮件或邀请

时间:2018-06-18 04:25:57

标签: asp.net vb.net outlook

我正在尝试使用asp页面动态发送电子邮件或邀请。

我用了, Oapp = new Outlook.Application();

但问题是在执行时,页面会尝试打开一个新的Outlook应用程序并发送邮件。我希望页面使用现有或打开的Outlook应用程序并发送邮件。 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务)自动化Microsoft Office应用程序,因为Office可能会出现不稳定Office在此环境中运行时的行为和/或死锁。

如果要构建在服务器端上下文中运行的解决方案,则应尝试使用已为安全无人值守执行的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。如果从服务器端解决方案使用Office应用程序,则应用程序将缺少许多成功运行的必要功能。此外,您将承担整体解决方案稳定性的风险。请在Considerations for server-side Automation of Office文章中详细了解相关内容。

作为一种解决方法,您可以考虑使用EWS或Outlook API,有关详细信息,请参阅EWS Managed API, EWS, and web services in Exchange

要发送电子邮件,请使用System.Net。*类从服务器端发送电子邮件。

 public static void CreateMessageWithAttachment(string server)
 {
     // Specify the file to be attached and sent.
     // This example assumes that a file named Data.xls exists in the
     // current working directory.
     string file = "data.xls";
     // Create a message and set up the recipients.
     MailMessage message = new MailMessage(
         "jane@contoso.com",
         "ben@contoso.com",
         "Quarterly data report.",
         "See the attached spreadsheet.");

    // Create  the file attachment for this e-mail message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);

    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);

    // Add the file attachment to this e-mail message.
    message.Attachments.Add(data);

    //Send the message.
    SmtpClient client = new SmtpClient(server);

    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try 
    {
        client.Send(message);
    }
    catch (Exception ex) 
    {
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString() );              
    }

    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    Console.WriteLine("File {0}", cd.FileName);
    Console.WriteLine("Size {0}", cd.Size);
    Console.WriteLine("Creation {0}", cd.CreationDate);
    Console.WriteLine("Modification {0}", cd.ModificationDate);
    Console.WriteLine("Read {0}", cd.ReadDate);
    Console.WriteLine("Inline {0}", cd.Inline);
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count);

    foreach (DictionaryEntry d in cd.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }

    data.Dispose();
 }