I have a Contact Us page in my website from which I am trying to send mail.Here is my code
MailMessage feedBack = new MailMessage();
feedBack.To.Add("some#some.com");
feedBack.From = new MailAddress("MyClient#some.com");
feedBack.Subject = "Mail from My Client's Website.";
feedBack.Body = "Sender Name: " + Name.Text + "<br/><br/>Sender Last Name:"+LastName.Text+"<br/><br/>Sender Company:"+Company.Text+"<br/><br/>Sender Designation:"+Designation.Text+"<br/><br/>Sender Email:"+Email.Text+"<br/><br/>Sender Phone No:"+ PhoneNo.Text+"<br/><br/>Sender Enquiry:"+Enquiry.Text;
feedBack.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.MyClient.com"; //Or Your SMTP Server Address
//smtp.Port = 25;
//smtp.EnableSsl =false;
smtp.Credentials = new System.Net.NetworkCredential("MyClient#some.com","XXXX");
//Or your Smtp Email ID and Password
smtp.Send(feedBack);
All the time I keep getting this error
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 64.37.118.141:25
I have verified with my client and all the details are correct(smtp server name,credentials).
I also tried setting port to 587 and 465 but it did not work.
Can someone help me out with this?
What can be the cause?
I am not able to find it.
Any suggestions are welcome.
I tested the ip above with port 25 through telnet and got the following response:
Command:
telnet 64.37.118.141 25
Response:
Could not open connection to the host, on port 25: Connect failed
Most likely port 25 is being blocked. You will need to find what port is used with your pop/smtp servers.
MEDIUM TRUST
If you are on a shared hosting service, chances are that ASP.Net is set to run in medium-trust (as it should). This restricts SMTP to port 25 only, you cannot use any other port.
SMTP problems when ASP.NET is not running in full-trust
SO similar post
Have you checked that the server that the web application resides on is set up to be allowed to send mail through the SMTP server? Trust connections sometimes have to be setup to allow relaying off one server throught another.
Related
I am given a task to create a new smtp mail server which can receive mail using C#.
While going through the articles i read we can send emails via SMTP but we have to receive or read using POP.
I was directed to links by some stackoverflow already existing questions:
Rnwood and sourceforge
Rnwood I am sorry but i did not understand how to use it.
source forge the msi asked to download if we run it, it asks to download framework 1.1.4322 which will not install in my system and throw error.
Usually there are codes for sending messages so I tried msdn example
I used localhost as the server and 587 as the port.
which gives me error (for any port 587,25)
I also found an article here which actually monitors the localhost and specified port when I try to run the msdn code.
But still I am unable to send email to test in any way.
So is there any way I can code to set up smtp in my own server and receive email and test.
Setting up and configuring a mail server is a completely different ball game than just sending or reading emails from an existing IMAP / POP3 server.
A mail server consists of a number of components such as:
A Mail Transfer Agent (MTA) that handles SMTP traffic and which is responsible for sending email from your users to an external MTA and to receive email from an external MTA.
Mail Delivery Agent which retrieves mail from the MTA and places it in the recipient's mailbox.
A domain name with appropriate DNS records and an SSL certificate.
A server that provides IMAP / POP3 functionality.
In short... stick to publicly available mail servers...
In your post you referenced the SmtpClient from the .NET framework. That library is used to connect to an existing mail server. You can use it like this.
MailMessage message = new MailMessage();
message.From = new MailAddress("your.email.address#example.com", "Your name");
MailAddress recipientsMailAddress = new MailAddress("the.recipients.email#example.com");
message.To.Add(recipientsMailAddress);
message.Subject = "The subject of your email";
message.Body = "The body / content of your email";
message.IsBodyHtml = false; // You can set this to true if the body of your email contains HTML
SmtpClient smtpClient = new SmtpClient
{
Credentials = new NetworkCredential("Your username/email", "Your password"),
EnableSsl = true, // Will be required by most mail servers
Host = "The host name of the mail server", //
Port = 465 // The port number of the mail server
};
smtpClient.Send(message);
If you have a Gmail account, you can use their SMTP server in your C# application, simply use these settings and it should all work.
Hostname: smtp.gmail.com
Port: 587
Username: your_email#gmail.com
Password: ********
RequireSSL: true
Have a look at SmtpListener, I think it does what you want.
It isn't a standard email server which will receive new emails throught SMTP, store them on disk and allow you to retrieve them using POP.
SmtpListener will create a SMTP server that will receive email and allow you to react to any new email through code.
However, please note that you will have to configure it in your production environment like a real SMTP server, including MX DNS entries.
I tried to Send email from a Desktop app using SMTP sever but my network is secure and port is closed.
So, is there another way to send email like using Gmail api ?!
I use this code but doesn't work with me
public void Send_Mail(string HTMLBody, string MailTo)
{
MailMessage Mail = new MailMessage();
SmtpClient SmtpClient = new SmtpClient();
string MailSubject = "Subject;
string MailFrom = "from#xxxx.com";
Mail.Subject = MailSubject;
Mail.Body = HTMLBody;
Mail.To.Add(MailTo);
MailAddress From = new MailAddress(MailFrom);
Mail.From = From;
Mail.IsBodyHtml = true;
SmtpClient.Host = "host";
SmtpClient.Port = port;
SmtpClient.EnableSsl = true;
SmtpClient.Send(Mail);
}
If your network doesn't allow outbound connections to whatever port gmail uses (or restricts a particular protocol, or IP, etc), then there's nothing you can do. You would have to talk to the "network guys" to either remove this restriction for you or better yet, ask them to provide the local smtp server for you to use.
I've worked in a place where we had a similar problem. Desktop machines were not allowed to send emails, but servers could be permissioned to talk to an SMTP server.
What we ended up doing was writing a windows service that listened for messages placed on a queue (Tibco EMS in our case, but MSMQ would also do). The service took the messages from the queue and passed them onto the SMTP server is was permissioned to use.
It added an extra step, and process, to the system, but was enough to satisfy the compliance department.
Normally a "secure network" means that there is a firewall in place that restricts the traffic and only allows communication on certain ports like port 80 and maybe 8080.
Such networks (workplaces, shared office spaces, schools, eg.) usually have an outgoing SMTP server you could use. Alternatively you will need to use a server that can be contacted through the port(s) that are actually open or relay/tunneling the request through a third party.
I was trying to impliment an email option in my program..
The follwing exception was thrown
+$exception {"The server rejected one or more recipient addresses. The server response
was: 550 5.7.1 <"mail-id">... Relaying denied. IP name lookup failed [172.25.9.23]\r\n"}
System.Exception {System.Web.HttpException}
This is what i have done......
MailMessage objEmail = new MailMessage();
objEmail.To = txtTo.Text;
objEmail.From = txtFrom.Text;
objEmail.Cc = txtCC.Text;
objEmail.Subject = txtSubject.Text;
objEmail.Body = txtBody.Text; ;
objEmail.Priority = MailPriority.High;
WebMsgBox.Show("test");
SmtpMail.SmtpServer = "someserver.com";
MailAttachment attach = new MailAttachment(#"D:\email.txt");
objEmail.Attachments.Add(attach);
try
{
SmtpMail.Send(objEmail);
WebMsgBox.Show("Your Email has been sent sucessfully - Thank you");
}
catch (Exception exc)
{
WebMsgBox.Show("Send failure: " + exc.ToString());
}
Perhaps check that the server you are using to send with (smtp) has the sender machine IP whitelisted. I don't actually know how this is done but your infrastructure folks could help. Seems as though the smtp server has been secured somewhat.
When we try to send mail, there are lots of configuration settings have to be ok including some background things. (mailserver, antivirus, firewalls,routers, anti spyware etc…). Have you checked your SMTP server (services) is enabled or not ?
I am not sure 100% that below steps solve your problem, but you can check your SMTP server configurations by it :
In the Control Panel >> Administrative Tools >> Internet Information Services.
Open the node for your computer, right-click the Default SMTP Virtual Server node and choose Properties.
In the Access tab, click Connection.
Select Only the list below, click Add and add the IP 127.0.0.1. This restricts connections to just the local computer, i.e., localhost. Close the Connection dialog box.
Click Relay and repeat Step 4 to allow only localhost to relay through this server.
For IIS7, you can do it by :
Open IIS and select the node for your computer, double click on "SMTP Email" option at right side panel.
You can set SMTP server and port details there.
In your firewall or router (or both), close incoming port 25. This is an important security measure that will prevent spammers from finding your SMTP server and using it to relay spam.
Hope this will solve your problem.
I'm trying to send email with C#. Our email provider suggested that I use mail.example.com/exchange instead of mail.example.com
string mailServer;
mailServer = "mail.example.com";
mailServer = "mail.example.com/exchange";
SmtpClient smtpClient = new SmtpClient(mailServer);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Send("from#example.com", "to#foo.com", "subj", "email body");
When mailServer does not include a directory, after a long pause, I get:
System.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
When mailServer does include a directory, with no pause at all, I get:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'mail.example.com/exchange'
How can I send an email with C# for a FQDN that includes a directory?
SMTP doesn't support the concept of directories, so you might want to get an email provider that knows what they're talking about.
That seems more like a webmail address than an SMTP address. DNS isn't going to know what to do with that path. Verify that they are talking about SMTP connections (very likely they have an alternate port they use)
I installed hmailserver 5.3.2 and configured it. It receives and sends
emails normally, but I wanted to use it to send emails from a .net/C#
application located in another server, and for that I wanted to use
SSL communication. Before, the application was configured to send
emails via gmail, on port 587 and it worked ok, but now we want to use
our own mail server. We first configured the application to connect on
smtp.domain.com on port 25 and that works, it sends the email.
Then we
created a self signed certificate to test the if we could send the
message through a secure channel.I created the certificate with
openSSL setting common name as: mail.domain.com, smtp.domain.com,
*.domain.com, domain.com. I opened port 587 on the firewall and
configured hmailserver to use a certificate for inbound connections on
that port.
None of the certificates I created worked (I tried one and then
created another one and so on), generating the following (generic) exception in the
application:
System.Exception: _COMPlusExceptionCode = -532459699
Of course I also tried to connect via telnet: telnet smtp.domain.com
587, and I just got a blank screen. It is not a firewall issue since
when I disable the ssl on port 587 I can connect normally.
Looking at the log doesn't even show an attempt to connect when using
587 with SSL.
I already checked these questions: Getting SmtpClient to work with a self signed SSL certificate and Using a self-signed certificate with .NET’s HttpWebRequest/Response, but it didn't solve my problem. The approach with ServerCertificateValidationCallback didn't have any influence.
I tried with ports 25 (which is also proposed in one of the questions above), 465, 587, and with all 3 it happens the same: The initial handshake (SYN / SYN-ACK / ACK) and after about 80s the connection is closed (FIN), nothing in between.
Do I have to install that certificate somewhere so the .net application sees it as trusted? I mean, I already installed it as a Trusted Root Certification Authority and could check by running mmc, so I have no idea where to go now...
Thanks for the help!
PS: Not sure if this belongs to ServerFault since it concerns a C# application but also a mail server...
EDIT: Code sample:
ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;
SmtpClient mailClient = new SmtpClient("smtp.domain.com");
mailClient.Credentials = new NetworkCredential("username#domain.com", "pwd");
mailClient.Port = 587;
mailClient.EnableSsl = true;
MailMessage mailMessage = new MailMessage("mailAddressFrom", "mailAddressTo", "subject", "body");
mailMessage.IsBodyHtml = true;
mailClient.Send(mailMessage);
EDIT 2: Log (Based on Ramunas' suggestion):
"TCPIP" 3588 "2010-06-23 10:02:49.685" "TCPConnection - Posting AcceptEx on 0.0.0.0:465"
"DEBUG" 3588 "2010-06-23 10:02:49.809" "Creating session 24039"
"TCPIP" 772 "2010-06-23 10:04:29.639" "TCPConnection - SSL handshake with client failed. Error code: 2, Message: End of file, Remote IP: X"
"DEBUG" 772 "2010-06-23 10:04:29.639" "Ending session 24039"
currently, you can not send mail using c# 4/.NET 4 to a hMailServer regardless whether the certificate used by hMailServer is purchased or self-signed.
the problem is two part AFAIK ... c# 4/.NET 4 will only send using TLS and port 587; hMailServer does not currently support STARTTLS. c# 4/.NET 4 does not support the alternative of 465/SSL.
see this thread "configuring SSL confusion ..." at hMailServer's forum.
"SmtpClient.EnableSsl Property ":
"An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported." -- MSDN
As gerryLowry said:
c# 4/.NET 4 will only send using TLS and port 587;
hMailServer does not currently support STARTTLS
You can update your hMailServer to hMailServer 5.5.1 (BETA) here
It now supports STARTTLS and with port 587 all is working correctly.
This is a sophisticated mechanism but in simple words client (computer you're making connection from) should know about WHO is certificate issuer (in your case your server is certificate issuer). If it does not find it in it's Trusted Root Certificate Authorities list then it considers this connection to be unsafe. (I bet you've seen browser warning you about unsafe request to some https://.... site).
Open Certificates snap-in in your Microsoft management console on a client computer and try to add the same self signed certificate to a Trusted Root Certificate Authorities list.
I installed hMailServer, created self signed certificate, added it to hMailServer and was not able to send mail via it, too. Though I was successful while sending emails without certificate.
I enabled logging on hMailServer (for everything) and tried again with no luck. But I saw an error in a log file stating
"Severity: 2 (High), Code: HM5113,
Source: TCPServer::Run(), Description:
Failed to load certificate file. Path:
<...>test.cer,
Address: 0.0.0.0, Port: 25, Error: An
invalid argument was supplied"
Maybe this is a case on your hMailServer also?
I have port 25 as normal SMTP open on my hMailServer as well as port 465 for SSL, so I had to change my code to point to the normal SMTP configuration. It should work after that. As for SSL, sorry, it won't work on hMailServer...
MailMessage message = new MailMessage();
message.From = new MailAddress("me#myself.home", "Me");
message.Body = "hello, World!";
message.To.Add(new MailAddress("you#myself.home", "You"));
SmtpClient client = new SmtpClient("secure.myself.home", 25);
client.EnableSsl = false;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("me#myself.home", "pwd");
client.Send(message);