user2110292
user2110292

Reputation: 3787

Sending email through asp.net

This is my Design page :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table style="border:1px solid" align="center">
    <tr><td>Username:</td><td><asp:TextBox runat="server" ID="txtUsername"></td></tr>
    <tr><td>Password:</td><td><asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></td></tr>
    <tr><td valign="top">Address:</td><td><asp:TextBox runat="server" ID="txtAdress" TextMode="MultiLine"></td></tr>
    <tr><td>Email-id:</td><td><asp:TextBox runat="server" ID="txtEmail"></td></tr>
    <tr><td colspan="2" align="center"><asp:Button runat="server" ID="btnRegister" 
            Text="Register" onclick="btnRegister_Click" /></td></tr>
    </table>

    </div>
    </form>
</body>
</html>

this is my code page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();
        msg.To.Add(txtEmail.Text);
        msg.From = new MailAddress("mysiteadmin@peers");
        msg.Subject="thanks for registraion";
        msg.Body="thanks for registraion";
        SmtpClient obj = new SmtpClient();
        obj.Host = "sri";obj.Send(msg);
        Response.Write("<h2>Registered</h2>");

    }

}

Here am getting an error below.... can you please help

Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 100.64.243.189 (100.64.243.189:25), connect error 10061 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.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 100.64.243.189 (100.64.243.189:25), connect error 10061

Source Error: 
Line 21:         msg.Body="thanks for registraion";
Line 22:         SmtpClient obj = new SmtpClient();
Line 23:         obj.Host = "sri";obj.Send(msg);
Line 24:         Response.Write("<h2>Registered</h2>");
Line 25:

Upvotes: 0

Views: 3223

Answers (6)

You have to give credentials.. check the below code once

        MailAddress to = new MailAddress("Email Id");

        MailAddress from = new MailAddress("Email Id");

        MailMessage mail = new MailMessage(from, to);

        mail.Subject = "";
        mail.Body = "";


        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;

        smtp.Credentials = new NetworkCredential(
            "Email Id", "Password");
        smtp.EnableSsl = true;

        smtp.Send(mail);

You have to add below namespaces

using System.Net.Mail;
using System.Net;

Upvotes: 0

user2110287
user2110287

Reputation:

There may be two cause:

  1. This usually results from trying to connect to a service that is inactive on the foreign host.

  2. Sometimes a 10061 error is caused by either a firewall or anti-virus software presence on the local computer or network connection.

Either one may be blocking the ports needed to make a successful connection to the server. Either you went to the wrong host, or the server application you're trying to contact is not executing. Check the destination address you are using. Check, if you used a hostname, did it resolve to the correct address or not..

Upvotes: 1

Mehdi Bugnard
Mehdi Bugnard

Reputation: 3979

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.Subject = "Title";
message.From = new System.Net.Mail.MailAddress(yourEmailExpeditor);
message.Body = "content message here";

//Initialize the smtp
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(adress,int.Parse(port));
//Send the message
smtp.Send(message);

Upvotes: 0

tkestowicz
tkestowicz

Reputation: 332

You can apply SMTP settings in web.config by putting this section inside:

    <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="...">
        <network host="smtp.gmail.com" port="587" defaultCredentials="false" userName="" password="" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>

This example shows configuration for Gmail SMTP server.

Upvotes: 0

abc
abc

Reputation: 157

Make sure to set up smtp on web config file

<system.net>
<mailSettings>
  <smtp>
    <network host="smtp@server" port="25" userName="user" password="password" defaultCredentials="true" />
  </smtp>
</mailSettings>
 </system.net>

Upvotes: 0

Ravi Gadag
Ravi Gadag

Reputation: 15881

  1. Make sure that you have a smtp server setup. add smtp server info in web.config. then try this.

you need to mention the smtp server url and the port number. SMTp

   SmtpClient obj = new SmtpClient(yourSmtpServerURL);
 NetworkCredential myCreds = new NetworkCredential("[email protected]", "YourPassword", "");           
            obj.Credentials = myCreds;

then now call obj.send .

Upvotes: 1

Related Questions