dnyaneshwar
dnyaneshwar

Reputation:

Sending Email in ASP.NET 2.0

iam trying to implement code of sending mail.i have tried the following procedure.

Configuration

<configuration>
  <!-- Add the email settings to the <system.net> element -->
  <system.net>
    <mailSettings>
      <smtp>
        <network 
             host="localhost" 
             port="25"
             userName="?"
             password="?" />
      </smtp>
    </mailSettings>
  </system.net>

HTML

<table border="0">
    <tr>
        <td><b>Your Email:</b></td>
        <td><asp:TextBox runat="server" ID="UsersEmail" Columns="30"></asp:TextBox></td>
    </tr>
    <tr>
        <td><b>Subject:</b></td>
        <td><asp:TextBox runat="server" ID="Subject" Columns="30"></asp:TextBox></td>
    </tr>
    <tr>
        <td colspan="2">
            <b>Body:</b><br />
            <asp:TextBox runat="server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <asp:Button runat="server" ID="SendEmail" Text="Send Feedback" />
        </td>
    </tr>
</table> 

Code-behind

protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
    '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS'
    Const ToAddress As String = "[email protected]"

    '(1) Create the MailMessage instance'
    Dim mm As New MailMessage(UsersEmail.Text, ToAddress)

    '(2) Assign the MailMessage's properties'
    mm.Subject = Subject.Text
    mm.Body = Body.Text
    mm.IsBodyHtml = False

    '(3) Create the SmtpClient object'
    Dim smtp As New SmtpClient

    '(4) Send the MailMessage (will use the Web.config settings)'
    smtp.Send(mm)
End Sub 

but its not working. error is The transport failed to connect to the server.

Upvotes: 1

Views: 969

Answers (3)

CraigTP
CraigTP

Reputation: 44909

You are instantiating your SmtpClient object using it's default (parameterless) constructor in this line of your code:

'(3) Create the SmtpClient object'
Dim smtp As New SmtpClient

Since sending an email requires that you have access to a valid SMTP server, this constructor will attempt to instantiate the object using the SMTP server settings as defined in your web.config file, specifically in the System.Net section. Below is an example of this:

<system.net>
  <mailSettings>
    <smtp>
      <network host="[your smtp server address]" port="[your smtp port - usually 25]"/>
    </smtp>
  </mailSettings>
</system.net>

If this is is missing, your SmtpClient object has no SMTP server to connect to. This is likely to result in the error message that you are experiencing.

To resolve this, you can either add this section to your web.config file, specifying a valid SMTP server for the SmtpClient object to connect to, or you can omit the and hard-code the server address directly when you instantiate your SmtpClient object by utilising one of it's overloaded constructors that accepts the SMTP server address/port number as parameters. See here for the constructor details.

An example of this would be:

'(3) Create the SmtpClient object'
Dim smtp As New SmtpClient("[your SMTP server address]", 25)

Be aware, however, that although you can specify the SMTP server address/port in the SmtpClient's constructor, it's generally considered better practise to configure these settings in the web.config file and use the default (parameterless) constructor in your code. Using the web.config method allows you to update the SMTP Server address/port that you use without re-compiling your code.

Upvotes: 5

MRG
MRG

Reputation: 3219

If you want to send it using your gMail account. Please change username and password with yours. your web.config should have following settings :

   <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network defaultCredentials="false" host="smtp.gmail.com" port="587" userName="[email protected]" password="xxxxxxxxxxxx" />
      </smtp>
    </mailSettings>
  </system.net>

Your SendMailFunction Should be as below :

protected void SendMail(String strFrom, String strSubject, String strTo, String  strBody)
{
    MailMessage mm = new MailMessage();


    mm.From = strFrom;

    mm.Subject = strSubject ;
    mm.To.Add(strTo);

    mm.Body = strBody;
    SmtpClient smtp = new SmtpClient();
    smtp.EnableSsl = true;

    smtp.Send(mm);

} 

Edit : Please check this SO Answer for information about encrypting SMTP settings in web.config

Upvotes: 0

Brisbe
Brisbe

Reputation: 1618

What are your MailSettings in web.config? I'd say your major problem is probably that you're giving it an invalid host or port.

Upvotes: 0

Related Questions