Email sending through .net code.

This example show's you how to send email through .net code.

For this you have a smtp server with the mail sending permissions.

C# code of email sending:-

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 Mail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        SendMail();
    }

    protected void SendMail()
    {
        MailMessage objEmail = new MailMessage("vinaysmart333@gmail.com", "rajnisharena@gmail.com");//("from","to")
        objEmail.Subject = "Invitation";
        objEmail.Body = "Hi this is a testing mail.";
        SmtpClient smtp = new SmtpClient();
        try
        {
            smtp.Host = "192.168.5.206";
            smtp.Send(objEmail);
            Response.Write("Mail send successfully");
        }
        catch (Exception except)
        {
            Response.Write("Mail send failure " + except.ToString());
        }
    }
}

ASPX code for mail sending:-

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

<!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>
        <asp:Button ID="btnSendMail" runat="server" Text="Send Mail"
            onclick="btnSendMail_Click" />
    </div>
    </form>
</body>
</html>

Download example

No comments:

Post a Comment