Tips and tricks for .NET using ASP and VB code.

Send an email

    Private Sub SendEmail(ByVal FromEmailAddressText As String, ByVal ToEmailAddressesText() As String, ByVal SubjectText As String, ByVal EmailBodyText As String, ByVal EmailServerNameText As String, Optional ByVal BccEmailAddressesText() As String = Nothing)
        Dim EmailMailMessage As New Net.Mail.MailMessage()
        EmailMailMessage.From = New Net.Mail.MailAddress(FromEmailAddressText)
        For Each ToEmailAddressText As String In ToEmailAddressesText
            EmailMailMessage.To.Add(ToEmailAddressText)
        Next ToEmailAddressText
        If BccEmailAddressesText IsNot Nothing Then
            For Each BccEmailAddressText In BccEmailAddressesText
                EmailMailMessage.Bcc.Add(BccEmailAddressText)
            Next BccEmailAddressText
        End If
        EmailMailMessage.Subject = SubjectText
        EmailMailMessage.Body = EmailBodyText
        Dim EmailSmtpClient As New Net.Mail.SmtpClient(EmailServerNameText)
        EmailSmtpClient.UseDefaultCredentials = False
        EmailSmtpClient.Credentials = New System.Net.NetworkCredential(System.Net.WebUtility.HtmlDecode("Username"), System.Net.WebUtility.HtmlDecode("Password"))
        EmailSmtpClient.Send(EmailMailMessage)
    End Sub

No comments:

Post a Comment

Search This Blog