Error sending filled form via email

This is the code I use to send my form via email:

protected void sendMail(string strFrom, string strBody)
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress(strFrom);
mail.To.Add("myEmail@myDomain.com");

//set the content
mail.Subject = txtSubject.Text;
mail.Body = strBody;
mail.IsBodyHtml = false;

//send the message
SmtpClient smtp = new SmtpClient("noAuth.myDomain.com");
smtp.Send(mail);
}

And this is the error message I received:

Server Error in '/home' Application.
--------------------------------------------------------------------------------

Unable to read data from the transport connection: net_io_connectionclosed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Source Error:

Line 84: //send the message
Line 85: SmtpClient smtp = new SmtpClient("noAuth.myDomain.com");
Line 86: smtp.Send(mail);
Line 87: }
Line 88: }

Source File: e:\wwwroot\home\email.aspx.cs Line: 86


Answer this question

Error sending filled form via email

  • Will Pearson

    Are you sure there is a Smtp server running on that domain and on port 25, if it is running on a other port you need to specify that to.

    Have you tried other Smtp servers, it doesn't seems to be an error in your code but just an error in the connection with the Smtp server.


  • vickey37139

    Just curious, how do I use the specify credentials I like to try the server that requires authentication and see if it works.

  • Jean Pierre LaBonte

    ljCharlie,
    you can write:

    smtp.Credentials = new NetworkCredential ("myusername", "mypassword");

    this should go before the Send ().

    Regards
    --mc


  • Santiago C.

    Use a mailmessage and specify the fields sendusername and sendpassword, here is a working example:


    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
    mail.From = "pj.vandesande@gmail.com";
    mail.To = "somebody@msn.com";
    mail.Subject = "Hello message!";
    mail.Body = "Body of the message";
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1"); //basic authentication
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "USERNAME");
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "PASSWORD");

    System.Web.Mail.SmtpMail.SmtpServer = "mysmtp.server.com";

    System.Web.Mail.SmtpMail.Send( mail );



  • Raschmann

    You need the System.Net namespace.

    --mc


  • mroctober

    You have to use the System.Net namespace. Put using System.Net; on top of you file or use:


    smtp.Credentials = new System.Net.NetworkCredential ("myusername", "mypassword");


  • KrisJ

    No, it still not working. However, this is the ASP version and it's working.

    Dim ObjSendMail
    Dim iConf
    Dim myField

    Set ObjSendMail = Server.CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set myField = iConf.Fields

    myField("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    myField("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "noAuth.myDomain.com"
    myField("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    myField.Update

    Set ObjSendMail.Configuration = iConf

    ObjSendMail.Subject = request.Form("subject")
    ObjSendMail.From = "webUser@yahoo.com"

    ObjSendMail.HTMLBody = unescape(request.Form("body"))

    do until emailRS.EOF
    if len(emailRS("email")) > 5 then
    'response.Write("sending mail")
    ObjSendMail.To = emailRS("email")
    ObjSendMail.Send
    end if
    emailRS.MoveNext
    Loop

    So how do I do the same thing in C#

  • jnelsonjr

    Thanks! This is the error I received:

    The type or namespace name 'NetworkCredential' could not be found (are you missing a using directive or an assembly reference )

  • Roxanne163

    Many thanks for the response. So if it is port 25 then I don't have to specify, correct I am pretty sure there is a smtp server because it is working in my PHP version of the web form.

  • Error sending filled form via email