开发者

Whats the equivalent asp.net/c# code for this php code?

<?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 jus开发者_如何转开发t happened
on $system
for id: $id
";
}

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

?>


You're looking for the SmtpClient class and the Request object.


First we have a standard ASPX page listening for people to post to it. (You could also use a ASHX handler but I won't get into that)

<%@ 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> 

Now, somewhere else, presumably in your postbin.org page, you need an HTML page that posts to another. Something like:

<!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>

In this standard Html page, I'm setting the form action to post to my ASPX page. When that happens, the Page_Load event on the ASPX page will fire and send the email (assuming the email settings are configured in the ASPX page's web.config file).


To get the request values you would do something like this

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

You can then use the string.format class to setup the messages

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

You then need to use the SmtpClient and MailMessage objects to build out the e-mail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜