.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Auto email with smtpport validation

Posted By:Athira Sreejith       Posted Date: January 02, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

code in Asp.net ,C#.

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;
using System.Threading;

public partial class SendMail : System.Web.UI.Page
{
public bool x;
public static string tomailfromPrevPg;
string[] toName;
string[] CcName;
string[] BCcName;
public static int NoOftimes=0;
public static int varTimeInterval=0;
public static int ji = 0;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tomailfromPrevPg = Request.QueryString.Get("tonm");
txtEmail.Text = tomailfromPrevPg;
}
}



protected void btnSendmail_Click(object sender, EventArgs e)//Button click
{
lblStatus.Text = "";
calculateTimeInterval(); //method Name
step1: x = ValidateMail(); //Method Name
if (x == false)
{
goto endline1;
}
// 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("fromaddr@webnm.com", txtName.Text);

// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtpout.secureserver.net"; //outgoing server address
smtpClient.Port = 25;

//From address will be given as a MailAddress Object
message.From = fromAddress ;

toName = txtEmail.Text.Split(',');
// To address collection of MailAddress
for (int i = 0; i < toName.Length; i++)
{
message.To.Add(toName[i]);
}
if (txtSubject.Text != "")
{
message.Subject = txtSubject.Text.Trim();
}
else
{
message.Subject = "[None]";
}

// 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")
if (txtCC.Text.Trim() != "")
{
CcName = txtCC.Text.Split(',');
for (int i = 0; i < CcName.Length; i++)
{
message.CC.Add(CcName[i]);
}
}


// You can specify Address directly as string
if (txtBCc.Text.Trim() != "")
{
BCcName = txtBCc.Text.Split(',');
for (int i = 0; i < BCcName.Length; i++)
{
message.Bcc.Add(new MailAddress(BCcName[i]));
}
}


//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;

// Message body content
message.Body = txtMessage.Text;
for (int i = 0; i < cmbAttach.Items.Count; i++) //combobox cmbattach
{
message.Attachments.Add(new Attachment(cmbAttach.Items[i].Text));
}


smtpClient.UseDefaultCredentials = false;

System.Net.NetworkCredential theCredential
= new System.Net.NetworkCredential("usernm", "pwd");
smtpClient.Credentials = theCredential;
// Send SMTP mail
smtpClient.Send(message);




for ( ji=NoOftimes -1; ji >0 ; ji--)
{



Thread.Sleep(varTimeInterval);
NoOftimes = NoOftimes - 1;
//txtNoOfTimes.Text = Convert.ToString( NoOftimes);


goto step1;
}
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed." + ex.Message;
}

endline1: int nouse;
}
public bool ValidateMail()
{
if ((txtNos.Text.Trim() == "") || (txtNoOfTimes.Text.Trim() == "") || (txtName.Text == "") || (txtEmail.Text == "") || (txtMessage.Text == "") || (txtSubject.Text == ""))
{
if (txtSubject.Text.Trim() == "")
{
txtSubject.Text = "[none]";
return true;
}
lblErr.Text = "All fields are compulsory";
return false;
}
return true;
}
protected void btnAttch_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFile )
{
cmbAttach.Items.Add(FileUpload1.PostedFile.FileName );
}

}

protected void btnRemove_Click(object sender, EventArgs e)
{
if (cmbAttach.Items.Count > 0)
{
cmbAttach.Items.Remove(cmbAttach.SelectedItem.Text);

}
}
public void calculateTimeInterval()
{
NoOftimes = Convert.ToInt32(txtNoOfTimes.Text.Trim());
varTimeInterval = Convert.ToInt32(txtNos.Text.Trim());
if (ddltimeInterval.SelectedItem.Text.ToLower() == "second")
{
varTimeInterval = varTimeInterval * 1000;

}
else if (ddltimeInterval.SelectedItem.Text.ToLower() == "minute")
{
varTimeInterval = varTimeInterval * 60 * 1000;
}
else if (ddltimeInterval.SelectedItem.Text.ToLower() == "hour")
{
varTimeInterval = varTimeInterval * 3600 * 1000;
}
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend