0

我有一个用 C# 发送电子邮件的网络服务

    [WebMethod]
    public string sendEmail()
    {
        MailMessage mail = new MailMessage();
        SmtpClient smtpsvr = new SmtpClient("smtp.gmail.com");

        // mail settings

        // smtpsvr settings

        smtpsvr.SendCompleted += new SendCompletedEventHandler(sentCompleteCallBack);

        try
        {
            smtpsvr.SendAsync(mail, "Email 1");
            return "sent"; //this return only indicate email has been sent out
        }

        catch (Exception)
        {
            return "failed";
        }
    }

    private void sentCompleteCallBack(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Error != null) //error
        {
            sendResult = "email failed " + (string)e.UserState;
        }
        else
        {   //this result, indicate the process of sending email has been completed
            sendResult = "email sent " + (string)e.UserState;
        }

        // ??? haw to pass sendResult value to client page
    }

我尝试使用属性字符串、类字符串来获取 sendResult 值;但在客户端页面(aspx)的最后,只有空/空。我只能获取字符串 sendEmail() 值。

如何将 sendResult 值传回给客户端?非常感谢你的帮忙 !

/ * ** * ** * ** * / 已编辑

可能我必须像这样更改代码吗?(仍在使用 sendAsync()

    [WebMethod]
    public string sendEmail()
    {
    MailMessage mail = new MailMessage();
    SmtpClient smtpsvr = new SmtpClient("smtp.gmail.com");

    //mail settings

    //smtpsvr settings
    smtpsvr.SendCompleted += new SendCompletedEventHandler(sentCompleteCallBack);

    try
    {
        smtpsvr.SendAsync(mail, "email 1");
        return "sent"; 
    }

    catch (Exception)
    {
        return "failed";
    }
}

 private void sentCompleteCallBack(object sender, AsyncCompletedEventArgs e)
 {
    if (e.Error != null) //error
    {
       //write to DB_email_status = "failed"
    }
    else
    {   
      //write to DB_email_status = "success"
    }
}

在客户端页面(aspx)中:(1)调用网络服务发送电子邮件。(2) 从 sendEmail() 方法获取电子邮件发送的字符串值。(3) button_onclick : 从 DB_email_status 查看/获取数据。(???)这种情况可以实现吗?(!!!) 非常感谢。

4

3 回答 3

0

为什么不直接在您的 SendEmail 方法中传递一个布尔值作为返回值,如下所示

bool sentEmail()
{
bool isValid = false;

try
{
    //if email successful
    isValid = true
}
catch(Exception ex)

{

  //Email Fails
  isValid = false;
}

return isValid;

}
于 2012-07-30T01:34:29.163 回答
0

如果您使用 HTTP 中的无状态协议,则服务器(Web 服务)只能向客户端提供一个响应。因此,WebService 无法将异步结果的详细信息发送到客户端,除非客户端再次对其状态进行探测。

IMO,您最好的选择是使用 .Send() 而不是 .SendAsync()。您可以使用Timeout属性来防止方法挂起。

于 2012-07-30T03:34:50.717 回答
0

或使用 send() 和线程。

 [WebMethod]
 public bool sendEmail()
 {
   //mail settings
   //smtp settings
   try 
   { 
        smtp.Send(mail);
        return true;
   }
   catch
   {
       return false;
   }

客户页面 (aspx)

   private void send()
   {
      Thread.Sleep(100;
      //call web service: sendEmail()
      if true 
      { label.Text = "sent"} else {label.Text = "fail"}
   }

   private void button_Onclick(...)
   {
       Thread t = new Thread (send);
        t.Start();
        t.Join();
    }

行为类似于 sendAsync(),但更容易获得返回值。

于 2012-07-31T00:01:17.220 回答