C# .NET - How to send Email using asp.net - Asked By abinav shankar on 16-Jan-12 08:55 AM

Hi

I want to send Email I want to know how to get the Smtp of my system and how to send mail with the body as follows

Name  from  To Number of Days  Status

Thanks
Abinav Shankar

smr replied to abinav shankar on 16-Jan-12 09:05 AM
hi

To configure SMTP configuration data for ASP.NET, you would add the following tags to your web.config file.
 
 <system.net>
    <mailSettings>
    <smtp from="abc@somedomain.com">
      <network host="somesmtpserverport="25userName="namepassword="passdefaultCredentials="true" />
    </smtp>
    </mailSettings>
 </system.net>


protected void btnSendmail_Click(object sender, EventArgs e)
    {
    // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
 
    // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
 
    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();
 
    try
    {
      MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
 
      // You can specify the host name or ipaddress of your server
 
      // Default in IIS will be localhost
 
      smtpClient.Host = "localhost";
 
      //Default port will be 25
 
      smtpClient.Port = 25;
 
      //From address will be given as a MailAddress Object
 
      message.From = fromAddress;
 
      // To address collection of MailAddress
 
      message.To.Add("admin1@yoursite.com");
      message.Subject = "Feedback";
 
      // 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 = txtMessage.Text;
      
      // Send SMTP mail
 
      smtpClient.Send(message);
 
      lblStatus.Text = "Email successfully sent.";
    }
    catch (Exception ex)
    {
      lblStatus.Text = "Send Email Failed." + ex.Message;
    }
    }


follow
http://www.dotnetcurry.com/ShowArticle.aspx?ID=65
dipa ahuja replied to abinav shankar on 16-Jan-12 09:26 AM
Step 1 : Add this in Web.config
    
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
  port="587"
  userName=""
  password=""
</smtp>
</mailSettings>
</system.net>
  
Step 2 : Mail Sending Code
 
using System.Net.Mail;
using System.Net;
 
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress = "xx@gmail.com";
  string GmailId = "abc@gmail.com";
  string bodyMsg = "helo its testing mail";
  string subject = "testing mail";
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = subject;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
    SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
  smtp.Send(mail);
}
 
 
kalpana aparnathi replied to abinav shankar on 16-Jan-12 12:14 PM
hi,

http://www.4guysfromrolla.com/articles/072606-1.aspx
Jitendra Faye replied to abinav shankar on 17-Jan-12 12:03 AM

You can send email by using System.Net.Mail Namspace.



Use this code-

protected
void SendEmail(object sender, EventArgs
e)

{

SmtpClient


smtpClient = new SmtpClient();

MailMessage


message = new MailMessage();

try

{

// Prepare two email addresses

MailAddress


fromAddress = new MailAddress("FromAddress", "DisplayName");

MailAddress


toAddress = new MailAddress("ToAddress");

// Prepare the mail message

message.From = fromAddress;

message.To.Add(toAddress);

message.Subject =

"Testing!";

message.Body =

"This is the body of a sample message";

smtpClient.EnableSsl =

true;

smtpClient.Port = 587;

smtpClient.Host =

"smtp.gmail.com";

smtpClient.Credentials =

new System.Net.NetworkCredential("UserName", "Password");

smtpClient.Send(message);

}

catch


(Exception ex)

{

statusLabel.Text =

"Coudn't send the message!<br />" + ex.Message;

}

}


Try this na dlet me know.

Suchit shah replied to abinav shankar on 17-Jan-12 12:24 AM
To send email with attachement do it like below

using System.Net.Mail;

using System.Net;

 

protected void btnSent_Click(object sender, EventArgs e)

{

   string toEmailAddress="xx@gmail.com";

   string GmailId="abc@gmail.com";

   string password="";

   string bodyMsg="helo its testing mail";

 

   MailMessage mail = new MailMessage();

   mail.To.Add(toEmailAddress);

 

   mail.From = new MailAddress(GmailId);

   mail.Subject = txtSubject.Text;

   mail.Body = bodyMsg;

   mail.IsBodyHtml = true;

 

   //Attach file using FileUpload Control
and put the file in memory stream

 

   if (FileUpload1.HasFile)

   {

   mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,
FileUpload1.FileName));

   }

 

   SmtpClient smtp = new SmtpClient("smtp.gmail.com");

   smtp.EnableSsl = true;

   smtp.UseDefaultCredentials = false;

   smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);

   smtp.Send(mail);

}