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