Ayman Farouk

Microsoft Maniac Guy ..!
posts - 33, comments - 104, trackbacks - 58

My Links

News

Archives

Blogroll

Links

System.net.mail with Gmail account

Here is a small code for sending an email from ASP.NET 2.0 page, using the namespace System.net.mail and a Gmail account.

First of all we have to import these :

Imports System.Net.Mail

Imports System.Net.Mail.MailMessage

Imports System.Net.NetworkCredential

Then,

    Dim mail As New MailMessage()

    Dim msgBody As String

    Dim smtp As New SmtpClient

    mail.From = New MailAddress(“ur-gmail-account@gmail.com, "display name")

    mail.To.Add(“ur-email@host.com“)

    mail.Subject = “Subject“

    mail.Body = “msgBody“

    mail.IsBodyHtml = True ' This is to enable HTML in your email body

    mail.ReplyTo = New MailAddress(“reply-to-email-address“)  ' This is optional, it allows you to add Reply To email address.

   smtp.Host = "smtp.gmail.com"

   smtp.Port = 25

   smtp.EnableSsl = True

   smtp.Credentials = New System.Net.NetworkCredential(“ur-gmail-account@gmail.com, "gmail-password")

   smtp.Send(mail)

   lblFlag.Text = "Your Message has beent sent."

Note: We can build this email form AJAX based, by putting the content in an UpdatePanel control, and add the button “send” as a trigger.

 

A.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Thursday, March 09, 2006 7:08 PM |

Feedback

Gravatar

# re: System.net.mail with Gmail account

I think Gmail uses SMTP on port 465, did you try this code?
3/11/2006 11:40 AM | Khalil
Gravatar

# re: System.net.mail with Gmail account

The above code is working fine dude.

A.
3/11/2006 11:51 AM | Ayman Farouk
Gravatar

# re: System.net.mail with Gmail account

a Microsoft guy using gmail SMTP, LoooL
You are not going to use it in during your MAD demo, right? =p
4/2/2006 8:58 AM | Bander
Gravatar

# re: System.net.mail with Gmail account

of course am not gonna mention it during my MAD Demo :D .. hehe
4/2/2006 2:00 PM | Ayman Farouk
Gravatar

# re: System.net.mail with Gmail account

This code is not about GMail, it is the standard way for sending any email! However, I am glad they enhanced the mail library in .NET Framework 2.0, the old library sucked, there were no exceptions thrown at errors, the structure was not good at all, it was horrible!
4/15/2006 6:13 PM | Rami
Gravatar

# re: System.net.mail with Gmail account

ag areg aerg aerg aergaeh
5/15/2006 10:40 PM | aa
Gravatar

# re: System.net.mail with Gmail account

Very nice code. i tried it . it works fine. thanks dude for your code .
i rate it as 5.
7/6/2006 2:41 PM | Rajesh
Gravatar

# re: System.net.mail with Gmail account

Regarding above comment, the standard SSL port 465 won't work (nor did 25 for me). Port 587 is however working.
7/24/2006 10:41 AM | Simon
Gravatar

# re: System.net.mail with Gmail account

I've tried with Hotmail instead. But failed to send mail with error 'Failure sending mail'. i've aLready set domain name as 'hotmail.com' and host as 'smtp.hotmail.com' in my Default SMTP Virtual Server Properties. What did i do wrong? Can anyone help me with this??

Below are the codes:
Imports System.Net.Mail
Imports System.Net.Mail.MailMessage
Imports System.Net.NetworkCredential
Public Class Form1
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim mail As New MailMessage()
Dim msgBody As String
Dim smtp As New SmtpClient
mail.From = New MailAddress("<myEmail>@hotmail.com", "<myName>")
mail.To.Add("("<myEmail>@hotmail.com")
mail.Subject = "Subject"
mail.Body = "msgBody"
mail.IsBodyHtml = True
mail.ReplyTo = New MailAddress("<myemail>@hotmail.com")
smtp.Host = "smtp.hotmail.com"
smtp.Port = 25
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("
<myEmail>@hotmail.com", "<hotmailpassword>")
smtp.Send(mail)
End Sub
End Class
10/16/2006 2:59 PM | May [Beginner]
Gravatar

# re: System.net.mail with Gmail account

I am getting errors with the above code....any idea why?
5/24/2007 12:05 PM | Kabir
Gravatar

# re: System.net.mail with Gmail account

yes yes yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees it's work thank you thanks man i was search to this code and here i found it thaaaaaaaaanks if you be here i will kiss you!!!!!!!!
12/26/2007 5:44 AM | ameer
Gravatar

# re: System.net.mail with Gmail account

Thanks a lot man for saving me of many more sleepless nites.The code works grt.Thnks again.u rock....
1/22/2008 3:43 PM | sandy_the_loner
Gravatar

# re: System.net.mail with Gmail account

thank you very much ayman,that helped me alot, by the way i did the c# version

rabena yekremak
1/26/2008 7:24 AM | amr
Gravatar

# re: System.net.mail with Gmail account

Nice code. I have found in another site that google using port 465, but I never thought that it can work using port 25.

If I can add maybe u can add this in web.config place it inside <configuration></configuration tag>

<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" userName="your_username" password="your_ password" port="465"/>

</smtp>
</mailSettings>
</system.net>

In this file I'm using port 465 but it still can work.
1/28/2008 8:43 PM | Henri
Gravatar

# re: System.net.mail with Gmail account

Thanks A Lot Mr. Ayman , I have great benefit from your code , but i want to explain some thing for all , it works only with Gmail Account not with yahoo or hotmail and it will be valid with your own paid email or hosting.
3/18/2008 9:04 AM | Yosri Negm
Gravatar

# re: System.net.mail with Gmail account

Faruk bey cok ama cok tesekkur ederim, uzler site dolandim ama hic biri ise yarayan bir sey yazmamisdi.


Ne mutlu turkum diyene.
3/21/2008 9:32 PM | Ashkan
Gravatar

# re: System.net.mail with Gmail account

Please provide me full procedure how to implement this code on my website i don't have so much idea about this please help. I also want a form for the in the contact us page for the user.
6/16/2008 7:43 AM | Jack
Gravatar

# re: System.net.mail with Gmail account

Thanks man Ur code really Helped!!!!
8/23/2008 10:57 AM | Harshraj
Gravatar

# re: System.net.mail with Gmail account

Can you please tell me how to use the same code in Yahoo and Hotmail.

It is not working in any other mail server except Gmail.
10/29/2008 1:12 AM | Balachander Ganesan
Gravatar

# re: System.net.mail with Gmail account

Thanks. The code is working for me with port 25 and 587. port 465 is not working.
12/15/2008 9:48 PM | Divya
Gravatar

# re: System.net.mail with Gmail account

Hi Friends
in my application i want to accesses inbox of the gmail into my .net 2.0 application
i need the code for that either in vb or c#
can any one you please help me
1/1/2009 11:44 PM | Srinivasu
Gravatar

# re: System.net.mail with Gmail account

this code is not working for me it say's unable to connect server.
2/3/2009 5:09 AM | dheeraj
Gravatar

# re: System.net.mail with Gmail account

i m having error in sending mail through asp.net 3.5
error is "failure sending mails"

the code is
System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("murtazakhursheed@gmail.com",
"murtazakhursheed@hotmail.com",
"This is the mail subject",
"Just wanted to say Hello");

MyMailMessage.IsBodyHtml = false;

System.Net.NetworkCredential nt = new System.Net.NetworkCredential("murtazakhursheed@gmail.com", "entermein");
SmtpClient sc = new SmtpClient("smtp.gmail.com",465);
sc.Port = 25;
sc.EnableSsl = true;
sc.UseDefaultCredentials = false;
sc.Credentials = nt;
//sc.DeliveryMethod = SmtpDeliveryMethod.Network;
//sc.Timeout = 5;

sc.Send(MyMailMessage);



plz help me
3/13/2009 7:21 AM | murtaza
Gravatar

# re: System.net.mail with Gmail account

thank you soooo much
4/28/2009 11:22 AM | anass
Gravatar

# re: System.net.mail with Gmail account

Cool!
Thank you very much! It works good in Windows Forms.
5/6/2009 12:45 PM | Kasper
Gravatar

# re: System.net.mail with Gmail account

Please can someone help me with a smtp that will power my AMS Mass Sender.I have tried getting an SMTP but it has not been easy
5/16/2009 1:18 AM | Dolph Brown
Gravatar

# re: System.net.mail with Gmail account

Thanks Man
7/3/2009 2:47 AM | CODER
Gravatar

# re: System.net.mail with Gmail account

iam use the code





SmtpMail.SmtpServer = "webmail.vsvnpolytechnic.in";
MailMessage msg = new MailMessage();
msg.From = TextBox1.Text;
msg.To = "xxxxxxxxx@gmail.com";
msg.Subject =TextBox2.Text;
msg.Body=TextBox3.Text;
SmtpMail.Send(msg);
Response.Write("your mail has been sent successfully");



the error message is

The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.


How to send in local emailaddress(Example for Raja@yahoo.co.in,Raja@gmail.com)

Pls Help me
9/17/2009 6:52 AM | Buvaneswaran
Gravatar

# re: System.net.mail with Gmail account

my codeis


SmtpMail.SmtpServer = "webmail.vsvnpolytechnic.in";
MailMessage msg = new MailMessage();
msg.From = TextBox1.Text;
msg.To = "xxxxxxxxx@gmail.com";
msg.Subject =TextBox2.Text;
msg.Body=TextBox3.Text;
SmtpMail.Send(msg);
Response.Write("your mail has been sent successfully");



the error message is

The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.


How to send in local Different emailaddress(Example for Raja@yahoo.co.in,Raja@gmail.com)

Pls Help me.......... very urgent..........
9/18/2009 9:01 AM | Buvaneswaran
Gravatar

# re: System.net.mail with Gmail account

my codeis


SmtpMail.SmtpServer = "webmail.vsvnpolytechnic.in";
MailMessage msg = new MailMessage();
msg.From = TextBox1.Text;
msg.To = "xxxxxxxxx@gmail.com";
msg.Subject =TextBox2.Text;
msg.Body=TextBox3.Text;
SmtpMail.Send(msg);
Response.Write("your mail has been sent successfully");



the error message is

The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

How to send in local Different emailaddress(Example for Raja@yahoo.co.in,Raja@gmail.com)

Pls Help me.......... very urgent..........
9/23/2009 10:21 AM | Buvaneswaran
Gravatar

# re: System.net.mail with Gmail account

i jus wanna ask..what is network credentials..??..n y we need to put gmail username n password in it..??
10/8/2009 7:34 AM | raman
Gravatar

# re: System.net.mail with Gmail account

my codeis


SmtpMail.SmtpServer = "webmail.vsvnpolytechnic.in";
MailMessage msg = new MailMessage();
msg.From = TextBox1.Text;
msg.To = "xxxxxxxxx@gmail.com";
msg.Subject =TextBox2.Text;
msg.Body=TextBox3.Text;
SmtpMail.Send(msg);
Response.Write("your mail has been sent successfully");



the error message is

The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

How to send in local Different emailaddress(Example for Raja@yahoo.co.in,Raja@gmail.com)

Pls Help me.......... very urgent..........
9/23/2009 10:21 AM | Buvaneswaran
10/10/2009 3:48 AM | bhuvaneswaran
Gravatar

# re: System.net.mail with Gmail account

Hi All,

This is great for localhost, but when I deploy it throw's an inner exception in the webservice (doesn't happen in the localhost), and I have set the servicereferences.clientconfig correctly to point at the live sites webservice

Any help would be great, thanks in advance!
10/22/2009 11:05 PM | Heath
Gravatar

# re: System.net.mail with Gmail account

my codeis


SmtpMail.SmtpServer = "webmail.vsvnpolytechnic.in";
MailMessage msg = new MailMessage();
msg.From = TextBox1.Text;
msg.To = "xxxxxxxxx@gmail.com";
msg.Subject =TextBox2.Text;
msg.Body=TextBox3.Text;
SmtpMail.Send(msg);
Response.Write("your mail has been sent successfully");



the error message is

The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

How to send in local Different emailaddress(Example for Raja@yahoo.co.in,Raja@gmail.com)

Pls Help me.......... very urgent..........very urgent..........
10/27/2009 8:55 AM | bhuvaneswaran
Gravatar

# re: System.net.mail with Gmail account

Thanks,
This code is working fine for me on the port 587.
Got ma problem slved after all............
12/24/2009 2:09 PM | Muhammad Imran
Gravatar

# re: System.net.mail with Gmail account

Had trouble with this until I changed it to port 587. Code now works as in the O.P. Thanks Muhammad Imran for the tip!!
12/27/2009 3:59 PM | Casey
Gravatar

# re: System.net.mail with Gmail account

This is the mail system at host smtp247.uni5.net.
3/29/2010 7:54 AM | NAIR PANIZZON
Gravatar

# re: System.net.mail with Gmail account

Thank yo u dude. it helps me a lot.
6/3/2010 8:33 AM | Mamun
Gravatar

# re: System.net.mail with Gmail account

Thanks this code works perfectly , I'm using it with Google APPS.

Thanks for your Help.

--evick
7/11/2010 8:51 AM | Emilio Vick
Gravatar

# loan apply now.......

Do you need a loan Email us at danmoon009@gmail.com
9/6/2010 8:32 AM | dan moon
Gravatar

# re: System.net.mail with Gmail account

System.net.mail with Gmail account for thanx services.
9/7/2010 11:26 AM | cihip
Gravatar

# re: System.net.mail with Gmail account

Excellent work!!!!!!!!!!!!

Thanx for the code. Its working.
2/16/2011 9:53 AM | BK
Gravatar

# re: System.net.mail with Gmail account

Thanks this really worked wonders.
7/27/2011 10:25 AM | Hum
Gravatar

# re: System.net.mail with Gmail account

i understand the code...but i dont know why the sending of message failed all the time...ca you help me.:)
8/19/2011 7:47 AM | earl
Gravatar

# re: System.net.mail with Gmail account

hello,
I want to send an email message which includes HTML form elements like textbox, checkbox, radio buttons, buttons, textarea, etc..
Can you help me please. Please. suggest me for that. And please, tell me what shoul i do for that.. Please help me. and send me source code if any regarding that..
Thank you..
Thanks & Regards,
Mr. Balasaheb Mule..
11/13/2011 4:14 AM | Balasaheb
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: