【问题标题】:How to call C# class method from link in Outlook email?如何从 Outlook 电子邮件中的链接调用 C# 类方法?
【发布时间】:2015-10-09 14:40:48
【问题描述】:

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

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 脚本并将其上传到网上,并在用户点击批准/拒绝时将用户重定向到此页面。
  • 你能告诉我怎么做吗?
  • 类似oMsg.HTMLBody = "&lt;a href=\"http:\\yourpage.com?accepted=1\"&gt;Approve&lt;/a&gt;&lt;pre&gt; &lt;/pre&gt;&lt;a href=\"http:\\yourpage.com?accepted=0\"&gt;Reject&lt;/a&gt;";的东西在线搜索php脚本,用php执行sql命令相当容易

标签: c# outlook href


【解决方案1】:

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

【讨论】:

  • 它不是一个 Web 应用程序,而是一个简单的控制台应用程序。所以我不能做类似yourpage.com
  • 这是电子邮件的正文。你可以放任何你想要的东西。
  • 我觉得我做错了什么。请检查我们是否需要将脚本保存在 .php 文件中。代替yourpage.com 的名称应该是什么。是项目名称吗?
  • 还有其他方法可以完成这项任务吗?更好的!!
  • 不,它不是项目名称,它是托管 php 脚本的 url。恐怕没有更好的解决方案。您的客户端将通过他的 webmail 客户端打开他的邮件,没有办法执行任何 C# 操作。您唯一能做的就是访问 Internet 上的某个页面(每个人都可以访问它),以便它可以执行您的脚本。
猜你喜欢
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多