protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("fromemail", "From Me");
MailAddress toAddress = new MailAddress("toemail", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.Host = "smtp.host.com";
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
smtpClient.Port = 587;
smtpClient.Send(message);
statusLabel.Text = "Email sent.";
}
catch (Exception ex)
{
statusLabel.Text = "Coudn't send the message!\n"+ex.Message;
}
}
The error is
"Failure sending mail."
I can not send any. What is the problem?
innerexception is
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 74.125.39.109:587 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, GeneralAsy ncDelegate 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)
For an SmtpException
, you always have to examine the inner exception through the Exception.InnerException
property. Always.
Remove the try/catch and see what happens :-)
You can test locally first:
<mailSettings>
<smtp deliveryMethod='SpecifiedPickupDirectory'>
<specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />
</smtp>
</mailSettings>
More details:
From the inner exception: 74.125.39.109:587 cannot be reached.
Make sure:
One way to test, is from that same server, open a telnet connection to 74.125.39.109:587.
u have use 587 as the port....this is similar to gmail's smtp service port number....if ur using gmail smtp service...try use this...
host: smtp.gmail.com port: 587
in network ceredentials give ur valid gmail username and its password...
also check ur internet connectivity
Try taking out all the smtp server settings and place the mailSettings in your app/web.config
Sometimes mail servers will not allow mail relaying. In this case, you will need to set up a virtual smtp server.
If you are using windows server 2008, you will need to install the IIS 6 management console to configure relaying.
Default SMPT port is 25. You are using 587, could this be the problem. Or is there a reason for using port?
I remember i face similar problem the solution i find is:
before:
smtpClient.Send(message);
Put this line:
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
BTW:
the port should be: smtp.Port = 25;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.