© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

  HomeSource Code DotNet ► Asp.Net ▼

Send Email via SMTP

The SMTP email portion application will demonstrate the following:

  • Using SMTP to configure and send email messages
  • Adding ‘CC’ addresses as message recipients
  • Adding attachments to a message

Imports System
Imports System.Net
Imports System.Net.Mail
Partial Class Default2
    Inherits System.Web.UI.Page
    Private mMailServer As String
    Private mTo As String
    Private mFrom As String
    Private mMsg As String
    Private mSubject As String
    Private mPort As Integer  
 Protected Sub btnSend_Click(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles btnSend.Click
       Dim strMsg As String
        strMsg = txtMessage.Text
        mTo = Trim(txtTo.Text)
        mFrom = Trim(txtFrom.Text)
        mSubject = Trim(txtSubject.Text)
        mMsg = Trim(txtMessage.Text)
        mMailServer = ConfigurationManager.AppSettings.Get("MyMailServer")
        mPort = ConfigurationManager.AppSettings.Get("MyMailServerPort")
        mCC = Trim(txtCC.Text)
        Try
            Dim message As New MailMessage(mFrom, mTo, mSubject, mMsg)
            If fileAttachments.HasFile Then
            Dim attached As New
              Attachment(Trim(fileAttachments.PostedFile.FileName.ToString()))
                message.Attachments.Add(attached)
            End If

            If mCC <> "" Or mCC <> String.Empty Then
                Dim strCC() As String = Split(mCC, ";")
                Dim strThisCC As String
                For Each strThisCC In strCC
                    message.CC.Add(Trim(strThisCC))
                Next
            End If

            Dim mySmtpClient As New SmtpClient(mMailServer, mPort)
            mySmtpClient.UseDefaultCredentials = True
            mySmtpClient.Send(message)

            MessageBox("The mail message has been sent to " & message.To.ToString())

        Catch ex As FormatException
            MessageBox("Format Exception: " & ex.Message)
        Catch ex As SmtpException
            MessageBox("SMTP Exception:  " & ex.Message)
        Catch ex As Exception
            MessageBox("General Exception:  " & ex.Message)
        End Try
    End Sub
  Protected Sub Button2_Click(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
        Dim strMsg As String
        strMsg = txtMessage.Text
        If strMsg.Length >= 140 Then
            strMsg = strMsg.Substring(1, 140)
        End If
        mTo = Trim(txtToNumber.Text) & _
              Trim(cboCarrier.SelectedItem.ToString())
        mFrom = Trim(txtFrom.Text)
        mSubject = Trim(txtSubject.Text)
        mMsg = Trim(txtMessage.Text)
        mMailServer = ConfigurationManager.AppSettings.Get("MyMailServer")
        mPort = ConfigurationManager.AppSettings.Get("MyMailServerPort")
        Try
            Dim message As New MailMessage(mFrom, mTo, mSubject, mmsg)
            Dim mySmtpClient As New SmtpClient(mMailServer, mPort)
            mySmtpClient.UseDefaultCredentials = True
            mySmtpClient.Send(message)

            MessageBox("The mail message has been sent to " & _
                        message.To.ToString())
        Catch ex As FormatException
            MessageBox("Format Exception: " & ex.Message)
        Catch ex As SmtpException
            MessageBox("SMTP Exception:  " & ex.Message)
        Catch ex As Exception
            MessageBox("General Exception:  " & ex.Message)
        End Try
    End Sub
End Class

 

 

 

 

 

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom