Similarly to the php mail function you can use C# to send emails via a hosted page. You can send email from a web page – just follow the tutorial below.
- Make sure you are using a Win account, not one on a Linux server.
- Connect to your files via an FTP client.
- In the www directory, create two files – one named mail.aspx and one named web.config
- In the web.config enter the following and save:
<configuration> <system.web> <customErrors mode="Off"/> </system.web> </configuration>
- In the mail.aspx file enter the following template and customize it as per the rules below.
<%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Net.Mail" %> <script language="C#" runat="server"> protected void Page_Load(object sender, EventArgs e) { MailMessage mail = new MailMessage(); mail.From = new MailAddress("mailaddress@domain.ext"); mail.To.Add("mailaddress-receiver@domain.ext"); mail.Subject = "This is test subject"; mail.Body = "This is the c sharp mail content"; SmtpClient smtp = new SmtpClient("yourmailserver.domain.ext"); NetworkCredential Credentials = new NetworkCredential("mailaddress@domain.ext", "password"); smtp.Credentials = Credentials; smtp.Send(mail); lblMessage.Text = "Mail Sent"; } </script> <html> <body> <form runat="server"> <asp:Label id="lblMessage" runat="server"> </asp:Label> </form> </body> </html>
- Customize the template like so (and make sure you keep the quotes):
- Change the email address in the mail.From line. This is the sender.
- Change the email address in the mail.To.Add line. This is the receiver.
- To change the subject edit the mail.Subject line.
- To change the content of the message edit the mail.Body line
- The email address in the NetworkCredential Credentials line is the sender – you need to authenticate with a password
- The ‘password’ in the same line is the password of your email address.
- Save/Upload.
- In the web.config enter the following and save:
- Test! Open the .aspx file in your browser and you should receive your email.