protected void Button1_Click(object sender, EventArgs e)
{

    MailMessage message = new MailMessage();
    message.From = new MailAddress(YourEmail.Text.ToString());
    message.To.Add(new MailAddress("purohit.mith@gmail.com"));
    message.Subject = "Message via mith from " + YourName.Text.ToString();
    message.Body = Comments.Text.ToString();
    SmtpClient client = new SmtpClient();
    client.Host = "203.92.50.112";
    client.Send(message);
}

// this is what i used to send the email
// but i get the exception as "Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email]purohit.mith@gmail.com[/email]".
anybody can help me out........

try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add("xxx@gmail.com");
            mail.From = new MailAddress("xxxx@gmail.com");
            mail.Subject = "Test Email";
            string Body = "<b>Welcome to CodeDigest.Com!!</b>";
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["SMTP"];
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception exc)
        {
            Response.Write("Send failure: " + exc.ToString());
        }

try it

in config file add

<appSettings>
<add key="SMTP" value="smtp.gmail.com"/>
<add key="FROMEMAIL" value="xxxn@gmail.com"/>
<add key="FROMPWD" value="pasword"/>
</appSettings>

no it doesnt works now am getting an exception as :

send failureSystem.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- 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 _Default.Button1_Click(Object sender, EventArgs e) in d:\NIIT\ASP.NET\practice\SMTP\Default.aspx.cs:line 43

<add key="SMTP" value="smtp.gmail.com"/>

in that value tag i have written my host address. i think it is correct but still it wont work.....

hey there
i solve this problem by specifying DeliveryMethod of PickupDirectoryFromIis

#  System.Net.Mail.SmtpClient myMailClient = new System.Net.Mail.SmtpClient(AppSettings("SMTPHost"));
  
    myMailClient.DeliveryMethod =  SmtpDeliveryMethod.PickupDirectoryFromIis;
     myMailClient.Send(From,To, Subject,Body);

no it still wont work mine code is

protected void Button1_Click(object sender, EventArgs e)
{
try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add("purohit.mith@gmail.com");
            mail.From = new MailAddress("mithileshmeister@gmail.com");
            mail.Subject = "Test E-mail";
            mail.Body = Comments.Text.ToString();
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["SMTP"];
           
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception exc)
        {
            Response.Write("send failure" + exc.ToString());
        }
}

whereas in web.config file

<appSettings>
  <add key="SMTP" value="203.92.50.112"/>
  <add key="FROMEMAIL" value="my gmail account username"/>
  <add key="FROMPWD" value="my gmail account password"/>
  </appSettings>

the exception i got is as:
send failureSystem.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- 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 _Default.Button1_Click(Object sender, EventArgs e) in d:\NIIT\ASP.NET\practice\SMTP\Default.aspx.cs:line 44


tell me if am doing something wrong or has missed some steps in between

no its doesnt work. i need to send mail through SMTP somehow

I think there is issue with your SMTP because i am doing mail from it its going fine and good.

Use port 587 for gmail SMTP

I am using it and works like a charm.

use the following settings in your web.config

<system.net>
    <mailSettings>
      <smtp from="you@gmail.com">
        <network host="smtp.gmail.com" password="yourpassword" userName="you@gmail.com" port="587"/>
      </smtp>
    </mailSettings>
  </system.net>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.