user1093651
user1093651

Reputation: 2021

How to send email by Page onLoad when the user enters to this page?

I am a new ASP.NET developer and I could be able to send email by the web application that I am working on it. Now, I want to create a page that when the user opens it, it will send email automatically. How to do that?

Upvotes: 0

Views: 475

Answers (3)

Alex Kovanev
Alex Kovanev

Reputation: 1888

First of all you can add some code to your Web.Config. In my example I'm using gmail server. Knowing the settings of your server you can easy change it.

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

Secondly you can modify your pageload event

protected void Page_Load(object sender, EventArgs e)
{
    MailMessage mailMessage = new System.Net.Mail.MailMessage();
    mailMessage.To.Add("[email protected]");
    mailMessage.Subject = "Some subject";
    mailMessage.Body = "Some text";

    using (var smtpClient = new SmtpClient())
    {
        smtpClient.Send(mailMessage);
    }
}

Upvotes: 1

huMpty duMpty
huMpty duMpty

Reputation: 14470

Have a look at this tutorials about Use ASP.NET to Send Email from a Web Site and Sending Email with ASP.NET

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039368

You could use the SmtpClient class to send an email. The documentation contains an example of its usage.

Upvotes: 1

Related Questions