Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Can we send email form local host using gmail smtp ? I am trying and getting error The operation has timed out.
I am trying to send email from local host from last 3 days. It works fine if I send emails from my hosting server using gmail but it is not working on localhost. I have disabled firewall anti virus but even then unlucky. Please guide me have u ever used gmail for sending emails from localhost (without any server involved)
If it is possible here is my code please guide me. Plese help me and guide me I am stucked.
thanks


protected void btnConfirm_Click(object sender, EventArgs e)
{
    MailMessage message = new MailMessage();
    message.To.Add("me@hotmail.com");
    message.From = new MailAddress("xxxxxx@gmail.com");
    message.Subject = "New test mail";
    message.Body = "Hello test message succeed";
    message.IsBodyHtml = true;
    message.BodyEncoding = System.Text.Encoding.ASCII;
    message.Priority = System.Net.Mail.MailPriority.High;
    SmtpClient smtp = new SmtpClient();
    smtp.EnableSsl = true;
    smtp.Port = 465;
    smtp.UseDefaultCredentials = false;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Host = "smtp.gmail.com";
    smtp.Credentials = new NetworkCredential("xxxxxx@gmail.com", "**mypassword**");
    try
    {
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
Posted
Comments
Sandeep Mewara 15-Jan-11 2:41am    
sending emails from localhost (without any server involved)
Well, localhost does not means no server, it simple means the same machine is going to act as a server too.

1 solution

I made a little changes in your code and its working fine

MailMessage message = new MailMessage();
message.To.Add("ashish_tyagi_20@yahoo.co.in");
message.From = new MailAddress("username@gmail.com");
message.Subject = "New test mail";
message.Body = "Hello test message succeed";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.ASCII;
//message.Priority = System.Net.Mail.MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
//smtp.Port = 465;   // wrong port no
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
tp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";

smtp.Credentials = new             System.Net.NetworkCredential("username", "password");
try
{
    smtp.Send(message);
}
catch (Exception ex)
{
    throw ex;
}
 
Share this answer
 
Comments
MCY 16-Jan-11 5:51am    
yes, it works. good one
Ashish Tyagi 40 16-Jan-11 7:19am    
Hello MCY, if it was good then why there is no vote............ ya you forgot to vote me
common vote it.... :)
Ragi Gopi 24-Nov-11 1:31am    
hii tried ur code, but iam getting the following error..
if u don't mind can u help me..plzz


System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.gmail.com' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at home_page.btsignin_Click(Object sender, EventArgs e) in e:\kfri_auto\automate\home_page.aspx.cs:line 146
Ashish Tyagi 40 26-Nov-11 0:27am    
It seems that your DNS configuration is not working properly.
First try to ping that domain with
COMMAND=> ping smtp.gmail.com

Output could be like :-
Pinging gmail-smtp-msa.l.google.com [74.125.53.109] with 32 bytes of data:
Request timed out.
Request timed out.

Timeout is not an issue, if you got ip [74.125.53.109] then if fine.
But if not the check DNS entry for your network interface (connection).

This exception is because domain name "smtp.gmail.com" is not get resolved to its IP, not because wrong code.

or you can try :
smtp.Host = "74.125.53.109";
instead of
//smtp.Host = "smtp.gmail.com";

Good luck. :)
Ragi Gopi 24-Nov-11 1:36am    
Ashish Tyagi...Can u plz help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900