How to send mail from Gmail without login your Gmail account


In this article I am going to discuss how to send mail from Gmail using Asp.net 3.5 without login your Gmail account.

Write the small line of code as follows:

Sample:

<html>
<body>
    <form id="form1" runat="server">
    <fieldset style="width: 500px; padding: 10px;">
        <legend>Sending email from GMail</legend>
        <div align="left" style="padding: 5px;">
            Your Gmail EmailID<br />
            <asp:TextBox ID="TextBoxSenderEmailId" runat="server" Width="250px"></asp:TextBox><font color=silver>Please enter your GMailId <br />(e.g [email protected])</font><br />
<br />
            Friend's EmailId<br />
            <asp:TextBox ID="TextBoxReceiverEmailId" runat="server" Width="250px"></asp:TextBox><font color=silver>Please enter your friend's emailId<br />(e.g [email protected])</font><br />
            <br />
            Subject<br />
            <asp:TextBox ID="TextBoxSubject" runat="server" Width="350px"></asp:TextBox><br />
            <br />
            Body<br />
            <asp:TextBox ID="TextBoxBody" Width="350px" TextMode="MultiLine" Rows="5" runat="server"></asp:TextBox>
<br /><br />
            <asp:Button ID="btnSendEmail" runat="server" OnClick="btnSendEmail_Click" Text="Send" />
        </div>
    </fieldset>
    </form>
</body>
</html>

Img1.png

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public
partial class SendingMailByGMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    
protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(TextBoxSenderEmailId.Text, TextBoxReceiverEmailId.Text, TextBoxSubject.Text, "");
        MyMailMessage.IsBodyHtml = false;
        MyMailMessage.Body = TextBoxBody.Text;
        //provide Authentication Details need to be passed when sending email from gmail
        System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(TextBoxSenderEmailId.Text, "password");//Sender password

        //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
        System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

        //Enable SSL

        mailClient.EnableSsl = true;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = mailAuthentication;
        mailClient.Send(MyMailMessage);
    }
}


Similar Articles