Geeks With Blogs
Ayman Farouk Microsoft Maniac Guy ..!

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.

Posted on Thursday, March 9, 2006 7:08 PM | Back to top


Comments on this post: System.net.mail with Gmail account

# re: System.net.mail with Gmail account
Requesting Gravatar...
I think Gmail uses SMTP on port 465, did you try this code?
Left by Khalil on Mar 11, 2006 11:40 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
The above code is working fine dude.

A.
Left by Ayman Farouk on Mar 11, 2006 11:51 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
a Microsoft guy using gmail SMTP, LoooL
You are not going to use it in during your MAD demo, right? =p
Left by Bander on Apr 02, 2006 8:58 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
of course am not gonna mention it during my MAD Demo :D .. hehe
Left by Ayman Farouk on Apr 02, 2006 2:00 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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!
Left by Rami on Apr 15, 2006 6:13 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
ag areg aerg aerg aergaeh
Left by aa on May 15, 2006 10:40 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Very nice code. i tried it . it works fine. thanks dude for your code .
i rate it as 5.
Left by Rajesh on Jul 06, 2006 2:41 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Regarding above comment, the standard SSL port 465 won't work (nor did 25 for me). Port 587 is however working.
Left by Simon on Jul 24, 2006 10:41 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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
Left by May [Beginner] on Oct 16, 2006 2:59 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
I am getting errors with the above code....any idea why?
Left by Kabir on May 24, 2007 12:05 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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!!!!!!!!
Left by ameer on Dec 26, 2007 5:44 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks a lot man for saving me of many more sleepless nites.The code works grt.Thnks again.u rock....
Left by sandy_the_loner on Jan 22, 2008 3:43 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
thank you very much ayman,that helped me alot, by the way i did the c# version

rabena yekremak
Left by amr on Jan 26, 2008 7:24 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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.
Left by Henri on Jan 28, 2008 8:43 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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.
Left by Yosri Negm on Mar 18, 2008 9:04 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Faruk bey cok ama cok tesekkur ederim, uzler site dolandim ama hic biri ise yarayan bir sey yazmamisdi.


Ne mutlu turkum diyene.
Left by Ashkan on Mar 21, 2008 9:32 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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.
Left by Jack on Jun 16, 2008 7:43 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks man Ur code really Helped!!!!
Left by Harshraj on Aug 23, 2008 10:57 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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.
Left by Balachander Ganesan on Oct 29, 2008 1:12 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks. The code is working for me with port 25 and 587. port 465 is not working.
Left by Divya on Dec 15, 2008 9:48 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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
Left by Srinivasu on Jan 01, 2009 11:44 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
this code is not working for me it say's unable to connect server.
Left by dheeraj on Feb 03, 2009 5:09 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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
Left by murtaza on Mar 13, 2009 7:21 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
thank you soooo much
Left by anass on Apr 28, 2009 11:22 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Cool!
Thank you very much! It works good in Windows Forms.
Left by Kasper on May 06, 2009 12:45 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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
Left by Dolph Brown on May 16, 2009 1:18 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks Man
Left by CODER on Jul 03, 2009 2:47 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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
Left by Buvaneswaran on Sep 17, 2009 6:52 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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..........
Left by Buvaneswaran on Sep 18, 2009 9:01 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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..........
Left by Buvaneswaran on Sep 23, 2009 10:21 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
i jus wanna ask..what is network credentials..??..n y we need to put gmail username n password in it..??
Left by raman on Oct 08, 2009 7:34 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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
Left by bhuvaneswaran on Oct 10, 2009 3:48 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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!
Left by Heath on Oct 22, 2009 11:05 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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..........
Left by bhuvaneswaran on Oct 27, 2009 8:55 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks,
This code is working fine for me on the port 587.
Got ma problem slved after all............
Left by Muhammad Imran on Dec 24, 2009 2:09 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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!!
Left by Casey on Dec 27, 2009 3:59 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
This is the mail system at host smtp247.uni5.net.
Left by NAIR PANIZZON on Mar 29, 2010 7:54 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thank yo u dude. it helps me a lot.
Left by Mamun on Jun 03, 2010 8:33 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks this code works perfectly , I'm using it with Google APPS.

Thanks for your Help.

--evick
Left by Emilio Vick on Jul 11, 2010 8:51 AM

# loan apply now.......
Requesting Gravatar...
Do you need a loan Email us at danmoon009@gmail.com
Left by dan moon on Sep 06, 2010 8:32 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
System.net.mail with Gmail account for thanx services.
Left by cihip on Sep 07, 2010 11:26 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Excellent work!!!!!!!!!!!!

Thanx for the code. Its working.
Left by BK on Feb 16, 2011 9:53 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Thanks this really worked wonders.
Left by Hum on Jul 27, 2011 10:25 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
i understand the code...but i dont know why the sending of message failed all the time...ca you help me.:)
Left by earl on Aug 19, 2011 7:47 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
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..
Left by Balasaheb on Nov 13, 2011 4:14 AM

# re: System.net.mail with Gmail account
Requesting Gravatar...
Hello,
Please i need help from anyone to put me put me thru what might be the error,i want to use the gmail smtp server in ams but it giving me this error message. Connecting to: smtp.gmail.com
Connection failed: aborted by TimeOut. Attempting to Re-Connect. 2 Attempt(s) left
Connecting to: smtp.gmail.comConnecting to: smtp.gmail.com
Connection failed: aborted by TimeOut. Attempting to Re-Connect. 2 Attempt(s) left
Left by Oluwa on Jan 02, 2013 4:49 PM

# re: System.net.mail with Gmail account
Requesting Gravatar...
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class send : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox2.Text.ToString());
message.To.Add(new MailAddress("matloob.ahmad@gmail.com"));
//message.CC.Add(new MailAddress("copy@domain.com"));
message.Subject = "Message via My Site from " + TextBox3.Text.ToString();
message.Body = TextBox4.Text.ToString();
SmtpClient client = new SmtpClient();
client.Host = "127.0.0.1";
client.Send(message);
}
}


i am using this code and i am facing error.
help me out.
Left by matloob on Mar 20, 2013 7:31 AM

Your comment:
 (will show your gravatar)


Copyright © Ayman Farouk | Powered by: GeeksWithBlogs.net