DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Email from vb app

  1. #1
    Johannes Guest

    Email from vb app


    Can anyone explain to me how do I Email from my application? How do I attach
    a file to the eMail? Please explain step by step.
    regards
    Johannes

  2. #2
    Siva Guest

    Re: Email from vb app


    "Johannes" <Greendoor@hotmail.com> wrote:
    >
    >Can anyone explain to me how do I Email from my application? How do I attach
    >a file to the eMail? Please explain step by step.
    >regards
    >Johannes



    Hi,
    I think its too detailed to explain in steps. The following code is documented
    sufficiently. I think its not too big...
    cheers,
    Siva

    Public Function SendMail(ByVal strSubject As String, _
    ByVal strMessage As String, _
    ByVal eMailBox As edpsMailboxOptions, _
    Optional ByVal strEMailTo As String, _
    Optional ByVal strServer As String, _
    Optional ByVal blnSendAsAttachment As Boolean =
    True) As Boolean

    Dim oSession As MAPI.Session
    Dim oMessage As MAPI.Message
    Dim oRecip As MAPI.Recipient
    Dim oOutBox As MAPI.Folder

    Dim sServer As String
    Dim sEMail As String
    Dim sMailbox As String
    Dim sProfileInfo As String
    Dim sProfileName As String

    ' Get the MTS Object context

    Set m_oObjectContext = GetObjectContext

    ' Create a reference to the session object

    Set oSession = New MAPI.Session

    m_oLog.LogData "----------------------", "MailAudit.txt"
    m_oLog.LogData "Starting to send a New Mail", "MailAudit.txt"

    ' Logon using the settings read from the MailConfig.xml.
    ' Please note that the ProfileInfo parameter = Servername & vbLf & Mailbox

    oSession.Logon "", "", False, False, , , strServer & vbLf & strEMailTo


    m_oLog.LogData "Logon Successful..", "MailAudit.txt"

    ' Create a reference to the Folder object..(OUTBOX)
    Set oOutBox = oSession.Outbox

    Set oMessage = oOutBox.Messages.Add

    With oMessage

    ' Form the subject and the contents of the message

    .Subject = strSubject

    ' Now check whether the message needs to be sent as an attachment.

    If blnSendAsAttachment Then
    Call CreateAttachmentList(oMessage, strMessage)
    Else
    .Text = strMessage
    End If

    ' Now add a recipient

    Set oRecip = .Recipients.Add(strEMailTo)

    m_oLog.LogData "Resolving recipients successfull..", "MailAudit.txt"

    ' Resolve the address of the recipient

    oRecip.Resolve

    ' After resolving, send the message to the specified recipient

    .Send
    End With

    ' Log off the current session

    oSession.Logoff

    ' We have finished. Return true

    SendMail = True

    If m_bInMTS Then
    m_oObjectContext.SetComplete
    End If

    m_oLog.LogData "The mail has been successfully sent..", "MailAudit.txt"

    CleanupExit:

    Set oSession = Nothing
    Set oMessage = Nothing
    Set oRecip = Nothing
    Set oOutBox = Nothing

    Exit Function

    ErrHandler:

    ' Clear down any objects

    Set oSession = Nothing
    Set oMessage = Nothing
    Set oRecip = Nothing
    Set oOutBox = Nothing

    ' Prepare variables to hold error information

    Dim nErrorNumber As String
    Dim sDescription As String
    Dim sSource As String

    ' Store the error into the variables

    With Err
    nErrorNumber = .Number
    sDescription = .Description
    sSource = .Source
    End With

    ' Set the MTS package as having an error

    If m_bInMTS Then
    m_oObjectContext.SetAbort
    End If

    ' Log the error to the database

    m_oLog.LogData "Error occured while sending mail: " & sDescription, "MailAudit.txt"
    m_oLog.LogData "----------------------" & vbCrLf, "MailAudit.txt"

    Call m_oErrorLog.LogError(Err)

    'Now raise the error to the calling routine

    Err.Raise nErrorNumber, sSource, sDescription

    End Function

    --------------------------------------------------------------------
    To send it as an attachment please refer the function below

    Private Sub CreateAttachmentList(ByRef oMessage As MAPI.Message, _
    ByVal strMessage As String)

    Dim oFileSys As New Scripting.FileSystemObject
    Dim oTextStream As Scripting.TextStream
    Dim sFileName As String

    On Error GoTo ErrHandler

    ' Create the text file to be sent as an attachment

    sFileName = App.Path & "\DPS_Attachment" & App.ThreadID & ".txt"
    Set oTextStream = oFileSys.CreateTextFile(sFileName, True)

    ' Write the message into the TextFile

    With oTextStream
    .Write strMessage
    .Close
    End With

    ' Add the attachment to the message

    With oMessage
    .Attachments.Add , , CdoFileData, sFileName
    .Update
    End With

    If Dir(sFileName) <> vbNullString Then
    Kill sFileName
    End If

    CleanupExit:

    Set oFileSys = Nothing
    Set oTextStream = Nothing

    Exit Sub

    ErrHandler:

    ' Clear down any objects

    Set oFileSys = Nothing
    Set oTextStream = Nothing

    ' Prepare variables to hold error information

    Dim nErrorNumber As String
    Dim sDescription As String
    Dim sSource As String

    ' Store the error into the variables

    With Err
    nErrorNumber = .Number
    sDescription = .Description
    sSource = .Source
    End With

    ' Log the error to the database

    Call m_oErrorLog.LogError(Err)

    ' Now raise the error to the calling routine

    Err.Raise nErrorNumber, sSource, sDescription

    End Sub
    -----------------------------------------------------------------------



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center