Create common class for sending Email using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

i have this email code which is working fine. 

i want to creat its class file, so i do not need to repeat the code on every page 

important points are 

1. i can pass email address as parameter

2. i can pass myString one or more bcs email sending variable can be one or more or zero

3. i can send file path if any require in email 

please advice

            string SMTP = "";
            string BCC = "";
            string EmailFrom = "";
            string Password = "";
            //Ports = (int)dview.Table.Rows[0]["Port"];

            string host = HttpContext.Current.Request.Url.Host;
            // localhost
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(SMTP.ToString());
            mail.From = new MailAddress(EmailFrom.ToString());
            //Email acpe = new Email();
            mail.To.Add(email);
            mail.Bcc.Add(BCC.ToString());
       
            StreamReader reader = new StreamReader(Server.MapPath("~/email/PasswordRecovery.html"));
            string readFile = reader.ReadToEnd();
            string myString = "";
            myString = readFile;

            //these mystring can one or more may be not any one
            myString = myString.Replace("{#memberid}", id.ToString());
            myString = myString.Replace("{#URL}", host.ToString());

            mail.Subject = "Recover Password" + " " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt");
            mail.IsBodyHtml = true;
            mail.Body = myString.ToString();
            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential(EmailFrom.ToString(), Password.ToString());
            SmtpServer.EnableSsl = false;
            object userState = mail;
            SmtpServer.SendCompleted += new SendCompletedEventHandler(SMTPClientForAsy.SmtpClient_OnCompleted);
            SmtpServer.SendAsync(mail, userState);

 

You are viewing reply posted by: pandeyism 5 months ago.
Posted 5 months ago

Hi nauna,

Refer below sample code.

Create a separate class make a parameterized method pass that what are you want to pass , Take reference to below code change accordingly to you.

Namespaces

C#

using System;
using System.Web;
using System.Net.Mail;
using System.IO;

VB.Net

Imports System
Imports System.Web
Imports System.Net.Mail
Imports System.IO

Code

C#

CS

protected void Page_Load(object sender, EventArgs e)
{
    string path = Server.MapPath("~/email/PasswordRecovery.html");
    string emailid = "";
    string bcs = "";
    Emails email = new Emails();
    email.Send(emailid, path, bcs);
}

Emails.cs

public class Emails
{
    public void Send(string email, string path, string bcs)
    {
        string SMTP = "";
        string BCC = "";
        string EmailFrom = "";
        string Password = "";
        string host = HttpContext.Current.Request.Url.Host;
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient(SMTP.ToString());
        mail.From = new MailAddress(EmailFrom.ToString());
        mail.To.Add(email);
        mail.Bcc.Add(BCC.ToString());
        StreamReader reader = new StreamReader(path);
        string readFile = reader.ReadToEnd();
        string myString = "";
        myString = readFile;
        myString = myString.Replace("{#memberid}", id.ToString());
        myString = myString.Replace("{#URL}", host.ToString());
        mail.Subject = "Recover Password" + " " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt");
        mail.IsBodyHtml = true;
        mail.Body = myString.ToString();
        SmtpServer.Port = 25;
        SmtpServer.Credentials = new System.Net.NetworkCredential(EmailFrom.ToString(), Password.ToString());
        SmtpServer.EnableSsl = false;
        object userState = mail;
        SmtpServer.SendCompleted += new SendCompletedEventHandler(SMTPClientForAsy.SmtpClient_OnCompleted);
        SmtpServer.SendAsync(mail, userState);
    }
}

VB.Net

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim path As String = Server.MapPath("~/email/PasswordRecovery.html")
    Dim emailid As String = ""
    Dim bcs As String = ""
    Dim email As Emails = New Emails()
    email.Send(emailid, path, bcs)
End Sub

Emails.vb

Public Class Emails
    Public Sub Send(ByVal email As String, ByVal path As String, ByVal bcs As String)
        Dim SMTP As String = ""
        Dim BCC As String = ""
        Dim EmailFrom As String = ""
        Dim Password As String = ""
        Dim host As String = HttpContext.Current.Request.Url.Host
        Dim mail As MailMessage = New MailMessage()
        Dim SmtpServer As SmtpClient = New SmtpClient(SMTP.ToString())
        mail.From = New MailAddress(EmailFrom.ToString())
        mail.[To].Add(email)
        mail.Bcc.Add(BCC.ToString())
        Dim reader As StreamReader = New StreamReader(path)
        Dim readFile As String = reader.ReadToEnd()
        Dim myString As String = ""
        myString = readFile
        myString = myString.Replace("{#memberid}", id.ToString())
        myString = myString.Replace("{#URL}", host.ToString())
        mail.Subject = "Recover Password" & " " & DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt")
        mail.IsBodyHtml = True
        mail.Body = myString.ToString()
        SmtpServer.Port = 25
        SmtpServer.Credentials = New System.Net.NetworkCredential(EmailFrom.ToString(), Password.ToString())
        SmtpServer.EnableSsl = False
        Dim userState As Object = mail
        SmtpServer.SendCompleted += New SendCompletedEventHandler(SMTPClientForAsy.SmtpClient_OnCompleted)
        SmtpServer.SendAsync(mail, userState)
    End Sub
End Class