Sending out an email using SMTP - c#

I am using the Send Mail Task in SSIS 2008 to send out an email.
I am using SMTP. I have SMTP set up in the IIS manager.
I have set it to use windows authentication.
The From and To mail address both exist.
When i configure the task to send an email, i get an error
"[Send Mail Task] Error: An error occurred with the following error message: "Failure sending mail.
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:25"
What IIS or SSIS config change has to be made for this to work?

Check your firewall and/or virus scanner; port 25 (the default SMTP port) is often blocked by this type of software. Not sure about the SSIS Send Mail Task specifically, but SQL Server SMTP Mail service can be configured to use a different port.
If you're in a corporate environment, it's likely that your network guys have some sort of SMTP relay you could/should use instead of the local host, and they might also have some sort of IP and/or email address whitelist they need to include you on in order to relay SMTP mail.

you need to have a valid SMTp server. The error message said you can not use local machine as SMTP server. you can try use gmail.com as a relay server.

Don't use the loopback IP address: try localhost or actual machine name etc
Edit: found a reference to say that 127.0.0.1 needs added to allowed IP addresses. Now, can't say if this applies to your case, but I'm sure I read something years ago about this

Related

Can you run a client application and a server application on the same host machine

I have a synchronous TCP server and client application the works absolutely fine on two separate host machines.
What I'd like to know is what IP and port do I bind the server socket and the client socket to when the applications are both running on the same host machine.
I can't find any solid information on Google about this.
When I try and use my network IP which was 192.168.0.32 I get an error that says the Host actively refused the connection.
I cannot find any reasonable information about this error.
Can I listen and send on the same Port?
What IP address should I use to bind the server and the client, when both applications are running on the same machine?
Thanks for your time.
In order to run both client and server applications on the same host you should bind your server socket to localhost (you can actually write "localhost" it's a preserved word or 127.0.0.1 ) and address it from the client as well.
Localhost allways refers to the computer you work on.
If you'd like to access your server from a machine which is outer to your local network using your network ip you've mentioned, you should first search for "IP FORWARDING" option in your router settings and forward incomming requests to the machine where the server is running on.
Or (my favourite) use the great IP TUNNELING service of ngrok. You can find it here https://ngrok.com/
good luck.
So the answer to this question is that I must bind to my loop back address with separate ports for the client and the server !!
The IP address could be the loopback 127.0.0.1 for both, or your IP address, I don't see why it would not work.
The port on the other hand has to be the same for it to work, assuming the client application doesn't also listens to the port that you "bind" it to.
You have to tell the server on which port it should listen. The client then has to send data on the same port for the server to get the information.
This example should get you going: https://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C

Listening for SMTP works when server and request is local but if doing the request from a remote server it fails

I am working on a project where I am making my own SMTP server. This is pretty much working but there is a bit of a problem.
When I run my SMTP server on my development machine and have a php script to send the email or telnet from my local computer to the local smtp server it works fine and my program receives the SMTP messages and I send the relevant responses.
I am then copying the program on to a server and running my smtp server and when I telnet from the server to the local smtp server i.e. telnet localhost 25 it works.
If I then try and connect to my smtp server through a remote PC on the same network and attempt to telnet to it I get Connecting to 192.168.1.74 (this is the address of the server)...could not open connection to the host, on port 25: connect failed and if I run the php script http://192.168.1.74/send-mail.php it fails.
On the smtp server code when I bind the socket I have tried binding to 127.0.0.1 and 192.168.1.74 on port 25 but it makes no difference.
Why would this not be working from a remote PC making the connection to the server but locally it works fine.
Thanks for any help you can provide.
A couple stabs in the dark here, but:
Does the server have a firewall running? If so, have you made the appropriate exceptions for port 25?
Is the remote PC really on the same network (i.e. same subnet)? If you've got a router in between you'll have to forward the port appropriately.
Found out what the problem was, I was being dumb, when I set the network card to be static I forgot to add the default gateway. Now that I have done that it is working now as expected.
Thanks everyone for your help and suggestions.

"Failure sending Mail" error on program

my friend gave me a program which runs in his laptop, he coded with system.webmail and C#, that program can send the email in his machine, but when i run and send it out in my machine, the email didn't send and get "Failure Sending Mail", but in his computer, he can send with his internet. But I am still getting error, even i can't ping to SMTP server or telnet. What will be issue, does my ISP ban the port, or ISP firewall ban the port?
I use port 25 and also 587 too. but it doesn't do any different at all.
There is an unfixed bug in System.Mail.SmtpClient where sending mail to a domain-enabled (Exchange) server fails if you cannot domain authenticate, even if username and password would authenticate, but only if you are farther than ~10ms from domain server.
I have a same issue before and it turned out that the email provider "Smart Mail" is blocking my email, The solution I simply added the email address at the safe list in my destination email.

Sending an email in C#

Is it possible to send an email in C# console, without needing and SMTP server?
Edit:
Why do I need another SMTP server? Can not I use my localhost machine as a server..?
Edit:
I just need to send an email from with my domain name, for example abc#mydomain.com
Is that possible? what do I need to do this in my C# program... I do not care about receiving emails, I just care about sending them....
Thanks
You don't have to depend on a local SMTP server if you don't have one. However, you will have to connect to a SMTP server anyway. Here is why.
You must achieve the following steps:
Determine what is the mail exchange servers of a given domain.
Connect to that mail exchange server and deliver your mail.
Those steps are normally done by your local SMTP server. Another advantage of your local SMTP server is that it will handle its queue and continue to try to deliver your email if it fail.
How to determine the MX records of a give domain.
I suggest you to have a look at this answer. Basically, you have to do a query on a DNS server to get the list of MX records of the domain name of the email address you want to send an email to.
How to connect to a mail exchange server
Well the answer will disappoint you. Exactly like you connect to your local SMTP server. Using the TcpClient, you connect to one of the mail exchange server you got at the previous step on port 25 and start the delivering process using the SMTP protocol.
The trick here is that you must handle multiple MX servers. They are usually listed with a preference. If the first one is unreachable, you try the next one and so on...
That is something your SMTP server can handle for you too.
If you really want to build that logic yourself, please have a look at the DirectSend method of the SmtpClient class of this open source project I'm involved in.
As #TomTom points out, the entire mail infrastructure depends on SMTP. What you can do is to skip the output (relaying) SMTP server and send the message directly to the receiving SMTP server.
To do that you need to create some kind of queuing mechanism (there is no guarantee that the receiving SMTP server can serve you when you try to connect) and that you can look it up.
MX records are entries stored in DNS servers and are used to find SMTP servers. You can find a article here with a MX lookup example: http://www.codeproject.com/KB/IP/dnslookupdotnet.aspx
However, I DO recommend that you install a local SMTP server and let it take care of the above mentioned issues.
Yes, Basically figure out where to sent the email and send it. i.e. a DNS MX lookup for the domain to find out the SMTP server.
Every email needs an SMTP server on the receiving side.
You can use gmail or yahoo SMTP server, if you don't want to install your own.
Before sending mail you first need to authenticate, otherwise sending mail is not going to possible.
You need access to some kind of email server to send your email, and your email will most likely pass through one or more SMTP servers on it's way to the recipient. However, the email server you connect to might let you send the email without using SMTP. For example, Exchange might let you use MAPI or CDO to send emails. Though I think that CDO is not officially supported by .Net and simple MAPI is deprecated in Windows and should not be used. You might be able to use Exchange Web Services as described here: Introducing the Exchange Web Services Managed API 1.0
If you have another email server than Microsoft Exchange, that server might have some kind of API you can use.
Something I do often is to create gmail account and send through that account.
You just need your SmtpClient to connect to the host smtp.gmail.com on port 587 with the username, password, and enableSSL property set to true.

Is this error because GMail is blocked?

I wrote a simple app to send an email via GMail SMTP servers. However, it returns an exception at runtime.
At my workplace gmail is blocked (cannot access gmail.com). Is this error because of that?
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebExceptio
n: Unable to connect to the remote server ---> System.Net.Sockets.SocketExceptio
n: A connection attempt failed because the connected party did not properly resp
ond after a period of time, or established connection failed because connected h
ost has failed to respond 74.125.113.109:587
Yeap, it looks like it is related to that. The request could be blocked by the firewall.
They are most likely blocking outbound traffic on port 587.
The answer is probably yes. There is a small possibility that the gmail server was down when you tried to access it, but it's clear that since you're blocked, you won't be able to connect.
No, it's because your network administrator has blocked this IP or port in the company's firewall.
Yes, the exception is coming up because the connection failed, but it might not necessarily be because it's blocked. I can't even connect to that host:
> telnet 74.125.113.109 587
Trying 74.125.113.109...
telnet: connect to address 74.125.113.109: Connection refused
Edit: on second look, I can connect to that host from another machine. There might be other connectivity issues afoot.

Categories