gpt4 book ai didi

.net - 在 Visual Studio 2005 : Problems sending E-Mail 中将旧代码从 System.Web.Mail 更新为 System.Net.Mail

转载 作者:行者123 更新时间:2023-12-03 17:22:06 28 4
gpt4 key购买 nike

使用过时的 System.Web.Mail 发送电子邮件工作正常,这是代码片段:

 Public Shared Sub send(ByVal recipent As String, ByVal from As String, ByVal subject As String, ByVal body As String)
Try
Dim Message As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
Message.To = recipent
Message.From = from
Message.Subject = subject
Message.Body = body
Message.BodyFormat = MailFormat.Html
Try
SmtpMail.SmtpServer = MAIL_SERVER
SmtpMail.Send(Message)
Catch ehttp As System.Web.HttpException
critical_error("Email sending failed, reason: " + ehttp.ToString)
End Try
Catch e As System.Exception
critical_error(e, "send() in Util_Email")
End Try
End Sub

这是更新的版本:
Dim mailMessage As New System.Net.Mail.MailMessage()

mailMessage.From = New System.Net.Mail.MailAddress(from)
mailMessage.To.Add(New System.Net.Mail.MailAddress(recipent))

mailMessage.Subject = subject
mailMessage.Body = body

mailMessage.IsBodyHtml = True
mailMessage.Priority = System.Net.Mail.MailPriority.Normal

Try

Dim smtp As New Net.Mail.SmtpClient(MAIL_SERVER)
smtp.Send(mailMessage)

Catch ex As Exception

MsgBox(ex.ToString)

End Try

我尝试了许多不同的变体,但似乎没有任何效果,我觉得这可能与 SmtpClient 有关,这些版本之间的底层代码是否发生了变化?

没有被抛出的异常。

最佳答案

System.Net.Mail 库使用配置文件来存储设置,因此您可能只需要添加这样的部分

  <system.net>
<mailSettings>
<smtp from="test@foo.com">
<network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>

关于.net - 在 Visual Studio 2005 : Problems sending E-Mail 中将旧代码从 System.Web.Mail 更新为 System.Net.Mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321/

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