locked
Sending email to Gmail by using SMTP Problem RRS feed

  • Question

  • I'm trying to send an email trough my Gmail account using this code

    Dim myMailMessage As Net.Mail.MailMessage = New Net.Mail.MailMessage("someone@gmail.com""someone@hotmail.com"
            myMailMessage.Subject = "Test1" 
            myMailMessage.Body = "this is test #1" 
            myMailMessage.Priority = Net.Mail.MailPriority.High 
            Dim mailSenderInstance As Net.Mail.SmtpClient = New Net.Mail.SmtpClient("smtp.gmail.com", 587) '25 is the port of the SMTP host      
            mailSenderInstance.Credentials = New System.Net.NetworkCredential("*******""******"
            Try 
                mailSenderInstance.Send(myMailMessage) 
            Catch ex As Exception 
                m.Text = "Error: " & ex.Message 
            End Try 
            myMailMessage.Dispose() 'Recommend disposing this object

    but it doesn't work and wants me to use TLS, which i don't know how!!!! Can anybody help me?

    NIMA
    Monday, July 21, 2008 12:19 PM

Answers

  • HI Nima,
    Try the following code, it looks same as your code but a bit different this works for me, hope it will work for u as well.

    Imports System.Net.Mail  
    Imports System.Text  
    Imports System.Net   
     
           Dim Message As New MailMessage  
            Message.To.Add("<TO EMAIL ID>")  
            Message.From = New MailAddress("<YOUR EMAIL ID>", "<YOUR NAME>", Encoding.UTF8)  
            Message.Subject = "<SUB>" 
            Message.SubjectEncoding = Encoding.UTF8  
            Message.Body = "<DATA to SEND>" 
            Message.BodyEncoding = Encoding.UTF8  
            Message.IsBodyHtml = False 
            Message.Priority = MailPriority.High  
            Dim client As New SmtpClient()  
            client.Credentials = New NetworkCredential("<YOUR EMAILID>", "<YOUR PASSWORD>")  
            client.Port = "<PORT NO>" 
            client.Host = "smtp.gmail.com" 
            client.EnableSsl = True 
            client.Send(Message) 
    Hope this helps, if it doesn't let us know wats the error.


    Regards,
    Rushabh D. Vipani.
    • Marked as answer by Nimaaa Monday, July 21, 2008 2:55 PM
    Monday, July 21, 2008 12:59 PM
  • Hi

    Have a look at this url and see if it helps.

    http://geekswithblogs.net/TakeNote/archive/2007/07/16/113974.aspx
    or
    http://forums.msdn.microsoft.com/en-US/vblanguage/thread/1272f8e8-1f46-4bc1-8435-a38406a571de/

    Alternatively try port no 465

    I hope this helps.
    Richard Hogan Development Manager SDT Ltd www.sdt.co.uk
    • Marked as answer by Nimaaa Monday, July 21, 2008 5:35 PM
    Monday, July 21, 2008 12:48 PM

All replies

  • Hi

    Have a look at this url and see if it helps.

    http://geekswithblogs.net/TakeNote/archive/2007/07/16/113974.aspx
    or
    http://forums.msdn.microsoft.com/en-US/vblanguage/thread/1272f8e8-1f46-4bc1-8435-a38406a571de/

    Alternatively try port no 465

    I hope this helps.
    Richard Hogan Development Manager SDT Ltd www.sdt.co.uk
    • Marked as answer by Nimaaa Monday, July 21, 2008 5:35 PM
    Monday, July 21, 2008 12:48 PM
  • HI Nima,
    Try the following code, it looks same as your code but a bit different this works for me, hope it will work for u as well.

    Imports System.Net.Mail  
    Imports System.Text  
    Imports System.Net   
     
           Dim Message As New MailMessage  
            Message.To.Add("<TO EMAIL ID>")  
            Message.From = New MailAddress("<YOUR EMAIL ID>", "<YOUR NAME>", Encoding.UTF8)  
            Message.Subject = "<SUB>" 
            Message.SubjectEncoding = Encoding.UTF8  
            Message.Body = "<DATA to SEND>" 
            Message.BodyEncoding = Encoding.UTF8  
            Message.IsBodyHtml = False 
            Message.Priority = MailPriority.High  
            Dim client As New SmtpClient()  
            client.Credentials = New NetworkCredential("<YOUR EMAILID>", "<YOUR PASSWORD>")  
            client.Port = "<PORT NO>" 
            client.Host = "smtp.gmail.com" 
            client.EnableSsl = True 
            client.Send(Message) 
    Hope this helps, if it doesn't let us know wats the error.


    Regards,
    Rushabh D. Vipani.
    • Marked as answer by Nimaaa Monday, July 21, 2008 2:55 PM
    Monday, July 21, 2008 12:59 PM
  • Hi Rushabh:

    I followed your example but I get a "Failure to send email" exception when trying to send. Here are the properties of my mail message and SMPT client:

    ?lsMail
    {System.Net.Mail.MailMessage}
        AlternateViews: Count = (Count) threw an exception of type System.TypeLoadException.
        Attachments: Count = (Count) threw an exception of type System.TypeLoadException.
        Bcc: Count = (Count) threw an exception of type System.TypeLoadException.
        Body: "Your password was recently changed. If this was not initiated by you, please contact the System Administrator immediately."
        BodyEncoding: {System.Text.UTF8Encoding}
        CC: Count = (Count) threw an exception of type System.TypeLoadException.
        DeliveryNotificationOptions: None {0}
        From: {"Venkat Venkataramanan" <myemail@MyEmailServer.com>}
        Headers: {System.Net.Mime.HeaderCollection}
        HeadersEncoding: Nothing
        IsBodyHtml: False
        Priority: High {2}
        ReplyTo: Nothing
        ReplyToList: Count = (Count) threw an exception of type System.TypeLoadException.
        Sender: Nothing
        Subject: "CTT: Password Change"
        SubjectEncoding: {System.Text.UTF8Encoding}
        To: Count = (Count) threw an exception of type System.TypeLoadException.
       
    ?lsSMTPClient
    {System.Net.Mail.SmtpClient}
        ClientCertificates: {System.Security.Cryptography.X509Certificates.X509CertificateCollection}
        clientDomain: "CTSNJY24592"
        Credentials: {System.Net.NetworkCredential}
        DeliveryMethod: Network {0}
        EnableSsl: True
        Host: "smtp.myemailserviceprovider.com"
        InCall: False
        MailConfiguration: {System.Net.Configuration.MailSettingsSectionGroupInternal}
        PickupDirectoryLocation: Nothing
        Port: 587
        ServicePoint: {System.Net.ServicePoint}
        TargetName: "SMTPSVC/"
        Timeout: 100000
        UseDefaultCredentials: False

    Can you tell me what I am doing wrong?

    venki


    • Edited by TheVenkster Monday, December 31, 2012 12:41 AM
    Monday, December 31, 2012 12:40 AM
  • By the way, the exception's inner message was "Unable to connect to the remote server"

    Venki

    Monday, December 31, 2012 1:13 AM
  • Kevin:

    This is the exception I get in my Command window when trying to execute your code with my credentials:

    Call back at 12/31/2012 4:17:57 PM token [test message1]

    [12/31/2012 4:18:08 PM] [test message1] [System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 173.194.73.108:587

       at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

       at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

       --- End of inner exception stack trace ---

       at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)

       at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)

       --- End of inner exception stack trace ---]

    Any ideas? I am able to sign onto Gmail with the same credentials. I am using "smtp.gmail.com" as the SMTP server and 587 as the port.

    Curiuously, I have the same account set on my Outlook. If I change the encryption from TLS to SSL, the test message does not get sent out. It reports an error.

    Venki

    Monday, December 31, 2012 9:49 PM
  • Hello Venki,

    I did a Google search on An attempt was made to access a socket in a way forbidden by its access permissions. Found a post where the reply stamped Tuesday, August 28, 2012 10:08 AM shows their issue from the text below. I am sure the code is fine as mentioned a while back, instead the issue is the same as the one below or close to this. A good example for me was I attempted to send SMTP messages from work and got a similar issue, talked to our network folks and they un-blocked a port, all worked at that point.

    i missed myclient.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpClient also(SmtpServer,port);

    But the actually problem was i ran this code on Windows7 in my office PC.. And i don't have priviledges to open a socket connection... :(  that was the problem finally i executed this code on Server(admin priviledge) and it worked.. :)


    KSG

    Monday, December 31, 2012 10:57 PM