Link to home
Start Free TrialLog in
Avatar of Jose Bredariol
Jose BredariolFlag for Brazil

asked on

How to Send Email from VB2008 with attachment thru gmail account ?

How can i  send an Email with attach thru Gmail account
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jose Bredariol

ASKER

I'm receiving an error with this code :
        Dim Msg As New MailMessage
        Dim smtp As New SmtpClient("smtp.gmail.com", 587)
        smtp.Credentials = New Net.NetworkCredential("account@gmail.com", "passw")
        smtp.EnableSsl = True
        Msg.Sender = New MailAddress("account@gmail.com", "ALIAS")
        Msg.To.Add(New MailAddress("Toemail@hotmail.com"))
        Msg.Subject = "subject"
        Msg.Body = "body"
        Msg.Attachments.Add(New Attachment("C:\M00758.txt"))
        Msg.From = Msg.Sender
        smtp.Send(Msg)   - error coul'd send message
        MsgBox("done")
What's wrong ?
Thanks
which error?
Sorry, I saw your error.

Can you provide the exact exception message?

Have you tried port 465?
I've tried with 465 too, but I'm receiving "Time Limited Exceed"
I tried, but I've got the same error.
        mail.From = New MailAddress("xxxxx@gmail.com", "SBA")
        mail.To.Add("yyyyyy@hotmail.com")
        mail.Subject = "XSubject"
        mail.Body = "XMessage"
        mail.IsBodyHtml = True
        mail.ReplyTo = New MailAddress("any email")  
        smtp.Host = "smtp.gmail.com"
        smtp.Port = 587  - tried with 25 and 465 too
        smtp.EnableSsl = True
        smtp.Credentials = New System.Net.NetworkCredential("accountgmail@gmail.com", "pass")
        smtp.Send(mail)  - Error here

Any other Ideia ?  Thanks
I just tried your code on port 587 and it is working fine for me (I removed the ReplyTo because it is of no use)

The other thing I can think of are:
-invalid credentials
-invalid "From"
-Your firewall is blocking you
>The other thing I can think of are:
-invalid credentials
-invalid "From"
-Your firewall is blocking you<

Invalid credentials gets you a 5.5.1 not authorized.  On gmail, the from address is taken from the credentials for security (to prevent sending spam)  and a firewall problem should result in a "Unable to connect to the remote server" type of error.

It would definitely help to know the exact exception we are dealing with.  Wrap your code in a try catch like this, and post back what exception is printed in the immediate window.
Try
    mail.From = New MailAddress("xxxxx@gmail.com", "SBA")
    mail.To.Add("yyyyyy@hotmail.com")
    mail.Subject = "XSubject"
    mail.Body = "XMessage"
    mail.IsBodyHtml = True
    mail.ReplyTo = New MailAddress("any email")
    smtp.Host = "smtp.gmail.com"
    smtp.Port = 587  '- tried with 25 and 465 too
    smtp.EnableSsl = True
    smtp.Credentials = New System.Net.NetworkCredential("accountgmail@gmail.com", "pass")
    smtp.Send(mail)  '- Error here
Catch ex As SmtpException
    Debug.WriteLine(ex)
End Try

Open in new window

Look to see if it's what you want.
Thanks

---------------------------------------------------------
System.Net.Mail.SmtpException: Falha ao enviar email. ---> System.Net.WebException: Impossível conectar-se ao servidor remoto ---> System.Net.Sockets.SocketException: Nenhuma conexão pôde ser feita porque a máquina de destino as recusou ativamente 209.85.133.109:587
   em System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   em System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
   em System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- Fim do rastreamento de pilha de exceções internas ---
   em System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
   em System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
   em System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
   em System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
   em System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   em System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   em System.Net.Mail.SmtpClient.GetConnection()
   em System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- Fim do rastreamento de pilha de exceções internas ---
   em System.Net.Mail.SmtpClient.Send(MailMessage message)
   em CET2008.Frmemail.Button2_Click(Object sender, EventArgs e) na E:\Junior\SISTEMAS\CET_vb6\CET2008.NET\Frmemail.vb:linha 80
have you seen http://www.systemnetmail.com/faq/5.2.aspx

and maybe you can get even more details by using this execption handler: http://www.systemnetmail.com/faq/5.aspx
Sounds to me like Gmail may be blocking you.  They do have some limitations in number of emails sent per time period and things like that to limit spammers.  Log in to Gmail via the web interface and see if any of your messages were sent, and if you received any bouncebacks or terms of service messages.
Now It's working. Thanks to all