[英]asp.net send mail in vb.net
This is web config 这是网络配置
<appSettings>
<add key="SmtpServer" value="gmail.com"/>
<add key="SmtpUtilisateur" value="superman@gmail.com"/>
<add key="SmtpPassword" value="12345678"/>
</appSettings>
This my vb method 这是我的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... 我不知道为什么这不好用......
Thank's for helping me! 谢谢你的帮助!
This in vb.net 这在vb.net中
First, it is recommended to use System.Net.Mail
instead of SmtpMail
, since the latter has been declared obsolete by Microsoft. 首先,建议使用
System.Net.Mail
而不是SmtpMail
,因为后者已被Microsoft宣布为过时。
Second, the Gmail SMTP server requires a secure connection which can be set using SmtpClient.EnableSsl.
其次,Gmail SMTP服务器需要一个可以使用
SmtpClient.EnableSsl.
设置的安全连接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
If you replace the appsettings
in the web.config with the following specific block, the SmtpClient will automatically configure itself accordingly: 如果使用以下特定块替换web.config中的
appsettings
,SmtpClient将自动相应地自行配置:
<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
MailMessage msg = new MailMessage {From = new MailAddress(txtGAddress.Text, “Sender’s Name”)};
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient
{
Host = “smtp.gmail.com”,
Credentials = new NetworkCredential(txtGAddress.Text, txtGPassword.Text),
Port = 587,
EnableSsl = true
};
Label1.Visible = true;
try
{
smtp.Send(msg);
Label1.Text = “Email sent accessfully.”;
}
catch (Exception exeption)
{
Label1.Text = exeption.Message;
}
You will need to set the port number to be used when sending via gmail using the smtpserverport
property 您需要设置使用
smtpserverport
属性通过gmail发送时使用的端口号
I think this is 587 for Google 我认为谷歌这是587
Also I believe that gmail will require an SSL connection which you can set using the smtpusessl
property 另外我相信gmail需要一个SSL连接,你可以使用
smtpusessl
属性设置它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.