1

This is web config

 <appSettings>
     <add key="SmtpServer" value="gmail.com"/>
     <add key="SmtpUtilisateur" value="[email protected]"/>
     <add key="SmtpPassword" value="12345678"/> 
 </appSettings>

This my vb method

 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 = "[email protected]"
    Message.To = "[email protected]"
    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

3
  • possible duplicate of Sending email in .NET through Gmail Commented Jan 6, 2012 at 9:31
  • @SteveB not an exact duplicate since OP uses SmtpMail instead of SmtpClient, which doesn't support SSL at all, afaik. Commented Jan 6, 2012 at 9:45
  • 1
    The OP mentions he wants to send an email. Not he wants to use SmtpMail. I guess the issue is to find a way to send email with gmail whatever the way is, and that issue is addressed in the suggested duplicate Commented Jan 6, 2012 at 9:48

3 Answers 3

4

First, it is recommended to use System.Net.Mail instead of SmtpMail, since the latter has been declared obsolete by Microsoft.

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 = "[email protected]"
    Message.To = "[email protected]"
    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:

<system.net>
   <mailSettings>
      <smtp from="[email protected]">
         <network host="smtp.gmail.com" 
                  password="12345678" 
                  userName="[email protected]"
                  enableSsl="true"
                  port=587/>
      </smtp>
   </mailSettings>
</system.net>

This would reduce your method to:

Sub SendSimpleMail()

    Dim Message As New Mail.MailMessage()
    Message.To = "[email protected]"
    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
Sign up to request clarification or add additional context in comments.

1 Comment

Without any further information I can't tell what's wrong. Please consult the link @SteveB provided in the comment to you question or google "asp.net send mail through gmail".
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;
}

1 Comment

Doesn't look too much like VB.NET to me ;-)
0

You will need to set the port number to be used when sending via gmail using the smtpserverport property

I think this is 587 for Google

Also I believe that gmail will require an SSL connection which you can set using the smtpusessl property

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.