hello, all

i have query regarding how to send mail in asp.net using C#.net

for that i have use the following code

but still not able to send

please give me your valuable suggestion

System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page 
{
    #region  "Send email"
    protected void btnSendmail_Click(object sender, EventArgs e)
    {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost 
            smtpClient.Host = "localhost";

            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("john_begginer@yahoo.co.in");
            message.Subject = "Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
           // message.CC.Add("");
           // message.CC.Add("");

            // You can specify Address directly as string
            //message.Bcc.Add(new MailAddress(""));
            //message.Bcc.Add(new MailAddress(""));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = false;

            // Message body content
            message.Body = txtMessage.Text;
         
            // Send SMTP mail
            smtpClient.Send(message);

            lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
        }
    }
    #endregion

    #region "Reset"
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtMessage.Text = "";
        txtEmail.Text = "";
    }
    #endregion
}

Dani AI

Generated

Brief summary and diagnostic path that complements the thread

As shows, classic ASP.NET examples commonly instantiate System.Net.Mail.SmtpClient — that still works on .NET Framework when you have a reachable SMTP server and correct credentials, but Microsoft now says SmtpClient is not recommended for new development and points developers to modern libraries instead. (learn.microsoft.com) Also keep SMTP settings out of code and in Web.config (the <system.net><mailSettings> section) for maintainability on older apps. (learn.microsoft.com)

Checklist to narrow the problem (do these in order)

  1. Confirm the SMTP host/port is reachable from the web server (firewall/NAT can block outbound ports).
  2. Verify authentication and TLS mode match the provider (STARTTLS on 587 vs implicit SSL on 465). Gmail/Workspace has specific requirements (app passwords or OAuth, and the correct ports). (developers.google.com)
  3. If you see “unable to relay” or similar, the server-side relay rules are the likely blocker — that needs correcting on the SMTP/relay host. (learn.microsoft.com)
  4. Check server antivirus / on-delivery scanners (as @SanaMirza suggested) and log the full exception (InnerException/stack) for the exact SMTP response.

Modern recommendation (if you can change libraries)
Use MailKit/MimeKit for new .NET projects (cross-platform, modern auth, better TLS handling). Example (minimal):

var msg = new MimeKit.MimeMessage();
msg.From.Add(new MimeKit.MailboxAddress("Sender","from@example.com"));
msg.To.Add(new MimeKit.MailboxAddress("Recipient","to@example.com"));
msg.Subject = "Test";
msg.Body = new MimeKit.TextPart("plain"){ Text = "Hello" };

using (var client = new MailKit.Net.Smtp.SmtpClient()) {
  client.Connect("smtp.example.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
  client.Authenticate("username","password");
  client.Send(msg);
  client.Disconnect(true);
}

See MailKit docs for details and advanced scenarios (OAuth, connection reuse, TLS options). (mimekit.net)

Recommended Answers

All 9 Replies

You need to set Relay IP-Address. (Relay restrictions).

Steps:

1. Open IIS applet from the control panel
2. Go to0 Default SMTP Virtual Server
3. Select tab Access + Click on Relay.
4. Add IP of localhost 127.0.0.1

Hi....

actually i have also try this i seen the web site which is given below

http://www.ehow.com/how_4489548_set-up-smtp-server-windows.html

and also


but still the result is same [:(]


You need to set Relay IP-Address. (Relay restrictions).

Steps:

1. Open IIS applet from the control panel
2. Go to0 Default SMTP Virtual Server
3. Select tab Access + Click on Relay.
4. Add IP of localhost 127.0.0.1

i want make software for window chat with text,picture,file,video,etc; i want use LAN connect server
.pls give me design for window and also code.

Additionally if you have Virus Scan Install,just disable 'Access Protection" and On- Delivery Email Scanner".
This wil help to send mail from your system.

Thanks

good one
but to send email to many we can try it

Good to read it. Understand and play around the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.