Wednesday, 27 August 2014

Send Emails using Godaddy in asp .net

In this article we will learn how to set up email address on godday hosting provider and to send emails using godaddy and asp .net. Godaddy is an internet domain registrar and hosting company. To send emails using godday one must have their website hosted at Godaddy hosting server.


Designer Code:


<%@ 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></title>
    <style type="text/css">
        body
        {
            font: 11px verdana;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td colspan="2">
                <asp:Label ForeColor="Green" ID="lblConfirmationMessage" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <b>Email To</b>
            </td>
            <td>
                <asp:TextBox Width="300px" runat="server" ID="txtEmailTo" />
            </td>
        </tr>
        <tr>
            <td>
                <b>Subject</b>
            </td>
            <td>
                <asp:TextBox Width="300px" runat="server" ID="txtSubject" />
            </td>
        </tr>
        <tr>
            <td style="vertical-align: top;">
                <b>Message</b>
            </td>
            <td>
                <asp:TextBox Width="300px" Height="100px" TextMode="MultiLine" runat="server" ID="txtEmailMessage" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:Button Text="Send Email" ID="btnSendEmail" runat="server" OnClick="btnSendEmail_Click" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


 Code Behind:

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

    }
    protected void btnSendEmail_Click(object sender, EventArgs e)
    {


        MailMessage message = new MailMessage();
        SmtpClient smtpClient = new SmtpClient();
        string msg = string.Empty;
        try
        {
            MailAddress fromAddress = new MailAddress("EmailAddressYouHaveCreatedOnGodaddy");
            message.From = fromAddress;
            message.To.Add(txtEmailTo.Text);
            message.Subject = txtSubject.Text;
            message.IsBodyHtml = true;
            message.Body = txtEmailMessage.Text;
            smtpClient.Host = "relay-hosting.secureserver.net";   //-- Donot change.
            smtpClient.Port = 25; //--- Donot change
            smtpClient.EnableSsl = false;//--- Donot change
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("EmailAddressYouHaveCreatedOnGodaddy", "PasswordSuppliedDuringEmailCreation");

            smtpClient.Send(message);

            lblConfirmationMessage.Text = "Your email successfully sent.";
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
    }
}

Download



No comments:

Post a Comment