C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Sending Email Through ASP.NET using C#


Posted Date: 01 Oct 2008    Resource Type: Code Snippets    Category: Email

Posted By: Meetu Choudhary       Member Level: Gold
Rating:     Points: 10



The following Sample Code is the code which can be used to send mails through ASP.Net C#.... its a working code for me.....


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Calling the function SendMail
Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body"));
}

public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "mail.server.com";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

smtpClient.Send(message);
msg = "Successful";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}

}



--
Thanks and Regards
Meetu Choudhary



Attachments

  • Email.aspx (21608-32324-Enquiry.aspx.txt)
  • Email.aspx.cs (21608-32325-Enquiry.aspx.cs.txt)



  • Responses

    Author: Narayanan Sukumaran    03 Oct 2008Member Level: Gold   Points : 1
    I have tried the above code, but i got namespace error message.
    I tried to attach the below DLL, but it is not there.

    using System.Web.UI.WebControls.WebParts;
    using System.Net.Mail;

    Please guide me on this?


    Author: Meetu Choudhary    03 Oct 2008Member Level: Gold   Points : 1
    Mr. Narayanan Sukumaran
    I have attached the files here please check them and try.. then let me know

    --
    Thanks and Regards
    Meetu Choudhary


    Author: sheetal jagdale    04 Oct 2008Member Level: Silver   Points : 2
    Yes ,This is working.
    Thanx , How to attached some file with this ??
    & if i want to send same mail to some say 5000 people ,where the email addresses comes from database (sql server2005),how i will do this ?? how much time it takes to send mail to 5000 people ??

    Pl.answer

    Thanx anyway
    sheetal


    Author: sheetal jagdale    04 Oct 2008Member Level: Silver   Points : 2
    Yes ,This is working.
    Thanx , How to attached some file with this ??
    & if i want to send same mail to some say 5000 people ,where the email addresses comes from database (sql server2005),how i will do this ?? how much time it takes to send mail to 5000 people ??

    Pl.answer

    Thanx anyway
    sheetal


    Author: Meetu Choudhary    04 Oct 2008Member Level: Gold   Points : 2
    Hi, sheetal ji

    If you want to send it to the more then one people then you can use cc and bcc list it is already implemented in the function u just have to fetch the list from the database and then you can pass that comma separated string to the cc or bcc parameter...




    --
    Thanks and regards
    Meetu Choudhary



    Author: Babu    20 Oct 2008Member Level: Bronze   Points : 1
    Hi All,
    Can you send the code for sending mail in ASP.NET using VB. Please it’s very urgent. Now I’m Begging level for developing the website. So please send the code as soon as possible



    Author: Meetu Choudhary    21 Oct 2008Member Level: Gold   Points : 2
    Mr. Babu

    Please Try This and Let Me know if any problems


    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports System.Collections
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Web.UI.HtmlControls
    Imports System.Net.Mail

    Public Partial Class [Default]
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Calling the function SendMail
    Response.Write(SendMail("meetuchoudhary@gmail.com", "meetudmeet@gmail.com", "meetudmeet@yahoo.com", "Test Mail", "Test Mail Body"))
    End Sub

    Public Function SendMail(ByVal toList As String, ByVal from As String, ByVal ccList As String, ByVal subject As String, ByVal body As String) As String
    Dim message As New MailMessage()
    Dim smtpClient As New SmtpClient()
    Dim msg As String = String.Empty
    Try
    Dim fromAddress As New MailAddress(from)
    message.From = fromAddress
    message.[To].Add(toList)
    If ccList <> Nothing AndAlso ccList <> String.Empty Then
    message.CC.Add(ccList)
    End If
    message.Subject = subject
    message.IsBodyHtml = True
    message.Body = body
    smtpClient.Host = "mail.server.com"
    smtpClient.Port = 25
    smtpClient.UseDefaultCredentials = True
    smtpClient.Credentials = New System.Net.NetworkCredential("info@server.com", "password")

    smtpClient.Send(message)
    msg = "Successful"
    Catch ex As Exception
    msg = ex.Message
    End Try
    Return msg
    End Function

    End Class

    ==
    Thanks and Regards
    Meetu Choudhary


    Author: Gaurav Arora    31 Oct 2008Member Level: Gold   Points : 2
    Hi Meetu!

    All above is great I must appreciate you for that.
    One more thing, If I am not wrong then we also can send the mail using Stored Proc.
    If yes, then why should we go for that much heavy coding.
    With the use of SQL we can mange the same very well.

    Hope you are agree with me.

    Thansk & regards,
    Gaurav Arora


    Author: Meetu Choudhary    31 Oct 2008Member Level: Gold   Points : 1
    Hi Gaurrav,

    Yes i do agree with you but what if in some sites we do not use datbases and still we want to send feebacks or other stuff for this only need i don't think that we must go for database and stored procedures.

    Hope you too agree with me

    ++
    Thanks nd Regards
    Meetu Choudhary




    Author: Pradeep Y    31 Oct 2008Member Level: Gold   Points : 0
    I DISAGREE WITH YOU ALL... buhahahahaha ;)


    Author: Nandini P    05 Nov 2008Member Level: Silver   Points : 1
    hi!!

    Thank you so much for this code snippet..i used this snippet after trying other codes which of course doesn't work..and your code worked for me..it is easy to understand and use even after some modifications..
    Thanks again..
    keep doing the great work :)


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sending mail through c#  .  Sending mail through asp.net  .  Sending mail  .  Sending Email  .  Email using c#  .  Email using asp.net  .  Email through asp.net c#  .  Email  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: How to send Mail using VB.NET
    Previous Resource: Sending Email Through ASP.NET using VB
    Return to Discussion Resource Index
    Post New Resource
    Category: Email


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design

    internet fax

    Contact Us    Privacy Policy    Terms Of Use