Send email via asp.net throw exception - c#

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.

Related

MailKit C# SmtpClient.Connect() to Office 365 generating exception: "An existing connection was forcibly closed by the remote host"

I have a problem sending email via Office 365 SMTP and MailKit. The exception I get is:
Unhandled Exception: System.IO.IOException: Unable to read data from
the transport connection: An existing connection was forcibly closed
by the remote host.
System.Net.Sockets.SocketException: An existing connection was
forcibly closed by the remote host
https://github.com/jstedfast/MailKit
Code:
var smtpClient = new SmtpClient();
smtpClient.Connect("smtp.office365.com", 587, true);
Microsoft Office 365 settings should be correct:
https://support.office.com/en-us/article/POP-and-IMAP-settings-for-Outlook-Office-365-for-business-7fc677eb-2491-4cbc-8153-8e7113525f6c
The weird thing is that if I use the following everything works, even though Office 365 says SSL is required.
smtpClient.Connect("smtp.office365.com", 587, false);
Got another error after posting this question which led me to the answer:
Handshake failed due to an unexpected packet format
The solution is to connect to Office 365 like this instead:
smtpClient.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

Syntax error, command unrecognized. The server response was ''

I've looked at 3 or 4 questions here so far and they haven't helped yet.
I have set up SmarterMail 14.x on a client's machine. I know very little about setting up mail servers (I don't know why he thinks I do) but I have it installed, a domain set up pointing to the MX record, the port open (9998) and I am able to send email just fine from the web interface.
However, I can't send email from Sql or from C#. I created a quick and dirty app to allow me to enter the server, port, credentials, and to/from and I get this response every time:
[Inner Exception]
Unable to connect to the remote server
[Details]
System.Net.Mail.SmtpException: 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 204.12.49.188:25
I've tried multiple logins. I've also confirmed that SMTP is turned on and IMAP/POP3 is turned off. I'd post images of it but unfortunately I can't yet.
The code to send is below and it is about as simple as it gets:
SmtpClient client = new SmtpClient(ServerTextBox.Text, Convert.ToInt32(PortTextBox.Text));
NetworkCredential login = new NetworkCredential(UsernameTextBox.Text, PasswordTextBox.Text);
client.Credentials = login;
MailMessage message = new MailMessage(FromTextBox.Text, ToTextBox.Text, "Wine and Spirits Jobs Alerts", "Below are this week's job alerts: There are no job alerts at this time");
client.SendCompleted += HandleSendCompleted;
client.SendAsync(message, null);
I've also tried it locally on their VPS and on my machine. I've turned on and off the firewall and I've got a specific rule allowing outgoing traffic on 9998 (and you can log into the web interface remotely) so this isn't a matter of being able to contact it.
I'm at a loss of what to do. If someone can help??
EDIT: It's clear I was using the wrong port - 9998 instead of 25. Using port 25 now tells me either time out or that it was actively refused. The firewall does have specific rules for port 25 that allow all traffic, so it shouldn't be stopped.
EDIT 2: I've re-created the MX record on the domain and ensured it is pointing to the correct IP address. I've also re-created the domain in Smartermail and made sure SMTP is turned on, authentication is required, SSL is not, POP and IMAP are turned off, and that my login is correct. I've also tried turning on and off the firewall. No dice.
I finally solved the issue.
It had nothing to do with firewalls or the MX record or using/not using an IP address and everything to do with the fact that while port 25 was indeed assigned to SMTP and SMTP was indeed turned on, the IP address of the domain itself was NOT assigned to the SMTP configuration so SmarterMail wasn't actually monitoring the port.
Problem solved - email sends. Fun stuff! Thank you for everyone's responses!
Try setting SMTP mail server IP address in your mail sending code.
SmtpMail.SmtpServer = "your mail server name or IP address goes here";
SmtpMail.Send(Message);
Your can set this property at global level or in any Initialization code.
Reference : https://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail%28v=vs.110%29.aspx

System.Net.Sockets.SocketException on 2008R2 Server

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.

C# : SMTP error

i am creating an email client that sends e-mail address from server ip instead of SMTP , i wrote the following code:
SmtpClient server = new SmtpClient();
server.Host= "50.23.128.66";
server.Port = 25;
server.Send("from#yahoo.com", "to#yahoo.com", "hi", "hope it works");
but when i run it, i get that error:
Unhandled Exception: System.Net.Mail.SmtpException: Service not
available, closing transmission channel. The server response was:
Cannot connect to SMTP server
50.23.128.66 (50.23.128.66:25), connect error 10061
i actually think that port is wrong . * By the way i am using Windows Server 2008 *
I think "connect error 10061" is the same as errno == ECONNREFUSED on a POSIX platform, which means the connection did not succeed, probably because the host you tried to connect to isn't running any sort of server on port 25.
According to this SMTP Server Test the server is not open to receiving connections on port 25:
No connection could be made because the target machine actively refused it 50.23.128.66:25
It's possible the server uses some form of encryption, and you will have to connect on one of the "secure" SMTP ports.
Are you sure it's an SMTP server, and not an IMAP server or the like?

SMTP : error relaying

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.

Categories

Resources