sending mail works on server but not on my local machine - c#

i have the following code below (i have stuck in xxx to not publish the real name). this code works fine when i upload to the server and will send out emails perfectly.
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmailAddress_);
msg.To.Add(new MailAddress(toEmailAddress_));
msg.Subject = subject_;
msg.Body = body_;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("xxx#xxxx.org", "password");
try
{
SmtpClient c = new SmtpClient("mail.xxx.org");
c.UseDefaultCredentials = false;
c.Credentials = basicAuthenticationInfo;
c.Send(msg);
}
catch (Exception ex)
{
throw ex;
Console.Write(ex.Message.ToString());
}
but when i test locally on my machine in visual studio, i get an error:
[SocketException (0x274d): No connection could be made because the target machine actively refused it 66.226.21.251:25]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +239
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +35
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +224
[WebException: Unable to connect to the remote server]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +5420699
System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +202
System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21
System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +332
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +160
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +159
any suggestions of why this wouldn't work

The first thing I'd do to run this down is to see whether the target machine is, as the error message says, actually refusing the connections. I can connect to port 25 on that IP address from here, so it's possible that your error message is misleading. These are all possible causes of a failure to connect:
there's some problem in your code
there's some problem in the library you're using
you have a local firewall preventing outbound mail from strange programs
your ISP does not permit outbound SMTP connections except to their own servers
some intermediate network is blocking things
the mail server's ISP is blocking SMTP connections from your network
the mail server really is rejecting you, perhaps because you're on a blocklist
To get more detail, download and install Wireshark. It's like a debugger for networks. Start a capture between your system and the target network and then run your code. Interpreting the captured packets takes some experience, so if you get stuck feel free to post more data here.

Relay restrictions are most likely preventing you from sending mail from outside an allowed set of IPs. If the SMTP Server is configured on a web/application server, it's probably set up to allow mail to be sent via the 127.0.0.1 only.
You can configure a local SMTP Server via the IIS Manager. If you go down this path, make sure port 25 is opened up on your local machine. Often times anti-virus type software block/monitor this port and it mucks with email delivery.

I think you are not using the port no in your code. Use these lines, Then mail should be gone on local also.
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Timeout = 60000;
smtp.Port = 25;
This is working on my system. I hope it will work also on your local system.

The provider has probably firewalled off their mail server so that it is only accepting mail from their hosting environment.
Change it to use your local mail server when you're testing it locally.

You asked for an XP Mail Server. Try hMailServer

Have a look to see if your antivirus is going crazy, i've have seen this problem a few times with people running mcafee.
Hope this helps.

Related

Failure Sending Mail in C# SMTP server

I am writing web services for an application in which there is a registration function and upon registration i have to send an email through the server. i have already done this kind of work for another website with Parallel Plesk server. using the same code i'm trying to send email but getting this exception. I searched Google and some related answers here but all in vain. Help?
"System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'domainname.com'\r\n at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)\r\n at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)\r\n at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)\r\n at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)\r\n at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)\r\n at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)\r\n at System.Net.Mail.SmtpClient.GetConnection()\r\n at System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n --- End of inner exception stack trace ---\r\n at System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n at ActivityTracker.Controllers.ValuesController.parentRegistration(parent p) in c:\Users\Harris\Documents\Visual Studio 2012\Projects\ActivityTracker\ActivityTracker\Controllers\ValuesController.cs:line 89"
From the machine running your code:
You CANNOT resolve domainname.com
You CAN resolve to the IP address of domainname.com
This indicates that you are missing an internal DNS entry.
As your machine can talk to the IP but not using the DNS name - you can work around the issue by creating a Hostname entry for the domain name in question.
Here's how to modify your Hostname file:
http://www.rackspace.com/knowledge_center/article/modify-your-hosts-file
Once this entry is in place, you should now be able to ping to domainname.com. When that works - rerun your code.
Some useful DNS reading:
http://www.techrepublic.com/article/understanding-how-dns-works-part-1/
http://social.dnsmadeeasy.com/blog/understanding-dns-forwarding/
NOTE: You've not confirmed if you can telnet to the port in question, so I am presuming this is ok - ruling out any firewall issues.
Try to use GMail SMTP if it works with GMail SMTP then the problem exist with the domain you specified.

Cannot initialize AndroidDriver

At W2k8 64 bit I have in the android emulator instance of android 4.1 API Level 16 with ARM as CPU.
After launch of it I'm running my UnitTest and on the first line
var webDriver = new AndroidDriver("http://localhost:8080/wd/hub"); //also with no argument
I got an exception:
Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:8080
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.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
Version of this WebDriver is 2.25.1.0.
I followed this article:
http://www.nishantverma.com/2011/06/installing-webdriver-on-android.html
When I'm using other drivers like Firefox- or ChromeDriver works fine.
I would be gratefull for any advice
I might be telling you what you already know, but have you started the tcp port forwarding to android server you are running. for reference check Android Driver
Your server is not starting for some reason. Is it possible you already have something else running on port 8080?
Try running netstat -a -n and see if port 8080 is listed.
If it is in use, you might try setting up on a different port or killing the process which is using that port.
You might also try disabling UAC and Firewall on your machine and/or running the process as Administrator and see if that helps (This would be temporary of course, don't leave these off).

SMTP Email Sending

No connection could be made because the target machine actively refused it 127.0.0.1:25
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:
System.Net.Sockets.SocketException: No
connection could be made because the
target machine actively refused it
127.0.0.1:25
Source Error:
An unhandled exception was
generated during the execution of the
current web request. Information
regarding the origin and location of
the exception can be identified using
the exception stack trace below.
Stack Trace:
[SocketException (0x274d): No
connection could be made because the
target machine actively refused it
127.0.0.1:25] System.Net.Sockets.Socket.DoConnect(EndPoint
endPointSnapshot, SocketAddress
socketAddress) +239
System.Net.Sockets.Socket.InternalConnect(EndPoint
remoteEP) +35
System.Net.ServicePoint.ConnectSocketInternal(Boolean
connectFailure, Socket s4, Socket s6,
Socket& socket, IPAddress& address,
ConnectSocketState state, IAsyncResult
asyncResult, Int32 timeout, Exception&
exception) +224
[WebException: Unable to connect to
the remote server]
System.Net.ServicePoint.GetConnection(PooledStream
PooledStream, Object owner, Boolean
async, IPAddress& address, Socket&
abortSocket, Socket& abortSocket6,
Int32 timeout) +5483819
System.Net.PooledStream.Activate(Object
owningObject, Boolean async, Int32
timeout, GeneralAsyncDelegate
asyncCallback) +202
System.Net.PooledStream.Activate(Object
owningObject, GeneralAsyncDelegate
asyncCallback) +21
System.Net.ConnectionPool.GetConnection(Object
owningObject, GeneralAsyncDelegate
asyncCallback, Int32 creationTimeout)
+332 System.Net.Mail.SmtpConnection.GetConnection(String
host, Int32 port) +160
System.Net.Mail.SmtpTransport.GetConnection(String
host, Int32 port) +159
System.Net.Mail.SmtpClient.GetConnection()
+35 System.Net.Mail.SmtpClient.Send(MailMessage
message) +1213
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage
message) +1531
Checkout.btnSend_Click(Object sender,
EventArgs e) +2675
System.Web.UI.WebControls.Button.OnClick(EventArgs
e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +110
I can't send email in live connection but in my local its working.
please help me guys.
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
+13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +1565
Version Information: Microsoft .NET Framework Version:2.0.50727.3615; ASP.NET Version:2.0.50727.3618
here's my C# code..
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Host = "localhost";
//delete session cookie
Response.Cookies["session"].Expires = DateTime.Now.AddYears(-30);
SmtpMail.Send(objEmail);
You forgot to start your SMTP server on that machine
Please don't tell you don't have a SMTP server on that machine. But, if you have to tell that, refer to this page and this page also
Even if you configure the local SMTP server, your emails are most likely to end up in spam folder. Better use the SMTP server of your email provider. If you are using Gmail, have a look at here
I got the error with smtp services installed but not started. To start services on the local machine with smtp already installed, here's the steps I took:
Open Server Manager
Select Local Server
Type "smtp" in the filter box under SERVICES (see image)
Right click on Simple Mail Transfer Protocol Service (SMTP) and
select Start Services

.Net Web Request - Unable to connect to the remote server

I'm using .Net Web Request to read the html of selected Webpages.
Once in a while I'm getting the following error:
2010-09-05 13:14:51,986 [File_29] ERROR Boxer.Classes.GetLinks [(null)] - HtmlUtils - GetResponse - Url - http://domain.com/index.html
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 69.212.110.118:5068
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
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.HttpWebRequest.GetResponse()
at Boxer.Classes.GetLinks.GetResponse(String url, Int32 timeout, Boolean& timeoutException)
I know for sure that the target URL is available.
Any idea what is the cause of the error?
There could be different reasons for this happening. Maybe the target server is throttling the response because it believes the request comes from a bot. In this case you might consider sending proper HTTP headers (if it works in your browser, try sending the same request headers). Also there could be a proxy in between that you need to configure and use.

Webservice SocketException - Only Breaks On One Application

I have a few applications which use a single Web Service which resides on the same server as the applications.
Why would only one of the applications have a problem connecting to a web service? And is there a way I can better diagnose exactly what the problem is with the connection?
It won't even connect to a web service the application itself is hosting
I have tried running it from my local machine and it works fine (updated the web ref url and the connection string to point to the liver server), which should rule out that the code itself is the problem.
I have tried setting up a different virtual directory to point to the application; and later copying the entrie app to another location on the same server and setting it up there. No luck.
[SocketException (0x274c): 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 xx.xx.xx.xx:80]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +239
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +35
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +224
[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.GetRequestStream() +5321194
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +103
...
Found it... the proxy was the cause of the issue and it is the only application where I specify proxy different from the default:
Since I only need the proxy locally I disabled it on the live web.config with enabled="false"
<defaultProxy enabled="false">
<proxy
autoDetect="False"
bypassonlocal="True"
scriptLocation="http://www.proxy.something"
proxyaddress="http://proxy.com" />
</defaultProxy>

Categories

Resources