Subscribe For Free Updates!

We'll not spam mate! We promise.

Apr 28, 2012

Sending Emails In ASP.NET

Views:

http://www.howtoasp.net/how-to-asp-net-uploads/2011/06/sendmailapsnet.png
TO day i am show you how to send email in asp.net with out any email hosting or smtp server.How ? .We can send email by using over personal email account like (Hotmail,g mail,yahoo mail) in asp.net all we have required a server port number , id and password . Demo Project is Availible you can download it but please note you have to provide your on information(email id and Password) in web.config file .

Following Some Code Demonstration

the email using the SmtpClient class. The namespace System.Net.Mail contains classes which take care of constructing an SMTP-based message. The System.Net.Mail.MailMessages class encapsulated constructing a SMPT-based message, and System.Net.Mail.SmtpClient class provides the mechanism for sending the message to an SMTP server.
using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendbtn_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
/*
MailAddress fromAddress = new MailAddress(tb_email.Text, tb_name.Text);
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.mail.yahoo.com";
//Default port will be 25
smtpClient.Port = 587;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("zainnedian@yahoo.com");
message.Subject = "Contact us ";
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1@yoursite.com")
//message.CC.Add("admin1@yoursite.com");
//message.CC.Add("admin2@yoursite.com");
// You can specify Address directly as string
//message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
//message.Bcc.Add(new MailAddress("admin4@yoursite.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = tb_emailbody.Text;
// Send SMTP mail
smtpClient.Send(message);*/
var mailMessage = new System.Net.Mail.MailMessage();
mailMessage.To.Add("zainnedian@yahoo.com");
mailMessage.CC.Add("ssajjadashraf@yahoo.com");
mailMessage.Subject = "FeedBack Contact From User"+tb_email.Text;
mailMessage.Body = "User name :"+tb_name.Text+"\n\n Mesasge :"+tb_emailbody.Text;
var smtpClient1 = new SmtpClient();
smtpClient1.EnableSsl = true;
smtpClient1.Send(mailMessage);
lb_status.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lb_status.Text = "Send Email Failed." + ex.Message;
}
}
}
WEB.CONFIG FILE CODE
EMAIL Sending Code in Aps.net

Download Source Code

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment

Become a Fan

visual studio learn