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

本文关键字:链接 调用 类方法 Outlook 电子邮件 | 更新日期: 2023-09-27 18:03:39

在我的Class1 oMsg。我想给出一个参考,以便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();
            }
        }
    }
}

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

我不认为你可以这样做,但你可以用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>";