C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Sending Email Through ASP.NET using C#


Posted Date: 01 Oct 2008    Resource Type: Code Snippets    Category: Email
Author: Miss Meetu ChoudharyMember Level: Diamond    
Rating: 4 out of 54 out of 54 out of 54 out of 5Points: 35



Sending Email Through ASP.NET using C#



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)

    For more details, visit http://aspnetbymeetu.blogspot.com/2009/03/sending-email-through-aspnet-using-c.html



  • 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: Miss Meetu Choudhary    03 Oct 2008Member Level: Diamond   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: Miss Meetu Choudhary    04 Oct 2008Member Level: Diamond   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: Miss Meetu Choudhary    21 Oct 2008Member Level: Diamond   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: Diamond   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: Miss Meetu Choudhary    31 Oct 2008Member Level: Diamond   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: Nandini P    05 Nov 2008Member Level: Gold   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 :)


    Author: usha    11 Feb 2009Member Level: Silver   Points : 1
    hi!

    i got the error that is
    "failure sending mail"
    one more question 1) how to check that my system has e_mail exchange server
    plz guide me ....
    thanks and regards
    usha


    Author: Miss Meetu Choudhary    11 Feb 2009Member Level: Diamond   Points : 1
    Hi Usha,
    I will suggest you once download the files with this article and run them still u get the error then display the error message in the label and let me know the error then i may help you


    Author: usha    11 Feb 2009Member Level: Silver   Points : 1
    hi!

    i download ur code from this article.........

    it is working fine now. thanks a lot....

    one small question.. i want to type the To address in a TextBox and send the mail to that particular person.. how to use it

    thanks and regards
    usha



    Author: Miss Meetu Choudhary    11 Feb 2009Member Level: Diamond   Points : 1
    simply pass the value of the text property of the textbox in the parameters of sendmail function

    happy codding if any other query feel free to ask



    Author: usha    11 Feb 2009Member Level: Silver   Points : 1
    hi

    thanks a lot Mr. Meetu Choudhary..

    all works fine


    thanks
    usha


    Author: Miss Meetu Choudhary    11 Feb 2009Member Level: Diamond   Points : 1
    Dear Usha Ji
    You are most welcome

    but i would like to tell you one thing

    that

    I am Miss Meetu Choudhary

    not

    Mr. Meetu Choudhary

    any ways happy to help you

    :)


    Author: baiju    23 Mar 2009Member Level: Silver   Points : 2
    hi meetu your code is not working
    smtpClient.Host = "mail.server.com";
    smtpClient.Port = 25;
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

    what is mail.server.com , is it common for every machine,portno =25 is it also common. and "info@server.com", "password".
    pls help me baiju



    Author: Miss Meetu Choudhary    23 Mar 2009Member Level: Diamond   Points : 1
    hi baiju
    i have attached the working version of the code with the article

    and ya
    the host will be anything like
    smtp.gmail.com
    or any orther host through which u will send ur mails


    Author: yoshini    23 Mar 2009Member Level: Bronze   Points : 2
    im yoshini,

    My whole project is to detect documents in pigeonholes and notify the user when documents available using email.Only for 3 users. Im now doing the interfacing part. Im using the 8051 microcontroller connected using rs 232 to the pc..Im confuse about which type of interface and GUI i will be able to use and how to store the database of the users? are there any other methods and references for me to refer? please help me.. Any recommended website? I need to explore myself more... thanks!!!




    Author: nisar    18 Apr 2009Member Level: Gold   Points : 1
    hey meetu, i am search this code from many days, i am very to find it, but please guide me one thing,
    mail.tatiwalaje.com
    this mail service is free for ever? mean to say can i relay on this, can i use this mail service for my contact us page?



    Author: Miss Meetu Choudhary    19 Apr 2009Member Level: Diamond   Points : 2
    NO this (mail.tatiwalaje.com) name space is not free for ever actually this is my client's space i just put it for example and you can use your gmail account instead of this the port will be 587 and the server will be pop.gmail.com or smtp.gmail.com and one more line you have to add is the requiredssl connection and it should be true. for gmail


    ++
    Regards
    Meetu Choudhary


    Author: nisar    19 Apr 2009Member Level: Gold   Points : 2
    thanks meetu for quick reply, i am not very expert, so please can you change your code according to gmail, yahoo or hotmail, but i actually want to not give my own email and password, as in your application, we can send mail through any email, no need to give password, can i do this with gmail? or is there any free mail serviced that work like mail.tatiwalaje.com


    Author: Miss Meetu Choudhary    19 Apr 2009Member Level: Diamond   Points : 1
    i am affraid that any mail server will give you this kind of service but you can use the service of the domain you will register they will give you this service.



    Author: nisar    19 Apr 2009Member Level: Gold   Points : 2
    yes my company has their own domain, and my task is to make a module where user can email each other internally, i write a code for gmail but they tell me that every user is not related to gmail and everyone not want to give their own password, then i use your code and its work exactly what they want, thats why i ask from you about mail.tatiwalaje.com, ok i actually want from you to please guide me what i ask from my company,
    smtp server?
    UserName?
    Password?
    Its enough?


    Author: nisar    19 Apr 2009Member Level: Gold   Points : 1
    thanks meetu, i am using your code but i change the smtp server, and again its work fine, i am using my company's domain with my official email and password to test, and its work fine, sorry if i ask the question that have no meaning


    Author: Miss Meetu Choudhary    19 Apr 2009Member Level: Diamond   Points : 2
    No Problem i am happy to help You


    Author: bajibabusk    20 Apr 2009Member Level: Gold   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
    bajibabu


    Author: Miss Meetu Choudhary    21 Apr 2009Member Level: Diamond   Points : 1
    yes you can fetch the address from the database as yu get the value for any other variable.. just pass the valur to the to paramerter of the function and time taken will depend on the server load...



    Author: charmii    29 May 2009Member Level: Bronze   Points : 1
    hey hi to all ,,

    well i m making 1 news letter form..
    this code is very fine for manual sending .. bt wht if i want to send it in bulk users at particular time ( like every day on 5 PM ) with 1 html file attachment ? .. plz help me out ..


    thanks :)


    Author: charmii    29 May 2009Member Level: Bronze   Points : 1
    hey hi to all ,,

    well i m making 1 news letter form..
    this code is very fine for manual sending .. bt wht if i want to send it in bulk users at particular time ( like every day on 5 PM ) with 1 html file attachment ? .. plz help me out ..


    thanks :)


    Author: arun    02 Jun 2009Member Level: Bronze   Points : 2
    hi i am new to mailing concept ..i used the settings given below..
    //CODE

    smtpClient.Host = "smtp.gmail.com";
    smtpClient.Port = 465; // i also tried for port 25
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Credentials = new System.Net.NetworkCredential("mygmail_id@gmail.com", "mygmail_password");

    //CODE

    I got this error message "The operation has timed out"

    can u suggest me what is the solution for this error??
    do i need to change any settings??

    Thanks in advance..



    Author: Miss Meetu Choudhary    03 Jun 2009Member Level: Diamond   Points : 1
    Instead of

    smtpClient.Port = 465; // i also tried for port 25

    try

    smtpClient.Port = 587

    in dotnet 587 is the port we use for gmail


    Author: arun    03 Jun 2009Member Level: Bronze   Points : 1
    Thank you Meetu for ur quick reply, i got solution in port 587


    Author: Miss Meetu Choudhary    03 Jun 2009Member Level: Diamond   Points : 1
    You are most welcome arun.

    Thanks..



    Author: shakthydoss    29 Aug 2009Member Level: Bronze   Points : 2
    Hi miss meetu i tried ur code with little modification
    but i get error ..

    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;

    namespace sentmail
    {
    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("shakthydoss@gmail.com", "sakthida@gmail.com", "shakthydoss@gmail.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 = 487;
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Credentials = new System.Net.NetworkCredential("myid@gamil.com", "password");

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

    }
    }

    Error :- Failure sending mail.
    looking forward for ur kind reply....


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add 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!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use