Monday, November 16, 2009

Work Stuff: System.Net.Mail Notes

Spent a rainy afternoon wrestling with Windows Server 2008, IIS 7

First 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 127.0.0.1:25
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
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.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at webservice.sendNotification(Int32 iAppID, String strFromUID, String strToUID, String strMessage) in c:\inetpub\wwwroot\webservice.ashx:line 1001

Solution: Go to Services and make sure the SMTP Service is running (Don't trust the Feature Manager)

Second Problem:
System.Net.Mail.SmtpFailedRecipientsException: Unable to send to all recipients. ---> System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay for nickfurry@live.com
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at webservice.sendNotification(Int32 iAppID, String strFromUID, String strToUID, String strMessage) in c:\inetpub\wwwroot\webservice.ashx:line 1001

Solution:
SmtpClient objClient = new SmtpClient();
objClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
objClient.Send(objMail);

The Delivery Method is the key.