muybn
muybn

Reputation: 291

socket, smtp and web exceptions

I've been getting an error sending e-mail messages from my application. I checked with my hosting company and got the correct mail server setting but I'm still getting error messages. Any suggestions? Stack trace and code below.

    [SocketException (0x274d): No connection could be made because the target machine actively refused it 68.178.232.62:25]
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +251
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +279

[WebException: Unable to connect to the remote server]
   System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +6136880
   System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +314
   System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21
   System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +322
   System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +146
   System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +222
   System.Net.Mail.SmtpClient.GetConnection() +50
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1496

[SmtpException: Failure sending mail.]
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1829
   Lostpass.EmailUser(User user) +713
   Lostpass.uxUserInfoSubmit_Click(Object sender, EventArgs e) +446
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

    <mailSettings>
    <smtp deliveryMethod="Network" from="support@<domain>.com">
        <network host="relay-hosting.secureserver.net" port="25" defaultCredentials="true"/>
    </smtp>
</mailSettings>

    void EmailUser(User user)
{
    user.ChangePasswordID = Guid.NewGuid();
    user.Save();
    MailMessage email = new MailMessage();
    email.To.Add(new MailAddress(uxEmail.Text));
    email.IsBodyHtml = true;
    email.From = new MailAddress(Settings.LostPasswordEmailFrom);
    email.Subject = Settings.LostPasswordSubject;
    //email.Subject = "your new password";
    email.Body = EmailTemplateService.HtmlMessageBody(EmailTemplates.MembershipPasswordRecovery, new { Body = Settings.LostPasswordText, BeginRequired = "", EndRequired = "", UserName = user.Name, GUID = user.ChangePasswordID.ToString() });
    //email.Body = "your new password is: password";
    SmtpClient client = new SmtpClient();
    //try
    //{
    client.Send(email);
//}
    //catch
    //{ Exception ex; }

    uxSuccessPH.Visible = true;
    uxQuestionPanel.Visible = false;
    uxUserInfoPanel.Visible = false;
    uxUserNameLabelSuccess.Text = uxEmail.Text;
}

Upvotes: 0

Views: 1065

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134095

Are you certain that defaultCredentials is correct? I suspect that's going to send your Windows login credentials, which probably isn't what your ISP needs. I think you need to set the Credentials property on your SmtpClient instance. Something like:

// set your SMTP server login credentials
System.Net.NetworkCredential credentials = 
    new System.Net.NetworkCredential("username", "password");
client.Credentials = credentials;

Upvotes: 1

Related Questions