0
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........

Edited by Nick Evan: Fixed formatting

5
Contributors
8
Replies
10
Views
7 Years
Discussion Span
Last Post by virang_21
0
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>

Edited by kdcorp87: n/a

0

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.....

Edited by mith_cool: n/a

0

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);
0

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

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.