Sending mail in asp.net with smtp

Leave a Comment
How to send mail or message in asp.net using smtp client.

Complete code is given to send mail in asp.net

Aspx page will give you complete design part:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
ToAddress:&nbsp;&nbsp; <asp:TextBox runat="server" ID="toaddress"></asp:TextBox><br /><br />
fromAddress:<asp:TextBox runat="server" ID="fromaddress"></asp:TextBox>
        <br />
        <br />
Subject:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox runat="server" ID="sub"></asp:TextBox><br />
Message:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox runat="server" ID="mes"></asp:TextBox>
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnsub" runat="server" Text="Send" onclick="btnsub_Click" Width="73px"/>
        <br />
        <br />
        <br />
<asp:Label runat="server" ID="lblsub"></asp:Label>
</div>
    </form>
    <p>
&nbsp;
    </p>
</body>
</html>
aspx.cs file:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnsub_Click(object sender, EventArgs e)
    {
        SmtpClient smtpclient = new SmtpClient();
        MailMessage message = new MailMessage();
        //SmtpClient smtpclient = new SmtpClient();
        //MailMessage message = new MailMessage();
        //MailAddress fromAdress = new MailAddress(txtFromAddress.Text, "Rathesh");
        try
        {
            message.From = new MailAddress(fromaddress.Text);
            message.To.Add(toaddress.Text);
            message.Subject = sub.Text;
            message.Body = mes.Text;
            //message.From = new MailAddress(fromaddress.Text);
            //message.To.Add(toaddress.Text);
            //message.Subject = sub.Text;
            //message.Body = mes.Text;
            smtpclient.Host = "smtp.gmail.com";
            //smtpclient.Host = "smtp.gmail.com";
            smtpclient.EnableSsl = true;
            //smtpclient.EnableSsl = true;
            //smtpclient.UseDefaultCredentials = true;
            System.Net.NetworkCredential network = new System.Net.NetworkCredential();
            //System.Net.NetworkCredential network = new System.Net.NetworkCredential();
            network.UserName = fromaddress.Text;
            //network.UserName = fromaddress.Text;
            network.Password = "password";
            smtpclient.UseDefaultCredentials = true;
            //smtpclient.UseDefaultCredentials = true;
            smtpclient.Credentials = new System.Net.NetworkCredential(fromaddress.Text, "password");
            //smtpclient.Credentials = new System.Net.NetworkCredential(fromaddress.Text, "9978562737");
         
            //smtpclient.Credentials = network;
            smtpclient.Port = 25;
            smtpclient.Send(message);

         
            lblsub.Text = "messagesend";
        }
        catch (Exception ex)
        {
            lblsub.Text = ex.ToString();
        }
    }
    }
Output:
This is the very simple example to send mail in asp.net. now you can send mail in group also for it you need just making loop in aspx.cs file.



0 comments:

Post a Comment