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

时间:2012-01-06 09:29:10

标签: asp.net vb.net

这是网络配置

 <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

3 个答案:

答案 0 :(得分:4)

首先,建议使用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>

这会将您的方法缩小为:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您需要设置使用smtpserverport属性

通过gmail发送时使用的端口号

我认为这是谷歌的587

另外我认为gmail需要SSL连接,您可以使用smtpusessl属性设置