ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Create common class for sending Email using C# and VB.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Refer below sample code.</p> <p>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.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System; using System.Web; using System.Net.Mail; using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System Imports System.Web Imports System.Net.Mail Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><span style="text-decoration: underline;"><strong>C#</strong></span></p> <p><strong>CS</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { string path = Server.MapPath(&#34;~/email/PasswordRecovery.html&#34;); string emailid = &#34;&#34;; string bcs = &#34;&#34;; Emails email = new Emails(); email.Send(emailid, path, bcs); }</pre> <p><strong>Emails.cs</strong></p> <pre class="brush: csharp">public class Emails { public void Send(string email, string path, string bcs) { string SMTP = &#34;&#34;; string BCC = &#34;&#34;; string EmailFrom = &#34;&#34;; string Password = &#34;&#34;; 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 = &#34;&#34;; myString = readFile; myString = myString.Replace(&#34;{#memberid}&#34;, id.ToString()); myString = myString.Replace(&#34;{#URL}&#34;, host.ToString()); mail.Subject = &#34;Recover Password&#34; + &#34; &#34; + DateTime.Now.ToString(&#34;dd/MMM/yyyy hh:mm:ss tt&#34;); 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); } }</pre> <p><span style="text-decoration: underline;"><strong>VB.Net</strong></span></p> <p><strong>VB</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim path As String = Server.MapPath(&#34;~/email/PasswordRecovery.html&#34;) Dim emailid As String = &#34;&#34; Dim bcs As String = &#34;&#34; Dim email As Emails = New Emails() email.Send(emailid, path, bcs) End Sub</pre> <p><strong>Emails.vb</strong></p> <pre class="brush: vb">Public Class Emails Public Sub Send(ByVal email As String, ByVal path As String, ByVal bcs As String) Dim SMTP As String = &#34;&#34; Dim BCC As String = &#34;&#34; Dim EmailFrom As String = &#34;&#34; Dim Password As String = &#34;&#34; 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 = &#34;&#34; myString = readFile myString = myString.Replace(&#34;{#memberid}&#34;, id.ToString()) myString = myString.Replace(&#34;{#URL}&#34;, host.ToString()) mail.Subject = &#34;Recover Password&#34; &amp; &#34; &#34; &amp; DateTime.Now.ToString(&#34;dd/MMM/yyyy hh:mm:ss tt&#34;) 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</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/967482/Create-common-class-for-sending-Email-using-C-and-VBNet/https://www.aspforums.net:443/Threads/967482/Create-common-class-for-sending-Email-using-C-and-VBNet/Wed, 17 Apr 2019 05:21:03 GMT