asp.net在vb.net中发送邮件 [英] asp.net send mail in vb.net
问题描述
这是Web配置
<appSettings>
<add key="SmtpServer" value="gmail.com"/>
<add key="SmtpUtilisateur" value="superman@gmail.com"/>
<add key="SmtpPassword" value="12345678"/>
</appSettings>
这是我的vb方法
Sub SendSimpleMail()
Dim Message As New Mail.MailMessage
Dim utilisateur As String
Dim pass As String
Dim server As String
utilisateur = ConfigurationSettings.AppSettings("StmpUtilisateur")
pass = ConfigurationSettings.AppSettings("SmtpPassword")
server = ConfigurationSettings.AppSettings("SmtpServer")
Message.From = "superman@gmail.com"
Message.To = "superman@gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", utilisateur)
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtppassworld", pass)
SmtpMail.SmtpServer = server
Try
SmtpMail.Send(Message)
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
我收到诸如连接服务器时传输失败"之类的错误消息
I get an error like the "transport fail in connection to server"
我不知道为什么这不能正常工作...
I don't know why this is not work well...
感谢您的帮助!
在vb.net中
推荐答案
首先,建议使用 System.Net.Mail
代替 SmtpMail
,因为后者已被Microsoft宣布淘汰.
First, it is recommended to use System.Net.Mail
instead of SmtpMail
, since the latter has been declared obsolete by Microsoft.
第二,Gmail SMTP服务器需要安全的连接,可以使用 SmtpClient.EnableSsl进行设置.
Second, the Gmail SMTP server requires a secure connection which can be set using SmtpClient.EnableSsl.
您的示例可以更改为以下内容:
Your example could be changed to the following:
Sub SendSimpleMail()
Dim utilisateur As String = ConfigurationSettings.AppSettings("StmpUtilisateur")
Dim pass As String = ConfigurationSettings.AppSettings("SmtpPassword")
Dim server As String = ConfigurationSettings.AppSettings("SmtpServer")
Dim Message As New Mail.MailMessage()
Message.From = "superman@gmail.com"
Message.To = "superman@gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
' You won't need the calls to Message.Fields.Add()
' Replace SmtpMail.SmtpServer = server with the following:
Dim client As New SmtpClient(server)
client.Port = 587
client.EnableSsl = true
client.Credentials = new System.Net.NetworkCredential(utilisateur,pass);
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try
End Sub
如果将web.config中的 appsettings
替换为以下特定块,则SmtpClient将相应地自动进行自身配置:
If you replace the appsettings
in the web.config with the following specific block, the SmtpClient will automatically configure itself accordingly:
<system.net>
<mailSettings>
<smtp from="superman@gmail.com">
<network host="smtp.gmail.com"
password="12345678"
userName="superman@gmail.com"
enableSsl="true"
port=587/>
</smtp>
</mailSettings>
</system.net>
这会将您的方法减少为:
This would reduce your method to:
Sub SendSimpleMail()
Dim Message As New Mail.MailMessage()
Message.To = "superman@gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
Dim client As New SmtpClient()
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try
End Sub
这篇关于asp.net在vb.net中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!