Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have written a code where in I am sending an email using an exchange server.
But while execution it throws an error as

code start
C#
MailMessage mail = new MailMessage();
         mail.To.Add("testuser2@company.com");
         mail.From = new MailAddress("testuser1@company.com");
         mail.Subject = "Test Email";
         string Body = "BODY of email";
         mail.Body = Body;
         SmtpClient smtp = new SmtpClient();
         smtp.Host = "exchange_server name">;//ConfigurationManager.AppSettings["SMTP"];
         smtp.Send(mail);



Code end

Below is the error:-

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 ip_address:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) 


Kindly help me.
Also let me know whether I need to do ask our network team to give rights to my user ID which is used here in as from Id to authenticate from exchange server?

Kind Regards,
Satwinder singh
Posted
Updated 19-Sep-11 23:26pm
v3

your best way is authenticating with a valid exchange user when smtp port is not open on firewall.
 
Share this answer
 
can you telnet your exchange server? It says in the error: No connection could be made because the target machine actively refused it ip_address:25. So or the exchange server uses not port 25 for sending mails or you are not allowed to reach the server as from the location where you are. Better talk to the networkadmin who can tell you which of the 2 is wrong.
 
Share this answer
 
Comments
sunny_satwinder 20-Sep-11 6:37am    
Hi,
When i have executed the telnet command it shows the following thing

Connecting To Exchange_server_name...Could not open connection to the host,
on port 23: Connect failed

Also I will ask to the networkadmin on this.
Thanks for your reply.

Regards,
Satwinder singh
Herman<T>.Instance 20-Sep-11 6:41am    
Telnet default connects to port 23. You need to set: telnet exchangeservername 25
and if you read ESMTP in the reply, then type EHLO else type HELO and after that type QUIT. In that case you know the server is reachable
sunny_satwinder 20-Sep-11 9:15am    
Hi i have executed the query
telnet exchangeservername 25 i got the follwoing reply
Connecting To exchangeservername...Could not open connection to the host, on port 25: Connect failed

I think i should talk with my network admin on this.
anyways thanks

Regards,
Satwinder singh
SmtpClient serverobj = new SmtpClient();
serverobj.Credentials = new NetworkCredential("youremail id", "yourpassword");
serverobj.Port = 587;
serverobj.Host = "smtp.gmail.com";
serverobj.EnableSsl = false;
obj = new System.Net.Mail.MailMessage();
obj.From = new MailAddress("anilmeets4u@gmail.com", "AgileLearning.com", System.Text.Encoding.UTF8);
obj.To.Add(ASPxtxtToUser.Text);
obj.CC.Add(txtCcUser.Text);
obj.Priority = System.Net.Mail.MailPriority.High;
obj.Subject = txtSubject.Text;
string date = DateTime.Now.ToString();
obj.Body = ASPxMemo1.Text;
serverobj.Send(obj);


Try this........ it will work


Regards,
Anilkumar.D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900