繁体   English   中英

如何从Outlook电子邮件中的链接调用C#类方法?

[英]How to call C# class method from link in Outlook email?

在我的Class1 oMsg.HTMLBody中,我想提供一个引用,以便在单击发送的电子邮件中的链接时调用DBConnectivity.cs类Connectivity()方法。 你能帮忙吗?

Class1.cs

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Data.SqlClient;

namespace Outlook_SendMailItem
{
    public class Class1
    {
        public static int Main(string[] args)
        {
            try
            {
                // Create the Outlook application by using inline initialization.
                Outlook.Application oApp = new Outlook.Application();

                //Create the new message by using the simplest approach.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

                //Add a recipient.
                // TODO: Change the following recipient where appropriate.
                Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("aman.agarwal4@cognizant.com");
                oRecip.Resolve();

                //Set the basic properties.
                oMsg.Subject = "This is the subject of the test message";
                oMsg.HTMLBody = "<a href=\"what_is_required_here\">Approve</a><pre>    </pre><a href=\"what_is_required_here\">Reject</a>";

                // Add an attachment.
                // TODO: change file path where appropriate
                String sSource = "C:\\Users\\461023\\Desktop\\Servlets.txt";
                String sDisplayName = "MyFirstAttachment";
                int iPosition = (int)oMsg.Body.Length + 1;
                int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);

                // If you want to, display the message.
                // oMsg.Display(true);  //modal

                //Send the message.
                oMsg.Save();
                ((Outlook._MailItem)oMsg).Send();

                //Explicitly release objects.
                oRecip = null;
                oAttach = null;
                oMsg = null;
                oApp = null;
            }

            // Simple error handler.
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught: ", e);
            }

            //Default return value.
            return 0;
        }
    }
}

DBConnectivity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SendEmail
{
    class DBConnectivity
    {
        public void connectivity()
        {
            string query = "Update Leaves Set status = @status where emailid = @emailid";

            using (SqlConnection connection = new SqlConnection("Data Source = ; Initial Catalog = ; Integrated Security = SSPI"))
            using (SqlCommand cmd = new SqlCommand(query , connection))
            {
                connection.Open();

                cmd.Parameters.AddWithValue("status", "approved");
                cmd.Parameters.AddWithValue("emailid", "email_id");

                int affected_rows = cmd.ExecuteNonQuery();
            }
        }
    }
}

我不认为您可以通过这种方式做到这一点,但是您可以使用PHP来做到这一点:

// Connect to mssql server 
$connection = mssql_connect($host, $user, $pass) or die("Cannot connect to server");

// Select a database 
$db = mssql_select_db($db_name, $connection) or die("Cannot select database");

// Get the status and execute the query
$query = 'UPDATE Leaves SET status = ' . $_GET['status'] . ' WHERE emailid = @emailid'; 
$result = mssql_query($query);

// Close the connection 
mssql_close($connection);

将此脚本放在php文件中并在线托管。 然后,您可以像这样使用它:

oMsg.HTMLBody = "<a href=\"http://yourpage.com?status=1\">Approve</a><pre> </pre><a href=\"http:///yourpage.com?status=0\">Reject</a>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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