gpt4 book ai didi

asp.net - smtp客户端 `SendAsync()`方法

转载 作者:行者123 更新时间:2023-12-02 17:02:39 26 4
gpt4 key购买 nike

 Protected Sub btnLocalSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLocalSubmit.Click

Dim logic = New connections
logic.emailsToSend(User.Identity.Name, getURL, reportedBy)
SendAsync()
Response.Redirect(getRedirectionPath, False)
Catch ex As Exception
Response.Write(ex.Message)
Finally
_con.Close()
_con.Dispose()
_sqlComm.Dispose()

End Try
End Sub

Sub SendAsync()

Dim _con As New SqlConnection(ConfigurationManager.ConnectionStrings("CitizenJDBConnectionString").ConnectionString)
Dim _sqlDataAdapter As New SqlDataAdapter("SELECT * FROM EmailSender", _con)
Dim _table As New System.Data.DataTable

Try
_con.Open()
_sqlDataAdapter.Fill(_table)
_con.Close()

For i As Integer = 0 To _table.Rows.Count - 1


Dim AppPath As String = Request.PhysicalApplicationPath

Dim sr As New StreamReader(AppPath & "EmailTemplates/NewReport.txt")
Dim message As New MailMessage()

message.IsBodyHtml = True

message.From = New MailAddress("admin@xxxx.com")

message.To.Add(New MailAddress(_table.Rows(i).Item(1)))


message.Subject = "New User registration !"

message.Body = sr.ReadToEnd()

sr.Close()

message.Body = message.Body.Replace("<%ReporterName%>", _table.Rows(i).Item(3))

message.Body = message.Body.Replace("<%ReportURL%>", _table.Rows(i).Item(2))

Dim client As New SmtpClient()
client.Host = "smtp.xxxxx.com"
'smtp.gmail.com
client.Port = 25
client.UseDefaultCredentials = True
client.Credentials = New System.Net.NetworkCredential("admin@xxxx.com", "123456")
'client.EnableSsl = True
Dim userState As Object = message

'wire up the event for when the Async send is completed
AddHandler client.SendCompleted, AddressOf SmtpClient_OnCompleted

client.SendAsync(message, userState)

Next

Catch ex As Exception
Response.Write(ex.Message)
End Try


End Sub 'SendAsync

Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
'Get the Original MailMessage object
Dim message As MailMessage = CType(e.UserState, MailMessage)

'write out the subject
Dim subject As String = message.Subject

If e.Cancelled Then
Console.WriteLine("Send canceled for mail with subject [{0}].", subject)
End If
If Not (e.Error Is Nothing) Then
Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString())
Else
Console.WriteLine("Message [{0}] sent.", subject)
End If
End Sub 'SmtpClient_OnCompleted

我正在使用 smtp 客户端 SendAsync() 函数异步发送电子邮件...但是这个函数不起作用...为什么?我没有收到任何电子邮件..当我同步发送时...我收到了电子邮件,这意味着我的设置是正确的...那么 SendAsync() 方法有什么问题?

最佳答案

我一直这样做,经过多年这样做,我建议对您的问题采用 (2) 折解决方案:

  1. 实际发送电子邮件代码(System.Net 内容)重构为 WCF 服务或单独的 .dll(我更喜欢该服务)。
  2. 继续从 ASP.NET 页面使用异步委托(delegate)调用,但以“即发即忘”的方式执行此操作,无需连接任何回调。

您可能会说您需要知道发送电子邮件时是否出现问题。让发送电子邮件的 WCF 服务来处理此问题。在服务中进行任何日志记录。毕竟你真正做的就是记录。如果您需要在电子邮件失败时更好地处理工作流程,可以通过多种方式标记您的 ASP.NET,但我认为您会发现,一旦发送电子邮件的服务稳定,您将很少遇到问题。

事实上,多年来我一直使用 ASP.NET 调用的服务来发送电子邮件,并且已经发送了数以万计的不同电子邮件,并且这种即发即忘的设计从未遇到过任何问题。

最后,设置页面 Async=True 通过阻塞主线程直到所有异步进程完成,最终使页面整体同步运行。这可能会使页面加载速度非常慢,通常是不需要的。

关于asp.net - smtp客户端 `SendAsync()`方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9756443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com