Apr 16, 2011

Asp. net 2.0/3.5/4.0 Email code for gmail/hotmail/yahoo

just copy the default.aspx code and remove the comment

default.aspx code

<%--<body>
    <form id="form1" runat="server">
    <div>
    <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 yourId@gmail.com)</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 abc@xyz.com)</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>

    </div>
    </form>
</body>--%>


just copy the sendemail button code and put it in button click event
and make necessary changes

default.aspx.cs

protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        try
        {
            //MailMessage class are used to construct e-mail messages
            MailMessage message = new MailMessage();

            // Collaboration Data Objects (CDO) library allows you to access the Global Address
            // List and other server objects, in addition to the contents of mailboxes
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);

            //basic authentication
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc@gmail.com");

            //set your username here
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");

            //set your password here
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "True");

            message.From = "xyz@gmail.com";
            message.To = "abc@gmail.com";
            message.Subject = TextBoxSubject.Text ;
            message.Body = TextBoxBody.Text ;

            //if (!txtAttach.Text.Equals("")) //adding attachments.
            //{
            //    MailAttachment attach = new MailAttachment(txtAttach.Text);
            //    if (attach != null)
            //    {
            //        message.Attachments.Add(attach);
            //    }
            //}
            SmtpMail.SmtpServer = "smtp.gmail.com";
           
            //The real server goes here
            SmtpMail.Send(message);

            Response.Write("Your message has been sent.");
            //MessageBox.Show("Your message has been sent.", "Mail Sent",MessageBoxButtons.OK, MessageBoxIcon.None);
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    //I have to attach a file so,I created a button called btnAttach.
    private void btnAttach_Click_1(object sender, EventArgs e)
    {
        //DialogResult clicked = OpenattachDialog.ShowDialog();
        //if (clicked.Equals(DialogResult.OK))
        //{
        //    txtAttach.Text = OpenattachDialog.FileName;
        //}
    }//btnAttach






No comments:

Image Gallery - Image Add/Delete/Preview - Single Page App -

https://github.com/TheKalin/jQuery-File-Upload.MVC5 Helper Class / Models   public class FilesHelperDB     {         dbImageGallery...