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.
Related
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.
I am getting the following error:
Failure sending mail
and inner exception is
Unable to connect to the remote server
in C#
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com","Newsletter", "This is a test mail");
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
This is my local smtp email setting in IIS 7.
Also installed the smtp server tools
Try setting the "Email Address" field in the IIS SMTP settings to the FROM address you are using.
SOURCE
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 wrote this code in asp.net for sending email:
MailMessage mailObject = new MailMessage("a#b.com(I use fake email, exactly this email)", "info#mydomain.com", "contact us", "Test message");
SmtpClient smtpC = new SmtpClient("my smtp server name");
smtpC.Send(mailObject);
But I receive this exception:
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
Also I can not connect to my "smpt server name" via tel net:
telnet SMTPServerName 25
Connecting To SMTPServerName...Could not open connection to the host, on port
25: Connect failed
Is there any standard for troubleshooting this?
Most likely the server name or port is incorrect. Make sure you can send from an SMTP client before troubleshooting further in your code.
i am creating an email client that sends e-mail address from server ip instead of SMTP , i wrote that code:
SmtpClient server = new SmtpClient("50.23.128.66");
MailMessage msg = new MailMessage("from#yahoo.com", "tome#yahoo.com", "subject", "body");
server.Send(msg);
but when i run it , i get that error:
Unhandled Exception: System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Relaying Denied.
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressColl
ection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
* By the way i am using Windows Server 2008 *
and i configured the smtp server to localhost and port 25 , but i don`t know what is that relying error .
If you are using you localhost to rely email, you should be using 127.0.0.1 or localhost instead of that ip address you posted in your code.
Relaying is the method the SMTP server uses to authenticate that it should route the email from a particular sender. An "open relay" means that there is no authentication and the SMTP server will send email sent from anyone. This is not a good practice and there are probably some mechanisms on this particular SMTP server to authenticates, such as a user name and password or even the IP address of the sender. Check with the administrator of the SMTP server to see what is required. I would think at a minimum you would need to set the user name and password, which you did not do in your code example.