cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Email from within Rulestream

Community Manager Community Manager
Community Manager

To send email using the SendEmail subroutine, place the following code in custom.VB.

 

    Public Sub SendEmail()

        Try

            Dim message As New System.Net.Mail.MailMessage("From@mycompany.com", "To@yourcompany.com", "Subject", "BodyText")

            Dim emailClient As New System.Net.Mail.SmtpClient("my.smtpserver.com")

 

            'use the next 3 lines if your smtp server needs authentication else comment them out

            Dim SMTPUserInfo As New System.Net.NetworkCredential("myself", "mypassword", "mydomain")

            emailClient.UseDefaultCredentials = False

            emailClient.Credentials = SMTPUserInfo

            '-----------------------------------------------------------------------------------------

            'use the next 2 lines to attach any files else comment them

            Dim attachFile As New System.Net.Mail.Attachment("C:\Attachment.GIF")

            message.Attachments.Add(attachFile)

            '-----------------------------------------------------------------------------------------

 

            emailClient.Send(message)

        Catch ex As Exception

            MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

 

 

Use the following code to send an Email from Exchange:

 

NOTE:

 

To use the signature you need to create a signature called Siemens (in this example). The email will be sent as HTML. If you don’t want HTML delete the signature section and change .HTMLBody to .Body

 

Public Sub SendMail(ByVal Subject As String, ByVal Bodytext As String, ByVal Recipient As String, Optional ByVal Attachment As String = "File as Attachment")

 

        Try

            '--- Check whether the Outlook process is running

            If Process.GetProcessesByName("outlook").Length > 0 Then

                '--- evrything is fine don't do anything ---

            Else

                '--- start Outlook  ---

                Process.Start("outlook")

                Application.DoEvents()

            End If

 

            '--- initalize Outlook ---

            Dim objOutlook As Object

            Dim objOutlookMsg As Object

            Const cMailItem = 0

 

            '--- if needed please uncomment ---

            'Const cBCC = 3

            'Const cImportanceHigh = 1

            'Const cCC = 2

            Const cTo = 1

                                            

                                             '--- get Signature ---

                                             Dim appData As String = Environment.GetEnvironmentVariable("APPDATA")

                                             Clipboard.SetText(IO.File.ReadAllText(appData & "\Microsoft\Signatures\Siemens.htm", System.Text.Encoding.Default))

                                             Dim mySignature As IDataObject = Clipboard.GetDataObject()

 

            '--- create object ---

            objOutlook = CreateObject("Outlook.Application")

            objOutlookMsg = objOutlook.CreateItem(cMailItem)

 

            '--- create mail ---

            With objOutlookMsg

                Dim objOutlookRecip As Object = .Recipients.Add(Recipient)

                objOutlookRecip.type = cTo

                .Subject = Subject

                .HTMLBody = Bodytext & CType(mySignature.GetData(GetType(String)), String)

                'If Attachment <> "File as Attachment" Then .Attachments.add(Attachment)

                '.Importance = olImportanceHigh 'If needed please uncomment

                .Display() ' Display() shows mail, .Send() sends directly

            End With

 

            '--- destroy object ---

            objOutlookMsg = Nothing

            objOutlook = Nothing

 

        Catch ex As Exception

 

            '--- Catch error if Outlook is not installed ---

            MsgBox(ex.Message)

            '----------------------------------------------------------

 

        End Try

 

    End Sub

Contributors