Actionscript 2.0使用ashx .net处理程序通过电子邮件形成LoadVars

时间:2009-09-03 02:34:15

标签: asp.net email actionscript-2

我有一个flash 2.0文件,我需要通过asp处理程序发送电子邮件。首先,这可能吗?第二,如果是的话,我如何得到一个status = true的回报?

.net codebehind

 public void ProcessRequest(HttpContext context)
    {
        //E-Mail Method

        string response = "sent=success";

        MailAddress fromAddress = new MailAddress(context.Request.QueryString["Email"].ToString(), context.Request.QueryString["Name"].ToString());
        MailAddress toAddress = new MailAddress("emailInbox@site.com", "Goons");
        MailMessage message = new MailMessage(fromAddress, toAddress);
        message.Subject = context.Request.QueryString["Name"].ToString() + " sent you a message from the website.";
        message.Body = context.Request.QueryString["Msg"].ToString();
        SmtpClient client = new SmtpClient("mail.grassrootsdm.com");
        // Include credentials if the server requires them.
        NetworkCredential SMTPUserInfo = new NetworkCredential("mailsenderemail","password");
        client.Credentials = SMTPUserInfo;

        try {
            client.Send(message);
        }
        catch (Exception ex) {
            response = ex.ToString();
        }

        context.Response.Write(response);
    }

动作脚本

if (i == 0) {
    sendVars.Name = fieldName.field.text;
    sendVars.Email = fieldEmail.field.text;
    sendVars.Msg = fieldMsg.field.text;
    sendVars.sendAndLoad("http://www.grassrootsdm.com/WebService/EmailHandler.ashx", statusVars, "POST");
    statusMsg.text = "Sending...";
    statusVars.onLoad = function(success:Boolean) {

        if (success) {
            if (statusVars.sent == "success") {
                clearForm();
                statusMsg.text = "Message sent";
            }
        } else {
            statusMsg.text = "Error!";
        }
        clearInterval(clearStatus);
        clearStatus = setInterval(clearStatusInt, 3000);
    };
}

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。

阅读每个代码底部的重要说明,这些说明与从闪存到.net页面发送和检索数据有关。代码的解释位于代码内的注释中。

Flash部分(动作脚本2)

//function to send invoke .net page to send email
//use other control/button to call this function
//important: in order for the 'onLoad' event to work correctly, this function has to be 'Void'
function sendMail():Void
{
    //show status
    statusMsg.text = "Sending...";

    //create LoadVars object
    var lv_in:LoadVars = new LoadVars();
    var lv_out:LoadVars = new LoadVars();

    //set onLoad event
    lv_in.onLoad = function(success:Boolean)
    {
        //if success, meaning data has received from .net page, run this code
        if (success)
        {
            //lv_in.sent is used to parsed out message/data from .Net page
            statusMsg.text = "Message sent!" + lv_in.sent;
        }
        //if fail, run this code
        else
        {
            statusMsg.text = "Error!";
        }
    }

    //begin invoke .net page to send email
    lv_out.sendAndLoad("SendMail.aspx", lv_in, "POST");
}

重要提示: 包含 onLoad 事件的函数(在本例中为 sendMail 函数)必须为 Void 函数,这意味着它不返回值。如果此函数返回值,则会在不等待.net页面返回数据的数据的情况下执行该函数,因此 onLoad 事件将无法正确设置。

.Net部分

我复制了OP的.Net代码,因此假设此代码在发送电子邮件时有效。

public void ProcessRequest(HttpContext context)
{
    //E-Mail Method        
    string response = "sent=Success&";
    MailAddress fromAddress = new MailAddress(context.Request.QueryString["Email"].ToString(), context.Request.QueryString["Name"].ToString()); 
    MailAddress toAddress = new MailAddress("emailInbox@site.com", "Goons"); 
    MailMessage message = new MailMessage(fromAddress, toAddress); 
    message.Subject = context.Request.QueryString["Name"].ToString() + " sent you a message from the website."; 
    message.Body = context.Request.QueryString["Msg"].ToString();
    SmtpClient client = new SmtpClient("mail.grassrootsdm.com");

    // Include credentials if the server requires them. 
    NetworkCredential SMTPUserInfo = new NetworkCredential("mailsenderemail","password"); 
    client.Credentials = SMTPUserInfo; 
    try
    {
        client.Send(message);
    }      
    catch (Exception ex)
    {
        response = ex.ToString();
    }

    context.Response.Write(response);
}

重要提示: 我从OP的.Net代码中唯一改变的是响应消息。它最初是“发送=成功”,我改为“发送=成功&”。 这样做的原因是,如果您希望动作脚本解析来自.Net的已发布消息/数据,它将停在& 符号处,从而保留其余消息并仅获取消息在发送变量下。