I am currently trying to use SmtpClient to send an email with exchange 2003. Using the code below it will not send an email, but it does not throw an exception which i assume means it is making a connection to the exchange server because. Also for the server setting I tried mail.server.com as well as the IP address and it still does not throw an exception.
public static void emailTest()
{
string fromEmail = #"me#me.com";
string ToEmail = #"me#me.com";
string body = "C Stuck Batches";
string subject = "C Stuck Batches";
try
{
SmtpClient MyMail = new SmtpClient("x.x.x.x");
MyMail.DeliveryMethod = SmtpDeliveryMethod.Network;
MyMail.UseDefaultCredentials = false;
MyMail.Credentials = new NetworkCredential(#"domain\user", "password");
MyMail.Send(fromEmail, ToEmail, subject, body);
MessageBox.Show("Sent", "SENT", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show("Exception", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
I have read through several other problems, but as far as I could tell most others were getting some sort of exception or timeout. Since this appears as though it connects I am not sure what the issue is. Would it throw an exception if it were unable to access the server? If I throw a different IP into the server it will throw an exception.
Also I have tried both the IP of the server and mail.xx.com and it will not throw an exception with either but if I put any other addresses it will fail.
Thanks in advance for anyhelp!
Is your Exchange server set up to relay mail from your IP address? If it isn't, mail may be "silently" discarded without throwing an Exception (the client is connecting correctly, it's the back end that's discarding the email)
This link provides more information on how to configure relaying on Exchange 2003 (I'm assuming "eSupport" would equate to your application in this example i.e. the IP of the machine running your code).
Log into Exchange 2003 server (192.168.5.25 in my example above) and start Exchange System Manager.
Expand the organization_name object, and then expand the Servers node. Expand the server_name object of the server on which you want to control mail relay, and then expand the Protocols node.
Expand the SMTP node, right-click the virtual SMTP server on which you want to control mail relay, and then click Properties
Click Relay.
In the Relay Restriction dialog box, several options are available. The Only the list below option is enabled by default; the list below this option is empty.
Click Add. Enter the IP address of the server running eSupport (192.168.5.80 in my example above).
In the Relay Restrictions dialog box, click OK.
Click Apply, and then click OK in the Default SMTP Virtual Server Properties dialog box.
The issues i had was that even the domain admins did not have access to the SMTP relay. I had to make sure the user was authenticated in the SMTP relay. Regardless of the fact the authenicating user was the same as the email adress used, that user did not have access. Once corrected it was able to send without issue.
Related
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
I have narrowed this down to an issue with IIS. I am using system.net.mail to send emails. They work on my local box, but when I publish them to my IIS8 Server 2012 Server, the emails fail to send.
Try
Dim mailMessage As New MailMessage("test#test.com", Me.ddlApprovers.SelectedValue)
mailMessage.Subject = "TEST"
mailMessage.Body = "TEST"
Dim smtpClient As New SmtpClient(My.Settings.SMTP, 25)
smtpClient.Send(mailMessage)
Catch ex As Exception
Me.lblAlertText.Text = "An error send email has occured. Contact your system administrator."
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "LaunchAlertPopup", "$(function() { AlertMessage(); });", True)
End Try
I have downloaded an SMTP test tool and send emails just fine through it, so it has to be an issue with this website in IIS.
Many ISPs block port 25. Try sending through 465 or 587. It would also be worth checking for any firewall rules that could be blocking any of these ports.
We can only assume that you're using a valid host name for your SMTP client. This needs to exist or your messages will never send.
I know that this question looks like duplicate of dozens other questions while its not.
When ever i try to send an email on my local machine through my web application an SMTPException is thrown and the exceptions is :
//on this line : SmtpServer.Send(mail);
Unable to read data from the transport connection: net_io_connectionclosed.
While the code on production is working perfectly, the same code, the same connections, the same credentials, i'm using IP instead of alias, i tried to turn off the firewall on my local machine, and nothing helped me to fix this issue.
While on my local machine is used to work previously, can anyone give just a hint what could be the problem that raising this issue?
If you are on a residential internet connection, quite often your ISP will block outgoing email sends by blocking all outbound connections to port 25. This is quite common here in the US. Try connecting to a local email server over TCP/IP, or to one on your own internal network.
This thread contains some.
For instance: it looks like assigning a static IP might solve the problem.
What this error means is that System.net.mail was unable to find the
smtp server.
The answer will vary depending on whether you have a fixed IP or a
dynamic IP but, basically, you need to assign a valid IP to your smtp
server.
With fixed IP's this is relatively straightforward. With dynamic IP's
it takes a bit of tweaking.
Open the IIS Manager and check the properties for the smtp server.
In the Default SMTP Virtual Server's properties, in the "Access" tab,
in the Connection Control and Relay dialogs, make sure that your local
IP is assigned. ( In my case, it's 10.0.0.2... )
You may also need to modify your hosts file, to point 127.0.0.1 to
your machine name. ( \WINDOWS\system32\drivers\etc\hosts )
Then, in your code, assign your machine name to the smtp client :
Dim client As New SmtpClient("yourmachinename") client.Send(mail)
Alternatively another guy in the same thread seems to have found a workaround for the SMTP connection not being correctly closed.
Set SmtpClient.ServicePoint.MaxIdleTime = 1 according to a supported
work-around:
http://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=146711
which makes all smtp work properly.
Here's a complete sample:
MailMessage msgMail = new MailMessage();
msgMail.To.Add(new MailAddress("info#somedomain.se"));
msgMail.Subject = "Message from web";
msgMail.IsBodyHtml = true;
msgMail.Body = "Test message";
SmtpClient Client = new SmtpClient(); /* uses settings form web.config */
Client.ServicePoint.MaxIdleTime = 1; /* without this the connection is idle too long and not terminated, times out at the server and gives sequencing errors */
Client.Send(msgMail);
msgMail.Dispose();
System.Net.NetworkCredential(user, password); ///Wrong
System.Net.NetworkCredential(email, password); ///right
because some email server is shability
In my case, I had to leave out the Domain property from Network Credentials when setting username and password
The Gmail account you are using has limit for number of emails being sent out per day/month and if you exceed that limit, it will reject any further outgoing emails.
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 mail through my Google Apps email account. This is setup on my own domain and is all working fine through the web interface and through outlook.
However, i'm trying to send an email from a webpage using C#, I get no exceptions and everything appears to go smoothly, but the emails never seem to arrive:
MailMessage msg = new MailMessage("no-reply#xxxx.co.uk", "dan#xxxx.co.uk");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("no-reply#xxxx.co.uk", "xxxxxxxxx");
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.Body = body;
smtp.Send(msg);
The body is a string which i've generated earlier in the code. As far as i'm aware, if there was a problem with actually connecting to the SMTP server at Gmail then I would get an exception thrown.
Any ideas what could cause this not to work? Only thing different I can see compared to other peoples examples is that I have set body as HTML and i'm sending from a Google Apps account rather than an #gmail.com account.
The google apps itself is all configured fully and working, i've set this up numerous times so I know it's not likely to be a problem there.
I also tried sending on port 25, as that's what you use when configuring Outlook to send from a Gmail account.
Same result for both, no exception thrown, but email never arrives. Both emails the sender and receiver are on my domain using Google Apps for Gmail.
EDIT:
Also I should mention that I have signed in using both accounts, and both have IMAP and POP enabled in their settings etc.
New findings
This is a strange one
If I send mail manually from:
no-reply#mydomain.com to dan#mydomain.com - Then it works
But if I send it through code this way...it doesn't work...
If I send the following through code it does work:
no-reply#mydomain.com to me#gmail.com or me#ntlworld.com - This works through code!
I would then be led to think that this means a problem with dan#mydomain.com receiving messages...But it receives any messages sent manually from any google, hotmail, or ntlworld email address i've tried.
So either Google Apps accounts can't receive email sent through code (unlikely) or something else is at play here
The server may silently discard your message if there is a problem with it.
- Despite any spec saying otherwise.
The message might be lost in the ether despite being "delivered".
The SMTP server may be applying severe filtering and might additionally require that your sender and destination email addresses match up 'correctly'
I suggest trying with a different SMTP host just to check. :)
When you setup Google Apps, even if the MX records are all working correctly for sending mail manually to/from other accounts, there can still be problems.
The answer is to wait 12-24 hours after setting up your MX Records, even if everything else is working fine.
If you receive no exception, then to me this is the only answer at the moment.
All is working correctly now. Even though all MX records were correct on my DNS and email appeared to be working, there were still changes going on in the background