使用smtp SendAsync发送邮件 [英] Send mail with smtp SendAsync

查看:489
本文介绍了使用smtp SendAsync发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是当我需要发送电子邮件时给我错误的方式.但是,这给我带来的错误是:

this is how when I need to send email gives me error. But the mistake that since gives me is this:

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

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

自MVC以来,我已经积累了很多知识,并且使用类来跟踪页面的各个区域.我之所以使用SendAsync,恰恰是因为它发送电子邮件等的速度要快一些.

I have accumulated since the MVC and have used class to keep track of ie areas of the page. The reason I have used SendAsync is precisely that it goes a little faster to send email, etc..

仅当我尝试向用户发送电子邮件时,才会发生此错误.

This error only happens when I try to send email to users.

public static void NewPassword(string mail, string name, string password)
    {
        MailDefinition oMailDefinition = new MailDefinition();
        oMailDefinition.BodyFileName = "~/MailList/emailskabelon/NewPassword.html";
        oMailDefinition.From = FromMail;

        Dictionary<string, string> oReplacements = new Dictionary<string, string>();
        oReplacements.Add("<<navn>>", name);
        oReplacements.Add("<<password>>", password);

        System.Net.Mail.MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl());
        oMailMessage.Subject = NewpasswordTitle + WebsiteName;
        oMailMessage.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient(AzureApi);
        System.Net.NetworkCredential netcred = new System.Net.NetworkCredential(AzureName, AzurePassword);
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;

        smtp.Credentials = netcred;
        smtp.Port = Convert.ToInt32("25");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

        using (var smtpClient = new SmtpClient())
        {
            smtp.SendAsync(oMailMessage, null);
        }
    }

我试图这样做:

public static async NewPassword(string mail, string name, string password)
        {
            ....
            using (var smtpClient = new SmtpClient())
            {
                await smtp.SendAsync(oMailMessage, null);
            }

我在这里看到: https://stackoverflow.com/a/35212320/7391454

推荐答案

将您的方法更改为:

public async Task SendEmail(string toEmailAddress, string emailSubject, string emailMessage)
{
var message = new MailMessage();
message.To.Add(toEmailAddress);

message.Subject = emailSubject;
message.Body = emailMessage;

using (var smtpClient = new SmtpClient())
{
    await smtpClient.SendMailAsync(message);
}
}

并这样称呼它:

var task = SendEmail(toEmailAddress, emailSubject, emailMessage);
var result = task.WaitAndUnwrapException();

在这里看看异步使用C#发送电子邮件吗? 在这里如何在C#中从同步方法调用异步方法?

这篇关于使用smtp SendAsync发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆