Posts
256
Comments
463
Trackbacks
17
Sending .NET EMail Via smtp.gmail.com

I just finished wrapping up an article on how to send email from within a Windows or Web application with the System.Net.Mail namespace. One of the things I uncovered is that you can send email through the gmail smtp server. Of course you need to have a GMail account first...


Imports System.Net.Mail

'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()

'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("from_address_here@gmail.com")

'To is a collection of MailAddress types
MyMailMessage.To.Add("to_address_here@domain_name_here")

MyMailMessage.Subject = "GMail Test"
MyMailMessage.Body = "This is the test text for Gmail email"

'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("account uid", " account pwd")
SMTPServer.EnableSsl = True

Try
   SMTPServer.Send(MyMailMessage)
   MessageBox.Show("Email Sent")
Catch ex As SmtpException
   MessageBox.Show(ex.Message)
End Try

Just thought I'd pass that little tidbit on.

Have a day. :-|

posted on Monday, July 16, 2007 12:17 PM Print
Comments
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
David
7/16/2007 6:22 PM
Thats an interesting way to do it. What namespace is SmtpClient in?
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Jim Duffy
7/16/2007 10:09 PM
Hi David,

SmtpClient is located in the same namespace as MailMessage and MailAddress, SYSTEM.NET.MAIL.

I've revised the code snippet to include the IMPORTS statement.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
David
7/16/2007 11:30 PM
Hello Jim,

Thanks for the update. Hope you have a good day! :P

David
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Mortaza Doulaty
7/24/2007 11:12 AM
Hi Jim,

Thanks for the code! It really helped me...
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Kris
8/6/2007 4:09 AM
Why does it always say FAILURE SENDING MAIL for me?
I can't figure out what the problem is...
I put my Gmail account setting in. But its not working...
I dont think it can connect to the Gmail server.
Any ideas?
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Jim Duffy
8/6/2007 7:48 AM
Hi Kris,

Are you using valid email addresses in the From and To lines?

Are you sure you have your Google account User id and password correct?
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Lokesh
9/4/2007 7:39 AM
hello sir how r u

sir i use code that is working accurately but there is problem in web.config so please tell me if i want to receive feedback on my gmail or yahoo or rediffmail account then what is host and port and username and password.

Reply soon


Thank You.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Lokesh
9/10/2007 7:19 AM
Hello Sir

I use the same code that is above, the difference is that i have done the setting of host, port, username and password in web.config but it gives error that Failure Sending Mail. Please reply me soon, what is the problem.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Matty
11/29/2007 10:04 PM
Regardless of the "From Address" that I specify on this line:

MyMailMessage.From = New MailAddress("from_address_here@gmail.com")

the reply address of the received email is my gmail address.

This means that replies will come to me rather than going back to the user.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Niraj Tiwary
2/13/2008 5:37 AM
Great Work!
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
ohad raz
3/14/2008 10:32 PM
thanks allot man
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
laffin
4/3/2008 10:39 AM
Thank you for this ... i'm looking for ...
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Gunwant
4/18/2008 9:10 AM
Hi ,

I am using gmail smtp server to send notifications from my web application to users.

I am able to send notificatiopns to various email addresses , but those are not formatted .

All body message is comming in one line.

Please can you help me formatting the mail.

Regards
Gunwant
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
mahen
4/20/2008 7:52 PM
its says operation timeout

//this is wut i did

'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()

'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("mahj85@gmail.com")

'To is a collection of MailAddress types
MyMailMessage.To.Add("notus_d@yahoo.com")

MyMailMessage.Subject = "hello"
MyMailMessage.Body = "hiiiiiiiiiii"

'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("mahj85@gmail.com", "mypass")
SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
Label1.Text = "Email Sent"
Catch ex As SmtpException
Label1.Text = ex.Message
End Try
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
zonianos
4/28/2008 2:05 PM
Great Work!!!Thanks a lot!!!
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Chris
5/2/2008 4:07 AM
I am getting the same error as mahen, can't seem to figure out why it is timing out. Does anyone have any ideas? Thanks for any help.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Chris
6/24/2008 6:33 AM
I use this code


Dim insMail As New MailMessage()
Dim Smtp As New SmtpClient("smtp.gmail.com", 465)

insMail.From = New MailAddress("mymail@gmail.com")
insMail.To.Add(New MailAddress("othermail@yahoo.com"))
insMail.Subject = "Testing message"
insMail.Body = "i hope it delivered"

Smtp.UseDefaultCredentials = False
Smtp.DeliveryMethod = SmtpDeliveryMethod.Network
Smtp.Timeout = 0
Smtp.EnableSsl = True
Smtp.Credentials = New System.Net.NetworkCredential("mymail@gmail.com", "mypassword")
Smtp.Send(insMail)


but i get error message
"The operation has timed out"
can anyone explained this to me???
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Rajeesh
7/1/2008 8:10 AM
Try setting the port number to 587
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
kaew
7/7/2008 10:34 AM
Thank you very much for code "Sending .NET EMail Via smtp.gmail.com ".
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Chris
7/9/2008 7:19 AM
If i use port 587
but i get error message
"Failure sending mail."

I try this setting to outlook with port 465, and it's work
but if i use port 587 it's not work.

what is the different with port 465 and 587 ?

Thank's
Gravatar
# Sending .NET EMail Via smtp.gmail.com
Chris
7/9/2008 7:21 AM
If i use port 587, i get error message : "Failure sending mail."
I try this setting to outlook with port 465, and it's work
but if i use port 587 it's not work.
what is the different within port 465 and 587 ?

Thank's
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Chris
7/9/2008 7:22 AM
if i use port 587, i get error message : "Failure sending mail."

I try this setting to outlook with port 465, and it's work
but if i use port 587 it's not work.

what is the different within port 465 and 587 ?

Thank's
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
ritesh
7/21/2008 2:35 AM
thanks , all of u, i can send mail using given code. when i send mail , it always goes from dummy@gmail.com , i just change objectmail.from ="abc@abc.com" but not get success. if possible help me , how can hide sender mail id?
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
ritesh
7/28/2008 3:44 AM
any on can help me , i can send mail using given code. when i send mail , it always goes from dummy@gmail.com , i just change objectmail.from ="abc@abc.com" but not get success. if possible help me , how can hide sender mail id?
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Prasath
7/30/2008 2:30 AM
hai i have an smtp server and i have an id xyz@xyz.com when i send email through asp.net to receiver, receive email but the from address shoud be abc@abc.com i dont want to display xyz@xyz.com any one please help me
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
devendra
8/21/2008 3:27 AM
Error; operation ha been timeout.
comes when i,am sending mail through loop in which i take one by one id and send mail.i have 100 emailid's to which .i want tosend mail.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Distnie
8/26/2008 7:54 AM
hi
this is not working plz any1 help me plz
this is wat i wrote:
'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()

'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("iyddeaz@gmail.com")

'To is a collection of MailAddress types
MyMailMessage.To.Add("iyddeaz@gmail.com")

MyMailMessage.Subject = "GMail Test"
MyMailMessage.Body = "This is the test text for Gmail email"

'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("iyddeaz@gmail.com", "mypass")
SMTPServer.EnableSsl = True

Try
SMTPServer.Send(MyMailMessage)
MessageBox.Show("Email Sent")
Catch ex As SmtpException
MessageBox.Show(ex.Message)
End Try



i have used valid username and password

plz help me
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
shane
9/19/2008 5:12 AM
hey i too get the same error mail send failure
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
smalltalk
9/25/2008 7:00 AM
i can't send email. always timeout. I don't understand. if anyone can send. please post code. thanks
Gravatar
#  Amazing coding: Sending .NET EMail Via smtp.gmail.com
Rathakrishnan
10/13/2008 7:49 AM
Really superb and simple coding for sending Email through vb.net. Great work.

Great Work!!!Thanks a lot!!!
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
happy11
11/4/2008 3:32 AM
same code as above...

but i have error...

" Failure sending mail. "

When i traced the code.. The error occurs
on line

SMTPServer.Send(MyMailMessage)

plz reply.. tnx
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Steve
12/10/2008 6:53 PM
I too am grateful for this bit of code. I seem to have a different problem than those who have posted here. If I use port 465 it times out and I get an error that it timed out, but if I use port 587 it appears to send successfully. The problem is the e-mail never actually gets to the recipient. Anyone having this problem or know of a solution?

Thanks,

Steve
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Gowdhaman
12/22/2008 7:02 AM
Hi
Good work...

Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
onetrakmindz
12/26/2008 2:24 PM
Excellent work.
Gravatar
# SMTP Using Gmail
Vijay Sekar
1/21/2009 7:14 AM
Thanks a lot.
Its working fine.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
Pix3l
1/23/2009 4:27 PM
Make sure you enable POP within your gmail account. If you don't then you will get the timeout. Hope this solves the timeout issue.
Gravatar
# re: Sending .NET EMail Via smtp.gmail.com
yogesh
2/11/2009 10:13 AM
you cannot change or hide your id in the send mail from this above method.
Comments have been closed on this topic.