什么是这个PHP代码的等效asp.net/c#代码?

时间:2010-03-25 15:43:45

标签: c# php asp.net

<?php

$system = $_POST['system']; // The FreshBooks system
$name = $_POST['name']; // The name of the event that just happened, e.g.invoice.create
$id = $_POST['object_id'];

$subject = "[$system] Event: $name";

if ($name=='callback.verify') {
$body = "
$name just happened on $system

Verification token: ".$_POST['verifier']."
";

} else {
$body = "
$name just happened
on $system
for id: $id
";
}

mail('youraddress@example.com',$subject,$body);

?>

3 个答案:

答案 0 :(得分:1)

您正在寻找SmtpClient类和Request对象。

答案 1 :(得分:0)

首先我们有一个标准的ASPX页面,听取人们发帖。 (您也可以使用ASHX处理程序,但我不会参与其中)

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Import Namespace="System.Net.Mail"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/C#" runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            var systemValue = Request.Form["system"];
            var nameValue = Request.Form["name"];
            var idValue = Request.Form["object_id"];
            var verifierValue = Request.Form["verifier"];

            var subject = string.Format("{0} Event: {1}", systemValue, nameValue);
            string body;

            if ( nameValue.Equals("callback verify", StringComparison.OrdinalIgnoreCase) )
                    body = string.Format("\n{0} just happened on {1}\n\nVerification token: {2}\n", nameValue, systemValue, verifierValue );
            else
                    body = string.Format("\n{0} just happened on {1} for id: {2}\n", nameValue, systemValue, idValue);

            var email = new MailMessage(
                    new MailAddress( "fromserver@anywhere.com")
                    , new MailAddress( "youraddress@anywhere.com") )
                            {
                                Subject = subject, Body = body
                            };

            var smtpServer = new SmtpClient();
            smtpServer.Send( email );
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html> 

现在,在其他地方,大概在你的postbin.org页面中,你需要一个发布到另一个页面的HTML页面。类似的东西:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form action="Default.aspx" method="post">  
        <input type="text" id="system" name="system" />
        <input type="text" id="name" name="name" />
        <input type="text" id="object_id" name="object_id" />
        <input type="text" id="verifier" name="verifier" />

        <input type="submit" />
    </form>
</body>
</html>

在这个标准的Html页面中,我将表单操作设置为发布到我的ASPX页面。发生这种情况时,ASPX页面上的Page_Load事件将触发并发送电子邮件(假设在ASPX页面的web.config文件中配置了电子邮件设置)。

答案 2 :(得分:0)

要获取请求值,您可以执行类似这样的操作

string myValue = Request.Form["MyPostArgument"];

然后,您可以使用string.format类来设置消息

string subject = string.format("{0} Event: {1}", mySystem, myEvent);

然后,您需要使用SmtpClient和MailMessage对象来构建电子邮件。