Friday, 14 July 2017

Additional Resources Helper

Additional Resources Helper : In C# we can have class for addition resource help like for sending mail, to get extension of files etc. We can put the functionality or additional resource helper can and use it whenever required in order save time and amount of code type instead of writing these code again and again.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.IO;

/// <summary>
/// Summary description for Class_AdditionalResourcesHelper
/// </summary>
public class Class_AdditionalResourcesHelper
{
    public Class_AdditionalResourcesHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static void Sendmail(string Toaddrss, string subject, string body, string Displayname, string rplyto)
    {
        MailAddress fromaddress = new MailAddress("mail@gmail.com", Displayname);
        MailAddress Toaddress = new MailAddress(Toaddrss);
        MailMessage mailmessage = new MailMessage(fromaddress, Toaddress);
        mailmessage.ReplyToList.Add(rplyto);
        mailmessage.Body = body;
        mailmessage.Subject = subject;
        mailmessage.IsBodyHtml = true;
        SmtpClient smtpmail = new SmtpClient("smtp.gmail.com", 587);
        smtpmail.EnableSsl = true;

        NetworkCredential credential = new NetworkCredential("mail@gmail.com", "UserPassword");
        smtpmail.Credentials = credential;
        smtpmail.Send(mailmessage);
    }

    public static void SendAttachmentmail(string toadd, string subject, string body, string Displayname, string FilePath)
    {
        MailAddress fromaddress = new MailAddress("mail@gmail.com", Displayname);
        MailAddress Toaddress = new MailAddress(toadd);
        MailMessage mailmessage = new MailMessage(fromaddress, Toaddress);
        mailmessage.Body = body;
        mailmessage.Subject = subject;
        mailmessage.IsBodyHtml = true;

        if (File.Exists(FilePath))
        {
            Attachment at = new Attachment(FilePath);

            mailmessage.Attachments.Add(at);
        }

        SmtpClient smtpmail = new SmtpClient("smtp.gmail.com", 587);
        smtpmail.EnableSsl = true;

        NetworkCredential credential = new NetworkCredential("mail@gmail.com", "UserPassword");
        smtpmail.Credentials = credential;
        smtpmail.Send(mailmessage);
    }

    public static void Sendmail2(string toadd, string subject, string body, string Displayname)
    {
        try
        {
            MailAddress fromaddress = new MailAddress("mail@gmail.com", Displayname);
            MailAddress Toaddress = new MailAddress(toadd);
            MailMessage mailmessage = new MailMessage(fromaddress, Toaddress);
            mailmessage.Body = body;
            mailmessage.Subject = subject;
            mailmessage.IsBodyHtml = true;
            SmtpClient smtpmail = new SmtpClient("smtp.gmail.com", 587);
            smtpmail.EnableSsl = true;

            NetworkCredential credential = new NetworkCredential("mail@gmail.com", "UserPassword");
            smtpmail.Credentials = credential;
            smtpmail.Send(mailmessage);
        }
        catch (Exception ex)
        {
        }

    }
    public static void Sendmail1(string toadd, string subject, string body, string Displayname)
    {
        MailAddress fromaddress = new MailAddress("mail@gmail.com", Displayname);
        MailAddress Toaddress = new MailAddress(toadd);
        MailMessage mailmessage = new MailMessage(fromaddress, Toaddress);
        mailmessage.Body = body;
        mailmessage.Subject = subject;
        mailmessage.IsBodyHtml = true;
        SmtpClient smtpmail = new SmtpClient("smtp.gmail.com", 587);
        smtpmail.EnableSsl = true;

        NetworkCredential credential = new NetworkCredential("mail@gmail.com", "UserPassword");
        smtpmail.Credentials = credential;
        smtpmail.Send(mailmessage);
    }

    public static void SendmailWithCC(string toadd, string subject, string body, string Displayname, string cc)
    {
        MailAddress fromaddress = new MailAddress("mail@gmail.com", Displayname);
        MailAddress Toaddress = new MailAddress(toadd);
        MailAddress SendCC = new MailAddress(cc);
        MailMessage mailmessage = new MailMessage(fromaddress, Toaddress);
        mailmessage.CC.Add(SendCC);
        mailmessage.Body = body;
        mailmessage.Subject = subject;
        mailmessage.IsBodyHtml = true;
        SmtpClient smtpmail = new SmtpClient("smtp.gmail.com", 587);
        smtpmail.EnableSsl = true;
        NetworkCredential credential = new NetworkCredential("mail@gmail.com", "UserPassword");
        smtpmail.Credentials = credential;
        smtpmail.Send(mailmessage);
    }

    public static void Sendmail2CC(string Toaddrss, string subject, string body, string Displayname, string rplyto)
    {
        MailAddress fromaddress = new MailAddress("mail@gmail.com", Displayname);
        MailAddress Toaddress = new MailAddress(Toaddrss);
        MailAddress SendCC = new MailAddress("abc@gmail.com");
        MailAddress SendCC2 = new MailAddress("xyz@gmail.com");
        MailMessage mailmessage = new MailMessage(fromaddress, Toaddress);
        mailmessage.CC.Add(SendCC);
        mailmessage.CC.Add(SendCC2);
        mailmessage.ReplyToList.Add(rplyto);
        mailmessage.Body = body;
        mailmessage.Subject = subject;
        mailmessage.IsBodyHtml = true;
        SmtpClient smtpmail = new SmtpClient("smtp.gmail.com", 587);
        smtpmail.EnableSsl = true;

        NetworkCredential credential = new NetworkCredential("mail@gmail.com", "UserPassword");
        smtpmail.Credentials = credential;
        smtpmail.Send(mailmessage);
    }

    public static string Getextention(string Filename)
    {
        string _extention;
        int index;
        index = Filename.IndexOf('.');
        _extention = Filename.Substring(index);
        return _extention;
    }  
}

No comments:

Post a Comment