我有一个用 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 查看/获取数据。(???)这种情况可以实现吗?(!!!) 非常感谢。