[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-19
14
Medium Priority
?
1,131 Views
Last Modified: 2013-11-27
How can i  send an Email with attach thru Gmail account
0
Comment
Question by:Jose Bredariol
  • 6
  • 5
  • 3
14 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23685464
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 1000 total points
ID: 23685566
You have to set credentials, use port 465 or 587 and enable SSL.  Aside from that I think everything else is standard.

http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Imports System.Net.Mail
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
 
        Dim smtp As New SmtpClient("smtp.gmail.com", 587)
        smtp.Credentials = New Net.NetworkCredential("gmail.user@gmail.com", "password")
        smtp.EnableSsl = True
 
        Dim Msg As New MailMessage
        Msg.Sender = New MailAddress("gmail.user@gmail.com", "John Henry")
        Msg.To.Add(New MailAddress("recipient@recip.com"))
        Msg.Subject = "subject"
        Msg.Body = "body"
        Msg.Attachments.Add(New Attachment("C:\temp\test.txt"))
        Msg.From = Msg.Sender
 
 
 
        smtp.Send(Msg)
        MsgBox("done")
    End Sub
End Class

Open in new window

0
 

Author Comment

by:Jose Bredariol
ID: 23691128
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23691244
which error?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23691247
Sorry, I saw your error.

Can you provide the exact exception message?

Have you tried port 465?
0
 

Author Comment

by:Jose Bredariol
ID: 23691321
I've tried with 465 too, but I'm receiving "Time Limited Exceed"
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23691451
0
 

Author Comment

by:Jose Bredariol
ID: 23691641
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
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23691855
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
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 23692102
>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

0
 

Author Comment

by:Jose Bredariol
ID: 23692453
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
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23692556
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
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 23693560
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.
0
 

Author Comment

by:Jose Bredariol
ID: 23743610
Now It's working. Thanks to all
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question