ipeoples
ipeoples

Reputation: 73

Send email on page Load to multiple recipients

Server configuration is in webconfig file.

<%@ Page Language="VB" %>

<!DOCTYPE html>

<script runat="server">

   Partial Class _Default
    Inherits System.Web.UI.Page

    'Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Originally the code manage a windows form.

        'Create instance of main mail message class.
        Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

        'Configure mail mesage
        'Set the From address with user input
        '    mailMessage.From = New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim())
        'Get From address in web.config
        mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress"))
        'Another option is the "from" attirbute in the <smtp> element in the web.config.

        'Set additinal addresses
        'mailMessage.To.Add(New System.Net.Mail.MailAddress(txtToAddress.Text.Trim()))
        mailMessage.To.Add(New System.Net.Mail.MailAddress("[email protected]"))
        'mailMessage.CC
        'mailMessage.Bcc
        'mailMessage.ReplyTo

        'Set additional options
        mailMessage.Priority = Net.Mail.MailPriority.High
        'Text/HTML
        mailMessage.IsBodyHtml = False

        'Set the subjet and body text
        'mailMessage.Subject = txtSubject.Text.Trim()
        mailMessage.Subject = "test email SUBJECT"
        'mailMessage.Body = txtBody.Text.Trim()
        mailMessage.Body = "body of email test"

        'Add one to many attachments
        'mailMessage.Attachments.Add(New System.Net.Mail.Attachment("c:\temp.txt")

        'Create an instance of the SmtpClient class for sending the email
        Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

        'Use a Try/Catch block to trap sending errors
        'Especially useful when looping through multiple sends
        Try
            smtpClient.Send(mailMessage)
        Catch smtpExc As System.Net.Mail.SmtpException
            'Log error information on which email failed.
        Catch ex As Exception
            'Log general errors
        End Try

    End Sub

End Class


</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

I just want to send email to multiple recipients every time this page loads.

•Multiple Recipients •Don't know how to make the page send email on every page load

Upvotes: 0

Views: 257

Answers (1)

Louis van Tonder
Louis van Tonder

Reputation: 3710

There is a page_load method on the page, just as there is a form_load method in a Windows Forms app. Just run your code from there?

Upvotes: 1

Related Questions