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;

Comments (16)

Loading... Logging you in...
  • Logged in as
I used the above code. but it is showing the error message "Failure Sending Mail" in Windows 2003 server
1 reply · active 688 weeks ago
Are you sending email thorugh gmail account? if yes then try changing the port 465 to 587. Hopefully it will solve the problem. Also check that your internet connection is working or not and no firewall is blocking 465/587 port
very very usefull code
1 reply · active 660 weeks ago
thanks for such very very usefull codesjavascript: hideMsgBox();
1 reply · active 660 weeks ago
Riazul Hasna Khan's avatar

Riazul Hasna Khan · 662 weeks ago

Zeeshan Umar Sahab, once again shukriya for this post. Further, can you please suggest me any such post MVC architecture too. It would be good for me if provided with a an example. I mean a small project using MVC architecture. :)
1 reply · active 660 weeks ago
Selection of article depends on your existing skill level on ASP.Net and MVC. However I think that this blog is really helpfull to understand basic and advance concepts of MVC:-
http://stephenwalther.com/archive/category/4
can you email me the zip working files sir.. c_zurc44@yahoo.com
and the html form also sir
How can i send an enquery on other mail. like info@abc .com. not on gmail
Hello dear,
I want to make inbox in c#, like Facebook massage box. Can i make the inbox by using above code, as you have listed.
Hi ... Report should be mailed every day automatically by using C# Complete Code
1 reply · active 402 weeks ago
You can create an exe with above code and schedule it daily through windows task scheduler.

Post a new comment

Comments by