Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C# C#3.5
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 14-Jan-11 20:09pm
M Ghoom385
Comments
Sandeep Mewara at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
            }
  Permalink  
Comments
MCY at 16-Jan-11 5:51am
   
yes, it works. good one
Ashish Tyagi 40 at 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.... :)
Ragigopi at 24-Nov-11 1:31am
   
hi
i 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 at 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. :)
Ragigopi at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 504
1 Maciej Los 349
2 Kornfeld Eliyahu Peter 325
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 15 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100