1

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

 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

FrankSharp
  • 2,302
  • 10
  • 33
  • 48
  • possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Steve B Jan 06 '12 at 09:31
  • @SteveB not an exact duplicate since OP uses `SmtpMail` instead of `SmtpClient`, which doesn't support SSL at all, afaik. – Dennis Traub Jan 06 '12 at 09: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 – Steve B Jan 06 '12 at 09:48

3 Answers3

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 = "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:

<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
Dennis Traub
  • 46,924
  • 7
  • 81
  • 102
  • 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". – Dennis Traub Jan 06 '12 at 10:22
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;
}
Ashfaq Shaikh
  • 1,538
  • 11
  • 20
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

Matt Wilko
  • 25,893
  • 10
  • 85
  • 132