【问题标题】:asp.net send mail in vb.netasp.net 在 vb.net 中发送邮件
【发布时间】:2012-01-06 09:29:10
【问题描述】:

这是网络配置

 <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

我收到类似“连接到服务器的传输失败”的错误

我不知道为什么这不好用......

谢谢你帮助我!

vb.net 中的这个

【问题讨论】:

  • @SteveB 不是完全重复的,因为 OP 使用 SmtpMail 而不是 SmtpClient,它根本不支持 SSL,afaik。
  • OP 提到他想发送一封电子邮件。不是他想使用SmtpMail。我想问题是要找到一种使用 gmail 发送电子邮件的方法,并且该问题已在建议的副本中得到解决

标签: asp.net vb.net


【解决方案1】:

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

其次,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>

这会将您的方法简化为:

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

【讨论】:

  • 没有任何进一步的信息,我无法判断出了什么问题。请查阅评论中提供给您的问题的链接@SteveB 或谷歌“asp.net 通过 gmail 发送邮件”。
【解决方案2】:
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;
}

【讨论】:

  • 在我看来不太像 VB.NET ;-)
【解决方案3】:

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

我认为这是 Google 的 587

我也相信 gmail 需要一个 SSL 连接,您可以使用 smtpusessl 属性进行设置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多