1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Could u send an Email by using C, try this.

Discussion in 'C' started by techinspiration, Apr 15, 2010.

  1. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Code:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web.Security;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Windows.Forms;
    using System.IO;
    using System.Security;
    using System.Security.Permissions;
    using System.Security.AccessControl;
    using System.Net.Mail;
    using System.Net.Cache;

    public partial class MailMessage : System.Web.UI.Page
    {


    protected string strMailServer = System.Configuration.ConfigurationManager.ConnectionStrings["MailServer"].ToString();
    protected string strFromEmail = System.Configuration.ConfigurationManager.ConnectionStrings["FromEmail"].ToString();
    string strMessage;
    string mailmessage;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void NetWorkEmail()
    {

    mailmessage += txtFirst.Text + " " + txtLast.Text + " "
    + "," + txtAddress.Text + "," + " " + txtPhone.Text + "," + " "
    + txtEmail.Text+ "|";


    SendEmail(mailmessage);

    }//End NetworkWorkEmail

    protected void SendEmail(string mailmessage)
    {
    try
    {

    string tomail = "youremail@something.com";
    strMessage = "This is where you will put your message to send";
    strMessage += "<br>" + "<br>";
    strMessage += "Continued Message";
    strMessage += "<br>" + "<br>" + "End of the message";

    SmtpClient client = new SmtpClient(strMailServer);
    MailAddress from = new MailAddress(strFromEmail);
    MailAddress to = new MailAddress(tomail);
    MailMessage message = new MailMessage(from, to);

    message.Subject = "Your Special Email Message";
    message.Body = strMessage;
    message.IsBodyHtml = true;
    client.Send(message);

    }//end try

    catch (System.Net.Mail.SmtpException e)
    {
    ErrorPage = e.Message.ToString();
    }
    catch (Exception ex)
    {
    throw new Exception("An error occurred while transfer data", ex);
    }

    }//end SendEmail()
    }


    Web.Config Settings

    Code:

    <connectionStrings>
    <add name="MailServer" connectionString="Your mail server IP Address"/>
    <add name="FromEmail" connectionString="someemail@whateveremail.com"/>
    </connectionStrings>
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    FULLY FUNCTIONAL CODE:
    Code:

    using System;
    using System.Collections;
    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.Collections.Generic;
    using System.Windows.Forms;
    using System.IO;
    using System.Security;
    using System.Security.Permissions;
    using System.Security.AccessControl;
    using System.Net.Mail;
    using System.Net.Cache;

    public partial class upload_uploaded_files_Default : System.Web.UI.Page
    {
    protected string strMailServer = System.Configuration.ConfigurationManager.ConnectionStrings["MailServer"].ToString();
    protected string strFromEmail = System.Configuration.ConfigurationManager.ConnectionStrings["FromEmail"].ToString();
    string strMessage;
    string mailmessage;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void NetWorkEmail()
    {

    mailmessage += txtFirst.Text + " " + txtLast.Text + " "
    + "," + txtAddress.Text + "," + " " + txtPhone.Text + "," + " "
    + txtEmail.Text + "|";


    SendEmail(mailmessage);

    }//End NetworkWorkEmail

    protected void SendEmail(string mailmessage)
    {
    try
    {

    string tomail = txtEmail.Text;
    strMessage = "This is where you will put your message to send";
    strMessage += "<br>" + "<br>";
    strMessage += "Continued Message";
    strMessage += "<br>" + "<br>" + "End of the message";

    SmtpClient client = new SmtpClient(strMailServer);
    MailAddress from = new MailAddress(strFromEmail);
    MailAddress to = new MailAddress(tomail);
    MailMessage message = new MailMessage(from, to);

    message.Subject = "Your Special Email Message";
    message.Body = strMessage;
    message.IsBodyHtml = true;
    client.Send(message);
    Response.Write("Message Sent");

    }//end try

    catch (System.Net.Mail.SmtpException e)
    {
    ErrorPage = e.Message.ToString();
    }
    catch (Exception ex)
    {
    throw new Exception("An error occurred while transfer data", ex);
    }

    }//end SendEmail()
    protected void btnSend_Click(object sender, EventArgs e)
    {
    NetWorkEmail();
    }
    }


    FRONT CODE:


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="upload_uploaded_files_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>
    First Name:<br /><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
    <br /><br />
    Last Name:<br /><asp:TextBox ID="txtLast" runat="server"></asp:TextBox>
    <br /><br />
    Address:<br /><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
    <br /><br />
    Phone:<br /><asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
    <br /><br />
    Email Address:<br /><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
    <br /><br />

    </div>
    <asp:Button ID="btnSend" runat="server" Text="Send Email" onclick="btnSend_Click" />
    </form>
    </body>
    </html>
     

Share This Page