After you are done with this goto the click event of the Send button and paste the below code.
protected void btnSendMail_Click(object sender, EventArgs e)
{
try
{
/* Create a new blank MailMessage with the from and to adreesses*/
MailMessage mailMessage = new MailMessage(txtSender.Text,txtReceiver.Text);
/*Checking the condition that the cc is empty or not if not then * include them */
if (txtCc.Text != null && txtCc.Text != string.Empty)
{
mailMessage.CC.Add(txtCc.Text);
}
/*Checking the condition that the Bcc is empty or not if not then * include them*/
if (txtBcc.Text != null && txtBcc.Text != string.Empty)
{
mailMessage.Bcc.Add(txtBcc.Text);
}
//Ading Subject to the Mail
mailMessage.Subject = txtSubject.Text;
//Adding the Mail Body
mailMessage.Body = txtBody.Text;
/* Set the properties of the MailMessage to the values on the form as per the mail is HTML formatted or plain text */
if (rblMailFormat.SelectedItem.Text =="Text")
mailMessage.IsBodyHtml = false;
else
mailMessage.IsBodyHtml = true;
/* We use the following variables to keep track ofattachments and after we can delete them */
string attach1 = null;
string attach2 = null;
string attach3 = null;
/*strFileName has a attachment file name for attachment process. */
string strFileName = null;
/* Bigining of Attachment1 process & Check the first open file dialog for a attachment */
if (inpAttachment1.PostedFile != null)
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = inpAttachment1.PostedFile;
/* Get size of the file */
int attachFileLength = attFile.ContentLength;
/* Make sure the size of the file is > 0 */
if (attachFileLength > 0)
{
/* Get the file name */
strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
/* Save the file on the server */
inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));
/* Create the email attachment with the uploaded file*/
Attachment attach = new
Attachment(Server.MapPath(strFileName));
/* Attach the newly created email attachment */
mailMessage.Attachments.Add(attach);
/* Store the attach filename so we can delete it later*/
attach1 = strFileName;
}
}
/* Attachment-2 Repeat previous step defiend above*/
if (inpAttachment2.PostedFile != null)
{
same for attachment 2
}
/* Attachment-3 Repeat previous steps step defiend above*/
if (inpAttachment3.PostedFile != null)
{
same for attachment 3
}
/* Set the SMTP server and send the email with attachment */
SmtpClient smtpClient = new SmtpClient();
// smtpClient.Host = emailServerInfo.MailServerIP;
//this will be the host in case of gamil and it varies from the service provider
smtpClient.Host = "smtp.gmail.com";
//smtpClient.Port = Convert.ToInt32 emailServerInfo.MailServerPortNumber);
//this will be the port in case of gamil for dotnet andit varies from the service provider
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName,emailServerInfo.MailServerPassword);
smtpClient.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
//this will be the true in case of gamil and it varies from the service provider
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
/* Delete the attachements if any */
try{
if (attach1 != null)
File.Delete(Server.MapPath(attach1));
if (attach2 != null)
File.Delete(Server.MapPath(attach2));
if (attach3 != null)
File.Delete(Server.MapPath(attach3));
}
catch{}
/* clear the controls */
txtSender.Text = string.Empty;
txtReceiver.Text = string.Empty;
txtCc.Text = string.Empty;
txtBcc.Text = string.Empty;
txtSubject.Text = string.Empty;
txtBody.Text = string.Empty;
txtUserName.Text = string.Empty;
/* Dispaly a confirmation message to the user. */
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Black;
lblMessage.Text = "Message sent.";
}
catch (Exception ex)
{
/* Print a message informing the user about the exception that was risen */
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Red;
lblMessage.Text = ex.ToString();
}
}
--------------------------------------------------------------
---------------------------------------------------------------
Write a code for Fibonacci Series
using System;
using System.Collections.Generic;
using System.Text;
namespace Fibbonaci
{
class Program
{
static void Main(string[] args)
{
int oldnum, newnum, fibnum, maxnum;
Console.Write("Enter Any Value");
maxnum = int.Parse(Console.ReadLine());
oldnum = 0;
newnum = 1;
fibnum = oldnum + newnum;
Console.WriteLine(oldnum);
Console.WriteLine(newnum);
Console.WriteLine(fibnum);
for (; ; )
{
oldnum = newnum;
newnum = fibnum;
fibnum = oldnum + newnum;
if (fibnum > maxnum)
{
Console.WriteLine("");
break;
}
Console.WriteLine(fibnum);
}
Console.ReadLine();
}
}
}
==============================
FIND THE O/P OF THIS
int i = 1;
int j = 1;
Console.WriteLine(i==j); TRUE
Console.WriteLine(i.ToString() == j.ToString()); TRUE
Console.WriteLine((object)i == (object)j); FALSE
==============================
Calculating an Age in C# From a Date of Birth
public static int CalculateAge(DateTime birthdate)
{
// get the difference in years
int years = DateTime.Now.Year - birthdate.Year;
// subtract another year if we're before the
// birth day in the current year
if (DateTime.Now.Month < birthdate.Month (DateTime.Now.Month == birthdate.Month && DateTime.Now.Day < birthdate.Day))
years--;
return years;
}
=====================================
WAP to Upload file in Asp.Net
protected void cmdUploadFile_Click(object sender, EventArgs e)
{
strfileDirectory=”C:\Saini”;
//determine file to be uploaded
string strFileName = System.IO.Path.GetFileName(FileToUpload.PostedFile.FileName);
if((FileToUpload.PostedFile != null) && (FileToUpload.PostedFile.ContentLength>0))
{
try
{
//save the file to server
FileToUpload.PostedFile.SaveAs(strfileDirectory+strFileName);
uploadLblMeg.Visible=true;
uploadLblMeg.Text="File"+strfileDirectory+strFileName+"Uploaded Successfully";
}
catch(Exception ex)
{
uploadLblMeg.Visible=true;
uploadLblMeg.Text= ex.Message;
}
}
else
{
uploadLblMeg.Text="Select a file to Upload. Please try again !";
}
Syntax of Operator Overloading with Example?
Public struct HoursWorked
{
Float regularHours;
Float OvertimeHours;
}
//CREATING OVERLOADING OPERATOR
Public static HoursWorked operator +(HoursWorked a, HoursWorked b)
{
HoursWorked Result = new HoursWorked();
Result.RegularHours= a.RegularHours + b.RegularHours;
Result.OvertimeHours=a.OvertiemHours+b.OvertimeHours;
Return Result;
}
No comments:
Post a Comment