RSS

Email send through vb.net on localhost or web

22 Mar

 

First Write server setting in webconfig

  <!–Start SMTP Settings –>
    <add key=”ServerName” value=”127.0.0.1″/>
    <add key=”smtpUser” value=”admin@sf.com”/>
    <add key=”smtpPassword” value=”123″/>
    <add key=”smtpPort” value=”25″ />
    <!–End SMTP Settings –>
    <add key=”From” value=”Test@yahoo.com”/>
    <!– Start Form ToEmail –>
    <!– Start Form ToEmail –>

 <add key=”email_To” value=”test@yahoo.com “/>
    <add key=”email_CC” value=”test@gmail.cm”/>
    <add key=”email_BCC” value=”test@gmail.com”/>
    <add key=”email_Subject” value=”Contact Us Form from the test Website”/>

For localhost you want to see the e-mail for testing porposes you create Email folder in any drive just write this code in webconfig

<system.net>
  <mailSettings>
   
      <smtp deliveryMethod=”SpecifiedPickupDirectory”>

        <specifiedPickupDirectory pickupDirectoryLocation=”d:\Emals\”/>
    </smtp>
  </mailSettings>
  </system.net>
write this code on button click event

 SendEmail(AppSettings(“email_To”), AppSettings(“email_CC”), AppSettings(“email_BCC”), AppSettings(“email_Subject”), sbMessage.ToString(), AppSettings(“from”))

Create a function in vb.net

 Public Sub SendEmail(ByVal EmailTo As String, ByVal EmailCC As String, ByVal EmailBCC As String, ByVal EmailSubject As String, ByVal EmailBody As String, ByVal EmailFrom As String)
        Dim smtpClient As New SmtpClient()
        Try
            If AppSettings(“ServerName”) <> “” Then
                smtpClient.Host = AppSettings(“ServerName”)
                If (AppSettings(“ServerName”).Length > 0) Then
                    smtpClient.Port = AppSettings(“smtpPort”)
                End If
                If (AppSettings(“smtpUser”).Length > 0) Then
                    smtpClient.Credentials = New NetworkCredential(AppSettings(“smtpUser”), AppSettings(“smtpPassword”))
                End If
                Dim emailMessage As New MailMessage()
                emailMessage.To.Add(New MailAddress(EmailTo))
                If EmailCC <> “” Then
                    emailMessage.CC.Add(New MailAddress(EmailCC))
                End If
                If EmailBCC <> “” Then
                    emailMessage.Bcc.Add(New MailAddress(EmailBCC))
                End If
                ‘smtpClient.DeliveryMethod = smtpClient.PickupDirectoryLocation

                emailMessage.From = New MailAddress(EmailFrom)
                emailMessage.Body = EmailBody
                emailMessage.Subject = EmailSubject
                emailMessage.IsBodyHtml = True
                emailMessage.Priority = MailPriority.Normal
                smtpClient.Send(emailMessage)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Advertisements
 
1 Comment

Posted by on March 22, 2011 in ASP Dot Net C#

 

One response to “Email send through vb.net on localhost or web

  1. paul

    February 1, 2014 at 6:24 pm

    Great!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: