+ Reply to Thread
Results 1 to 7 of 7

Thread: Email C#

  1. #1

    Default Email C#

    This tutorial will show you how to send email from your website using c#.net in plain text or HTML format. Below you will find all the functionality and code you need to do this in the simplest format.

    The following code is an example of the complete working project

    Code:
    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Net.Mail" %>
    <script runat="server">
    public void Page_Load(object Src, EventArgs E)
    {
    	subPlainTextEmail();
    	subHtmlEmail();
    }
    
    void subPlainTextEmail()
    {
    	string strTo,strFrom,strBody,strSubject,strBcc;
    	bool bolHtml = false;// set the body content to plain text(false) or HTML(true)
    	strFrom = "info@yourwebsite.com";
    	strTo = "email1@theirdomain.com";// ; Separates emails
    	strBcc = "email2@theirdomain.com; email3@theirdomain.com; email4@theirdomain.com";// ; Separates emails
    	strSubject = "Subject for Plain Text email";
    
    	strBody = "To whom it may concern," + "\r"; //  "\r" is used for a line break
    
    	strBody += "This is an example of a plain text email" + "\r";
    
    	strBody += "Best wishes," + "\r";
    	strBody += "Yourwebsite.com";
    
    	subEmail(strFrom, strTo, strBcc, strSubject, strBody, bolHtml);
    }
    
    void subHtmlEmail()
    {
    	string strTo,strFrom,strBody,strSubject,strBcc;
    	bool bolHtml = true;// set the body content to plain text(false) or HTML(true)
    	strFrom = "info@yourwebsite.com";
    	strTo = "email1@theirdomain.com";// ; Separates emails
    	strBcc = "email2@theirdomain.com; email3@theirdomain.com; email4@theirdomain.com";// ; Separates emails
    	strSubject = "Subject for HTML email";
    
    	strBody = "<html><head></head><body>";
    
    	strBody += "To whom it may concern,<br><br>";
    
    	strBody += "This is an example of an HTML format email<br><br>";
    
    	strBody += "<img src=\"http://www.coolwebawards.com/image/forum_logo.gif\" /><br><br>";
    
    	strBody += "Best wishes,<br>";
    	strBody += "Yourwebsite.com";
    
    	strBody += "</body></html>";
    
    	subEmail(strFrom, strTo, strBcc, strSubject, strBody, bolHtml);
    }
    
    void subEmail(System.String strFrom, System.String strTo, System.String strBcc, System.String strSubject, System.String strBody, System.Boolean bolHtml)
    {
    
    	//*** Written by user CWA, CoolWebAwards.com Forums. 24 February 2010
    	//*** http://forum.coolwebawards.com/threads/103-Email-C
    
    	string strMailServer = "mail.yourwebsite.com";
    
    	MailMessage objMail = new MailMessage();
    	objMail.From = new MailAddress(strFrom);
    	int i;
    	string[] arrArray ;
    
    	arrArray = strTo.Split(';');
    	for (i = 0; i < arrArray.Length; i++) {
    		objMail.To.Add(arrArray[i]);
    	}
    
    	arrArray = strBcc.Split(';');
    	for (i = 0; i < arrArray.Length; i++) {
    		if (arrArray[i] != ""){
    			objMail.Bcc.Add(arrArray[i]); 
    		}
    	}
    
    	objMail.Subject = strSubject;
    	objMail.Body = strBody;
    	objMail.IsBodyHtml = bolHtml;
    	SmtpClient smtp = new SmtpClient(strMailServer);
    	//smtp.Credentials = New System.Net.NetworkCredential("username", "password")
    	smtp.Send(objMail);
    }
    </script>
    Setup

    1. Variables to be passed to “subEmail”
    strFrom – The email address the email is sent from
    strTo – The email address that the email will be sent to. Use “;” to separate emails is you like to add more than one
    strBcc – Send a Blind Carbon Copy (Bcc) to these email address/es. Use “;” to separate emails is you like to add more than one
    strSubject – Email subject line
    strBody – The body text for your email
    bolHtml – Whether or not the email body is HTML (true) or plain text (false)

    2. Change the following line to your mail server address

    Code:
    string strMailServer = "mail.yourwebsite.com";
    3. If your mail server requires authentication, you can uncomment the following line to pass the username and password values which must also be setup on your mail server

    Code:
    smtp.Credentials = New System.Net.NetworkCredential("username", "password");
    CWA - CoolWebAwards.com

  2. #2

    Default

    This is very good example of sending email in asp.net with C Sharp language.Through your information I am also create a email option in my application.
    But I am also send some attachment with my email.so how can i do?

  3. #3

    Default

    C# Email Server (CES). Implements SMTP and POP3 apparatus that can be acclimated as a standalone server or as apparatus in added applications.The C# Mail Server is a C# anchorage of the Java Email Server (JES). JES is an accustomed email server targeted at small centrally maintained installations.

  4. #4
    Ruby Star member nobita_deptrai is on a distinguished road
    Join Date
    Jun 2011
    Posts
    458

    Default

    1-The namespace System.Net.Mail is a part of the class library of .NET framwork 2.0 (currently in BETA version), to send mail using .NET framework 1.1, you'd use System.Web.Mail instead..
    2-There's no class called SmtpClient in the System.Web.Mail namspace, but it's a member of the System.Net.Mail namspace in .NET Framework 2.0.

    To send mail via Microsoft .NET Framework 1.1 (the current version) you can use:

    System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
    message.From="from e-mail";
    message.To="to e-mail";
    message.Subject="Message Subject";
    message.Body="Message Body";
    System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
    System.Web.Mail.SmtpMail.Send(message);

    If you want the shortest way:
    System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
    System.Web.Mail.SmtpMail.Send("from","To","Subject ","MessageText");


    Now, in fact, both segments of code will not work for most SMTP hosts. This is because most SMTP hosts today require authentication (user/pass) to allow you to use the server. To send mail using Authenticated SMTP, many people use third party tools. There are many out there and some of them are good and free, but, System.Web.Mail doesn't need that really. You can send mail via Authenticated SMTP mail servers using the 1st code I wrote, just add the bold lines to it to be:

    System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();

    message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
    message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","SmtpHostUserName" );
    message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","SmtpHostPassword" );

    message.From="from e-mail";
    message.To="to e-mail";
    message.Subject="Message Subject";
    message.Body="Message Body";

    System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
    System.Web.Mail.SmtpMail.Send(message);


    For more detailed information on sending mail using .NET framework 1.1, check this detailed FAQ (take care, it's often detailed more than you may need!).
    For information on the System.Net.Mail namespace in Microsoft.NET Framework 2.0, you may check Visual Studio 2005 Library > .NET Framework Reference > Class Library > System.Net.Mail.
    BDS Real Estate Company
    Go to my website rao vat ban nha to learn about real estate in Vietnam: Home sales , Apartment for sale, Feng Shui ...

  5. #5

    Default

    I have just started studying .net using c# language. I am just a beginner to this technology. Since these codes are similar to java code. So I am getting the basics easily. For the advance stages these codes for “sending email from your website” will definitely going to give me some confidence to jump to advance programming stages. This code can be directly implemented into the website to enable email support. I am having a doubt that if I update my .net framework then how the code will behave, Do I have to modify the code as well.

  6. #6
    Prospect darin9casey is on a distinguished road
    Join Date
    Aug 2011
    Location
    5325 Clay Ter Ne, Washington
    Posts
    1

    Default

    I am a new learner.I am facing problems in inserting an image.Can anyone help?

  7. #7

    Default

    some it is my problem. any one given some any idea..

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts