I have an SmtpClient that works fine when I run it localhost. However, on google cloud server it returns:
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 xx.xx.xx.xx:587
My code:
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("User email");
mailMessage.From = new MailAddress("My Company Email");
mailMessage.Subject = "Test";
mailMessage.Body = "MSG Test";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.mycompany.com"; //Or Your SMTP Server Address
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("email#mycompany.com", "password");
smtp.Send(mailMessage);
Does anyone know why? Should I use Azure instead of google cloud for .net applications?
Thanks
I suspect the problem is that traffic on port 587 is blocked by default on Compute Engine, except for known IP addresses.
From the firewall documentation:
Compute Engine blocks or restricts traffic through all of the following ports/protocols between the Internet and virtual machines, and between two virtual machines when traffic is addressed to their external IP addresses through these ports (this also includes load-balanced addresses). These ports are permanently blocked; they cannot be opened using firewall rules.
...
Most outgoing traffic to port 465 or 587 (SMTP over SSL) is blocked, except for known Google IP addresses.
Also, from the "Sending Email from an Instance" documentation:
Google Compute Engine does not allow outbound connections on ports 25, 465, and 587. By default, these outbound SMTP ports are blocked because of the large amount of abuse these ports are susceptible to. In addition, having a trusted third-party provider such as SendGrid, Mailgun, or Mailjet relieves Compute Engine and you from maintaining IP reputation with your receivers.
This latter documentation page provides a number of alternative approaches.
Related
I am trying to send mail through SMTP but i am getting the error unable to connect remote server, I have tried alot but still getting same error
I am hosted my site on Go Daddy,
please any body suggest me where i am wrong , or is there any settings on GoDaddy server to send mail from "relay-hosting.secureserver.net"
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient("
relay-hosting.secureserver.net", 25);
Client.Credentials = CredentialCache.DefaultNetworkCredentials;
Client.DeliveryMethod = SmtpDeliveryMethod.Network;
Client.Send(message);
You are trying to send mail using remote SMTP server, but using credentials from your local environment.
Try to use another credentials class type to specify it more concrete:
Client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
Referring to your code:
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient("
relay-hosting.secureserver.net", 25);
You're using port 25, which is actually the problem. System Administrators who utilize GoDaddy as their hosting provider may experience trouble sending mail over port 25. This is because GoDaddy requires their servers to send through their own SMTP Relay instead of any third party. This is really a frustration situation, but this is what Godaddy policy is. If you use Godaddy relay, then it will work.
I have a program that I want to run on a server which is able to monitor various servers on a network and send out email notifications when certain situations involving those servers occur.
Currently, I am able to run this program on my local machine, and have it fire off emails (using gmail SMTP). However, when I try to run it on the server via the command prompt, it throws the following exception:
Unhandled Exception: 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
xxx.xxx.xxx.xxx:587
The code related to the sending of the email is fairly simple:
static void sendEmail(MailAddressCollection mailCollection, string emailSender,
string emailDisplayName, string emailPassword,
string subject, string body) {
MailAddress fromAddress = new MailAddress(emailSender, emailDisplayName);
SmtpClient smtpClient = new SmtpClient {
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, emailPassword)
};
MailMessage message = new MailMessage {
From = fromAddress,
Subject = subject,
Body = body
};
foreach (MailAddress mailAddress in mailCollection) {
message.To.Add(mailAddress);
}
smtpClient.Send(message);
}
The server in question is a 2008R2 box, and it does not have Windows Firewall enabled. Also, port 25 is definitely open, as this server can send other types of emails (specifically, those generated by a Dynamics CRM email router).
What could be causing this error?
The issue was related to Gmail blocking the IP address of the server attempting to send email.
When attempting to send emails using a Gmail account, first attempt logging into Gmail manually on that machine specific at that specific IP address, using a browser. I ultimately had to do this and go through the process of authenticating that the email send attempts from my server (being generated programmatically) were legitimate. If you do not go through this process like I did, Gmail can assume that your server is not a legitimate email sender and can continue to assume all traffic from your IP address is invalid--until you formally validate that IP address as described above.
I want to send mail automatically by special email account, but now, I only know the email address: tsp.monitor#qorosauto.com , and the password. so do you know how to get the SMTP server. below is my C# code:
SmtpClient client = new SmtpClient();
client.Host = "What is the SMTP Server, I want to get from email address, can you help me";
string account = "tsp.monitor#qorosauto.com";
string password = "Qoros111";
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(account, password);
You find the SMTP server of a domain by taking the host part of the email address (qorosauto.com in your example) and looking up the MX record for it.
$ dig +short mx qorosauto.com
10 euq2.qorosauto.com.
5 euq1.qorosauto.com.
The number before the hostname indicate preference - in this case euq1.qorosauto.com is the preferred server to connect to.
Doing this in .Net is not straight-forward, as the answer to this question indicates: How to get mx records for a dns name with System.Net.DNS?
To add to the problems, many ISPs will filter your connection in the firewall and won't let you talk to any SMTP server except the ISPs one, which in turn will relay the mail to the recipient.
Essentially, you want to use your ISPs or organizations SMTP server - not the recipients MX.
Trial and error might get you there..
mail.quorosauto.com
smtp.quorosauto.com
www.quorosauto.com
smtp.gmail.com
If you've exausted every possibility you'll need to provide your own SMTP server. There are a few freebie servers:
http://freesmtpservers.com/
However if you are doing this professionally you probably want to use the SMTP server of your organization so you probably want to ask your boss or a colleague.
You may try mail.orosauto.com or smtp.orosauto.com. Login to you domain account and check mail settings. hopefully you can find the mail server details over there.
To find out the responsible mail server you have to ask the DNS. By using the web for example go to this nslookup page.
Normally you start at the domain server of your provider, but you can also start with a root-server (e.g. 198.41.0.4) and ask for the domain google.com and the query type MX - Mail exchange.
You'll get back a list of responsible dns server. Simply pick one out of the list (e.g. 192.26.92.30) and send the same query again to this server. Rerun this sequence till you'll get a list of type MX. Simply pick one address out of this list (maybe the first, maybe the one with the lost value in preference) and use this to establish your smtp connection.
That's the way how every mail server does its work. Now it is up to you to implement that into your application. ;-)
I have following piece of code, usiong which I am trying to send an email. I am using another server as SMPTClient.
MailMessage message = new MailMessage();
message.To.Add(toEmailId);
message.Subject = "test Subject";
message.From = new System.Net.Mail.MailAddress("myid#xyz.com");
message.Body = "This is a system generated email. Please do not reply";
SmtpClient smtp = new SmtpClient("anotherservername");
smtp.Send(message);
While debuggin, I get error at last line. The error is:
An attempt was made to access a socket in a way forbidden by its access permissions SERVER IP :25
I found out from internet that My firewall might be blocking me to access that server's port, or is there any setting, configuration which I have missed. Apparently the applications deployed on that server are able to send emails. I am still building one to test.
Some servers perform authentication and authorization by checking if you've connected to POP server some time before connecting to SMTP. You should try first connecting and authenticating with POP and afterwards connecting to SMTP.
Historically, email clients first checked and then sent emails in Send&Receive routines, so this was used as poor mans authorization. Take a look at wikipedia article about POP before SMTP.
Another issue which caused this for some people is how server address is provided to SmtpClient. You should use constructor which explicitly provides port number instead of appending it to IP/hostname. I.e.
instead of new SmtpClient("123.123.123.123:25")
use new SmtpClient("123.123.123.123", 25)
I am trying to send an smtp email through gmail's smtp server using the code below:
MailMessage message = new MailMessage("myEmail#gmail.com", "myEmail#purdue.edu", "Testing SMTP", "Test, yo");
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("myEmail#gmail.com", "myPassword");
client.Send(message);
Using the same code my friend successfully sent me an email from another network, but .NET throws the error "No connection could be made because the target machine actively refused it 74.125.91.109:587". This has to be a network issue right?
My network admin claims there are no blocked outbound ports and my firewall is entirely off, what else could be causing this? I have tried Purdue's smtp server as well (smtp.purdue.edu), and it fails with the same message.
Turn off any antivirus program.
Regarding the test to Purdue, are you sure that they use port 587?
You should use port 465 for SSL according to this
Change the port to 465. It worked for me.
This may caused by McAfee Virus Blocks the Mass Mailing Worms. you can do the following:
1.Open the VirusScan Console by right clicking the VirusScan shield in the system tray and select VirusScan Console...
2.Double click the Access Protection item and open it.
3.Uncheck the Prevent Mass Mailing Worms from Sending Mail rule.
4.Click OK and close the VirusScan Console.
Hope this is helpless.