dotnetspider.com


 


TutorialsForumResourcesReviewsJobsInterviewVideosCommunitiesProjectsTraining

Subscribe to Subscribers


Online MembersSankar
Sunitha
cloud
Rajalingam
SivaSaiKrishna
Ashokkumar
er.g.ilango
Anu George
Krishna Kant
parameswaran
More...




Resources » Code Snippets » Email


Sending Email Through ASP.NET using C#


Posted Date:     Category: Email    Rating: 4 out of 54 out of 54 out of 54 out of 5
Author: Member Level: Gold    Points: 35 (Rs 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)
    Reference http://aspnetbymeetu.blogspot.com/2009/03/sending-email-through-aspnet-using-c.html


    Did you like this resource? Share it with your friends and show your love!





  • Responses to "Sending Email Through ASP.NET using C#"
    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: 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: Miss 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: Miss 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: Miss 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: 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: Gold   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: Gold   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: Gold   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: Gold   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: Gold   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: Gold   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: Gold   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: Gold   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: Gold   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: Gold   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....



    Author: pankaj singh    09 Jan 2010Member Level: Bronze   Points : 2
    hello neetu

    i have tried ur code but it give message;


    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: The name 'smtpClient' does not exist in the current context

    Source Error:



    Line 42: message.IsBodyHtml = true;
    Line 43: message.Body = body;
    Line 44: smtpClient.Host = "mail.gmail.com";
    Line 45: smtpClient.Port = 587;
    Line 46: smtpClient.UseDefaultCredentials = true;


    Source File: c:\Documents and Settings\pankaj\Desktop\mysite\enquiry.aspx.cs Line: 44




    the actual code is:-
    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.Net.Mail;

    public partial class enquery : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    string f = TextBox1.Text;
    string s = TextBox2.Text;
    string b = TextBox3.Text;

    Response.Write(SendMail(f,s,b) );

    }
    public string SendMail( string from, string subject, string body)
    {
    MailMessage message = new MailMessage();
    SmtpClient sc = new SmtpClient();
    string msg = string.Empty;
    try
    {
    MailAddress fromAddress = new MailAddress(from);
    message.From = fromAddress;
    message.To.Add("exesolutionsinfo@gmail.com");

    message.Subject = subject;
    message.IsBodyHtml = true;
    message.Body = body;
    smtpClient.Host = "mail.gmail.com";
    smtpClient.Port = 587;
    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;
    }
    }
    plzzzzzzzzzz help me out?
    pankaj Singh



    Author: Manindra Kumar Upadhyay    02 Apr 2010Member Level: Gold   Points : 1
    try this link:
    http://manindra.net/post/2010/03/29/Send-email-from-Aspnet.aspx




    Author: Shivraj Singh Rajawat    26 Jul 2010Member Level: Silver   Points : 1
    hello mam,

    This is indeed nice code for sending email. Can u tell me about specifying username & password. Is it sender email id's username, password or hosting panel's username & password.

    thanks & regards


    Shiv



    Author: Er. Ram Singh    06 Sep 2010Member Level: Gold   Points : 2
    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; }}


    Author: Venkatesan Prabu .J    08 Sep 2010Member Level: Gold   Points : 1

    Thats a very big communication.. :-)

    Cheers,
    Venkatesan Prabu .J
    http://venkattechnicalblog.blogspot.com/



    Author: raja paturu    10 Sep 2010Member Level: Gold   Points : 2
    Hello meetu madam,

    i have one content and nearly 4000 mail id's, and want to send the content to all the 4000 mail id's , instead of sending one by one mail.

    At this point of time, can i use the above mentioned code?

    in the above code,
    can i modify in this block?
    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"));
    }

    ------


    protected void Button1_Click(object sender, EventArgs e)
    {
    //Calling the function SendMail
    Response.Write( SendMail("mail1","mail2","mail3"..."mail4000","Test Mail","Test Mail Body"));
    }

    i need to add all the 4000 mail ids in button1_click block ?


    thanks 'n' regards
    raja



    Author: vidhya    19 Nov 2010Member Level: Bronze   Points : 1
    hi,

    im getting follwing error
    The remote name could not be resolved: 'smpt.gmail.com'

    pls tel me solution for this.

    With Regards,
    Vidhya.C



    Author: Charles    24 Mar 2011Member Level: Bronze   Points : 1
    Hello Meetu Madam,
    I have tried your code on yahoo.com but I get error message - message sending failed. May I please request you help me the mail over yahoo.
    In fact I have a datagrid containing names and email addresses of people. I want to know:
    (1) How can I retrieve name and address of members selected from the datagrid (datagrid has a checkbox column to select chosen members)
    (2) How can I retrieve different email domains form each address and send them all a generic mail message?
    Madam, I am quite novice and I will heartily appreciate your help.

    Regards
    Charles



    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to send Mail using VB.NET
    Previous Resource: Sending Email Through ASP.NET using VB
    Return to Resources
    Post New Resource
    Category: Email


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Email  .  Email using asp.net  .  Sending Email  .  Sending mail through asp.net  .  Sending mail  .  

    My Profile

    Active Members
    TodayLast 7 Daysmore...


    Awards & Gifts


    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds



    About Us    Trademark Disclaimer    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.