Send Mails Via .Net


Here is the code to send mails via .Net:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Threading;

/// <summary>
/// Summary description for Emailing
/// </summary>
public class Emailing
{
private const int SmtpPort = 25;
private const string SmtpServer = "__SMTP SERVER NAME___";
private const string UserName = "__ USER NAME ___";
private const string Password = "__ PASSWORD __";
private const bool EnableSsl = false; //For Gmail Server set it to true

/// <summary>    
/// Sends the mail    
/// </summary>    
/// <param name="toEmailAddresses">';' seperated To email addresses.</param>    
/// <param name="fromEmailAddress">From email address.</param>    
/// <param name="subject">The subject.</param>    
/// <param name="body">The body.</param>    
/// <param name="mailAttachments">';' seperated mail attachments.</param>    
/// <param name="isBodyHTML">is body HTML.</param>   
public static void SendMail(string toEmailAddresses, string fromEmailAddress, string subject,
string body, string mailAttachments, bool isBodyHTML)
{
char[] splitter = { ';' };
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromEmailAddress);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHTML;

//Adding Multiple To Addresses
string[] mailAddresses = toEmailAddresses.Split(splitter);
foreach (string mailAddress in mailAddresses)
{
if (mailAddress.Length != 0)
{
mailMessage.To.Add(new MailAddress(mailAddress));
}
}

//Adding Multiple Attachments
string[] attachments = mailAttachments.Split(splitter);
foreach (string attachment in attachments)
{
if (attachment.Length != 0)
{
Attachment attachFile = new Attachment(attachment);
mailMessage.Attachments.Add(attachFile);
}
}

SmtpClient smtpClient = new SmtpClient();
try
{
smtpClient.Host = SmtpServer;
smtpClient.EnableSsl = EnableSsl;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = UserName;
NetworkCred.Password = Password;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = NetworkCred;
smtpClient.Port = SmtpPort;
smtpClient.Send(mailMessage);
}
catch
{
mailMessage = null;
smtpClient = null;
}
}
/// <summary>    
/// Sends the mail on thread.    
/// </summary>    
/// <param name="toEmailAddresses">';' seperated To email addresses.</param>    
/// <param name="fromEmailAddress">From email address.</param>    
/// <param name="subject">The subject.</param>    
/// <param name="body">The body.</param>    
/// <param name="mailAttachments">';' seperated mail attachments.</param>    
/// <param name="isBodyHTML">is body HTML.</param>    
public static void SendMailOnThread(string toEmailAddresses, string fromEmailAddress, string subject,
string body, string mailAttachments, bool isBodyHTML)
{
Thread mailThread; mailThread = new System.Threading.Thread(delegate()
{
SendMail(toEmailAddresses, fromEmailAddress, subject, body, mailAttachments, isBodyHTML);
});
mailThread.IsBackground = true; mailThread.Start();

}

}


You can use this class like :-

//Send Simple Mail
Emailing.SendMail("sample@sample.com", "sample@sample.com", "Subject is test", "body is Test", "", true);

//Send mail on thread with two attachments
Emailing.SendMailOnThread("sample@sample.com", "sample@sample.com", "Subject is test", "body is Test", 
"c:\\setup.log;c:\\setup1.log;", true);

In case you want to send emails through Gmail server, then use these settings:-
private const int SmtpPort = 465;//if 465 do not work then try 587
private const string SmtpServer = "smtp.gmail.com";
private const string UserName = "__ USER NAME ___";
private const string Password = "__ PASSWORD __";
private const bool EnableSsl = true;

How to set Session Timeout in ASP.Net

Here are different places from where you can set timeouts. Check all these settings in your application:-

1- Web Config
You can define session time out in sessionstate tag of web.config file. Also if you are using forms authentication then also define session timeout in forms as mentioned below:-
<forms loginurl="sampleloginpage.aspx" name="samplecookie" timeout="45" path="/" requiressl="true" protection="All">
</forms>
<sessionstate mode="InProc" stateconnectionstring="tcpip=127.0.0.1:42424" cookieless="false" timeout="45">

2-Global.asax Session_Start Event
You can also set this in global.asax file as mentioned here:-
Session.Timeout = 60 ; // in Session.Start() event

3-sessionState
To set session timeout to 45 minutes write this in the web.config file :
<sessionstate mode="InProc" stateconnectionstring="tcpip=127.0.0.1:42424"
 sqlconnectionstring="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20">

The maximum limit for session timeout is 525,600 minutes(1 year) - (365 days x 24 hours x 60 min)

If all mentioned above did not help you then it might be due to anti virus software. Software scans the files in application folder and updates their date/time. Which causes IIS to restart. There fore i uninstalled the anti virus from server. However, if you exclude the application folder from virus scan that will also do the job. Also you need to remove .config,.aspx,.ascs and other extensions specific to your application for further security.

Welcome


Hi,

I am creating this blog to support .Net community. Input from all the guys is welcomed