Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net在vb.net中发送邮件_Asp.net_Vb.net - Fatal编程技术网

asp.net在vb.net中发送邮件

asp.net在vb.net中发送邮件,asp.net,vb.net,Asp.net,Vb.net,这是网页配置 <appSettings> <add key="SmtpServer" value="gmail.com"/> <add key="SmtpUtilisateur" value="superman@gmail.com"/> <add key="SmtpPassword" value="12345678"/> </appSettings> 我收到一个类似“连接到服务器时传输失败”的错误

这是网页配置

 <appSettings>
     <add key="SmtpServer" value="gmail.com"/>
     <add key="SmtpUtilisateur" value="superman@gmail.com"/>
     <add key="SmtpPassword" value="12345678"/> 
 </appSettings>
我收到一个类似“连接到服务器时传输失败”的错误

我不知道为什么这不好用

谢谢你的帮助


在vb.net中,首先,建议使用
System.net.Mail
而不是
SmtpMail
,因为后者已被Microsoft宣布为过时

其次,Gmail SMTP服务器需要一个安全连接,可以使用
SmtpClient.EnableSsl设置该连接。

您的示例可以更改为以下内容:

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将自动相应地配置自身:

<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>

首先,建议使用
System.Net.Mail
而不是
SmtpMail
,因为后者已被Microsoft宣布为过时

其次,Gmail SMTP服务器需要一个安全连接,可以使用
SmtpClient.EnableSsl设置该连接。

您的示例可以更改为以下内容:

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将自动相应地配置自身:

<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>

您需要使用
smtpserverport
属性设置通过gmail发送时使用的端口号

我想这是谷歌的587


此外,我认为gmail需要SSL连接,您可以使用
smtpusessl
属性设置SSL连接。您需要设置使用
smtpserverport
属性通过gmail发送时使用的端口号

我想这是谷歌的587


另外,我相信gmail需要一个SSL连接,你可以使用@SteveB的
smtpusessl
属性设置它。可能的@SteveB副本并不完全相同,因为OP使用
SmtpMail
而不是
SmtpClient
,后者根本不支持SSL,好吧。OP提到他想发送一封电子邮件。他不想使用
SmtpMail
。我想问题是要找到一种用gmail发送电子邮件的方法,不管是什么方式,这个问题都在建议的副本中解决了@SteveB的可能副本不是完全相同的副本,因为OP使用
SmtpMail
,而不是
SmtpClient
,后者根本不支持SSL,好吧。OP提到他想发送一封电子邮件。他不想使用
SmtpMail
。我想问题是要找到一种用gmail发送电子邮件的方法,不管是哪种方式,这个问题都会在建议的副本中解决,在我看来,它与VB.NET不太相似;-)在我看来不太像VB.NET;-)没有任何进一步的信息,我说不出是怎么回事。请参考“给你的评论”问题中提供的@SteveB链接或谷歌“asp.net通过gmail发送邮件”。如果没有任何进一步的信息,我就说不出有什么问题。请参考问题评论中提供的@SteveB链接或谷歌“asp.net通过gmail发送邮件”。
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;
}