微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Net Framework 4.5.2 SmtpClient.SendmailAsync错误

如何解决Net Framework 4.5.2 SmtpClient.SendmailAsync错误

我正在研究存在阻塞问题的Framework 4.5.2 MVC项目。它正在使用SmtpClient并正在内联同步调用“ SendMail”,因此我试图使myClient.SendMailAsync正常工作,但是尽管我能想到的结构安排很多,但它失败了:

发送邮件失败”

的内部异常

此时无法启动异步操作。异步操作只能在异步处理程序或模块内或在页面生命周期中的某些事件期间启动。如果在执行页面时发生此异常,请确保将该页面标记为。此异常也可能表示尝试调用“异步无效”方法,ASP.NET请求处理通常不支持这种方法。相反,异步方法应返回一个Task,而调用者应等待它。

编辑-我意识到该消息非常冗长,但是我正在做的任何事情似乎都没有用,除非我丢失了一些东西。 -EndEdit

邮件生成功能调用是从SignalR集线器内部进行的,否则非常简单(经过简化和测试的结果相同):

    public async Task SendMatchEmail(MatchData data)
    {
        try
        {
            if (await Testemail(data))
            {
                await Clients.Caller.broadcastComplete("Match Completed");
            }
            else
            {
                await Clients.Caller.broadcastComplete("Match Failed with " + _msg);
            }
        }
        catch (Exception ex)
        {
            await Clients.Caller.broadcastComplete("Match Failed with " + ex.Message);
        }
    }

以及中心中的电子邮件发送方法

    private async Task<bool> Testemail(MatchData data)
    {
        try
        {
            string fromEmail = ConfigurationManager.AppSettings["SMTP.ReplyTo"];
            string emailAddress = "xxxx@xxxx.com";
            string subject = "test email";
            string body = "<htmL><body><p>this is a test</p></body></html>";
            MailMessage mm = new MailMessage(fromEmail,emailAddress,subject,body)
            {
                IsBodyHtml = true
            };
            SmtpClient smtp = new SmtpClient()
            {
                Host = ConfigurationManager.AppSettings["SMTP.Server"],Credentials = new NetworkCredential(
                                  ConfigurationManager.AppSettings["SMTP.Account"],(ConfigurationManager.AppSettings["SMTP.Password"])),Port = Convert.ToInt16(ConfigurationManager.AppSettings["SMTP.Port"]),EnableSsl = ConfigurationManager.AppSettings["SMTP.EnableSSL"] == "True"
            };
            await smtp.SendMailAsync(mm).ContinueWith(t =>
            {
                Console.WriteLine("done");
            });
            return true;
        }
        catch (Exception ex)
        {
            //fails to here with noted exception messages
            _msg = ex.Message;
            return false;
        }

    }

这是主叫客户代码

function match() {
        try{
            $("#matchingProgress").text("Getting IDs To Match");
            var hubc = $.connection.matchingHub;
            hubc.client.broadcastComplete = function (msg) {
                console.log("COMPLETE");
                window.parent.toggleLoading(false);
                $("#matchingProgress").text(msg);
                moveNext();
            }
            hubc.client.broadcastProgress = function (msg) {
                $("#matchingProgress").text(msg);
            }

            $.connection.hub.disconnected(function() {
                setTimeout(function() {
                    $.connection.hub.start();
                },5000); // Restart connection after 5 seconds.
            });

            $.connection.hub.start().done(function () {
                //call the match function on the server
                window.parent.toggleLoading(true);
                $("#matchingProgress").text("Requesting Matching");
                var matchData = {ids:pending,s:currentSid};
                hubc.server.matchSanta(matchData);
            });
        }catch(e){
            console.log("match Catch e",e);
            $("#matchingProgress").text(e.message);
            window.parent.notifySnack("error",e.message,null,progress = false)
        }

    }

我一无所知-可能很简单,但除非SignalR有一些独特之处,否则我看不出来会是什么.....

任何帮助表示赞赏!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。