How to send E-mail from .NET applications

The code sample is written in C# and intends to send mail from .NET applications. The code is implemented as a partial class in C# that extends a Page class. Thus making the code easily reusable.

The method that actually sends the e-mail is an event handler for a button. So, to use this code create a .aspx page with a button, textarea, and corresponding textboxes. Attach this handler to the button's click event.


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.Web.Mail;


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

protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
// Sending mail using Code..
MailMessage mm = new MailMessage();
try
{
mm.From = "gagrawal@del.aithent.com";
mm.To = txtTO.Text.ToString();
mm.Cc = txtCC.Text.ToString();
mm.Bcc = txtBCC.Text.ToString();
mm.Body = txtBody.Text.ToString();
mm.Subject = txtSubject.Text.ToString();
string filePath = fileupload1.PostedFile.FileName;
mm.Attachments.Add(new System.Web.Mail.MailAttachment(filePath,MailEncoding.UUEncode));
System.Web.Mail.SmtpMail.Send(mm);
lblMessage.Text = "Email successfully sent.";
}
catch(Exception ex)
{
lblMessage.Text = "Send Email Failed." + ex.Message;
}

}
}


Comments

Author: Junaid Soomro09 Nov 2008 Member Level: Bronze   Points : 0

Please Create "aspx" file and send complete in zip.
Thanks.

Author: Puneeth04 Mar 2010 Member Level: Bronze   Points : 1

Hi dude,
I am less experience about these.
Hope its not a silly question
But in wiv template do we create it ?
does a windows form application work for this code.

Please help
i am stuck from 2 days.
I am a beginner

Thanks in advance

Author: Manigandan04 Mar 2010 Member Level: Gold   Points : 1

We can also send this Mail using SMTPMailMessage.
But the basic difference is we don't have the CC and BCC Option in it.

Author: Er. Ram Singh29 Mar 2010 Member Level: Gold   Points : 2

MailMessage mm = new MailMessage();
try
{
mm.From = "gagrawal@del.aithent.com";
mm.To = txtTO.Text.ToString();
mm.Cc = txtCC.Text.ToString();
mm.Bcc = txtBCC.Text.ToString();
mm.Body = txtBody.Text.ToString();
mm.Subject = txtSubject.Text.ToString();
string filePath = fileupload1.PostedFile.FileName;
mm.Attachments.Add(new System.Web.Mail.MailAttachment(filePath,MailEncoding.UUEncode));
System.Web.Mail.SmtpMail.Send(mm);
lblMessage.Text = "Email successfully sent.";
}
catch(Exception ex)
{
lblMessage.Text = "Send Email Failed." + ex.Message;
}

Author: john14 Jun 2010 Member Level: Bronze   Points : 2

Pro: Upgrading Your Enterprise Support (70-621) 70-680 certification 000-377 exam is the one, if the need to obtain a MCTS certification, you may also need to attend other relevant examinations, 000-331 Certification, please visit the topic, where you will see all the relevant MCTS certification exam subjects. 70-663 certification is the certification authority, one has a pivotal position in the international. If you have a MCTS certification, you enter the IT industry coupled with the power company will a heavy a+ practice test weight. The certificate holders will enjoy worldwide free credit MCTS relevant professional treatment, some countries in North America as immigrants technical evaluation criteria. Oracle certification dumps Obtain any level of 70-662 certification means you are one of influential figure IT industry.

Author: Er. Ram Singh06 Sep 2010 Member 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; }}



  • 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:
    Email: