Xaisoft
Xaisoft

Reputation: 46591

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 just happened
on $system
for id: $id
";
}

mail('[email protected]',$subject,$body);

?>

Upvotes: 1

Views: 2379

Answers (3)

Thomas
Thomas

Reputation: 64635

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( "[email protected]")
                    , new MailAddress( "[email protected]") )
                            {
                                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).

Upvotes: 0

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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.

Upvotes: 0

SLaks
SLaks

Reputation: 887215

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

Upvotes: 1

Related Questions