使用过时的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有关,这些版本之间的底层代码有什么变化吗?
没有抛回的异常。
发布于 2008-09-09 17:13:47
我已经测试了你的代码,我的邮件已经成功发送了。假设您对旧代码使用相同的参数,我建议您的邮件服务器(MAIL_SERVER)正在接受该消息,并且在处理过程中存在延迟,或者它认为它是垃圾邮件并将其丢弃。
我建议使用第三种方式发送消息(telnet,如果你觉得自己很勇敢),看看是否成功。
编辑:我注意到(从您随后的回答中)指定端口在一定程度上有所帮助。您还没有说明您使用的是端口25 (SMTP)还是端口587 (提交)或其他端口。如果您还没有这样做,那么使用sumission端口也可以帮助您解决问题。
Wikipedia和rfc4409有更多细节报道。
发布于 2008-09-09 17:14:22
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>发布于 2008-09-09 17:10:26
你有没有试过添加
smtp.UseDefaultCredentials = True 在发送之前?
另外,如果您尝试更改以下内容会发生什么:
mailMessage.From = New System.Net.Mail.MailAddress(from)
mailMessage.To.Add(New System.Net.Mail.MailAddress(recipent))要这样做:
mailMessage.From = New System.Net.Mail.MailAddress(from,recipent)--凯文·费尔柴尔德
https://stackoverflow.com/questions/52321
复制相似问题