Results 1 to 12 of 12

Thread: email/attachment difficulity

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    email/attachment difficulity

    hi....
    this is my first application for sending emails through visual basic...

    my application works fine but there are few things i need help please..

    1-when i send an email with an attachment to a person the person will recieve attachments of the same document...(for example if i sent 1.doc to [email protected] this user will recieve in his email the attached document 1.doc two time ) i don't want this to happen...
    and if i want to send to more than one user i get this error :
    Invalid seesion, here where i get the error when i debug:

    Code:
     .ResolveName
    2-and if i want to send an email withoout an attachment i get attachment not found

    Code:
      .Send
    3-and how can i send more than one attachment

    this is my code:

    Code:
    Private Sub Form_Load()
    'Sign on to the MAPI Session
    
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    
    MAPIMessages1.Fetch
    
    
    End Sub
    Code:
    Private Sub cmdSend_Click()
    'Start by telling the control that we are composing an e-mail
    
    'MAPIMessages1.Compose
    
    'Use whatever is in the Textboxes as the information for our e-mail.
    
    'MAPIMessages1.RecipDisplayName = txtTo.Text
    
    'MAPIMessages1.MsgSubject = txtSubject.Text
    
    'MAPIMessages1.MsgNoteText = txtMessage.Text
    'MAPIMessages1.AttachmentPathName = txtAttach.Text
    
    'MAPIMessages1.ResolveName
    
    'Send the e-mail message to the Recipient
    
    'MAPIMessages1.Send
    ' MAPISession1.SignOn
    
           'Create Message w/Attachments then Send
           With MAPIMessages1
       
               .Compose
               .MsgSubject = txtSubject.Text
    
               .RecipDisplayName = txtTo.Text
               .ResolveName
    
               'The value 2 is passed to the Space() function in the
               'next line because there are two attachments to add.
               'These spaces act as placeholders for the attachments.
               .MsgNoteText = Space(2) & vbCrLf & _
                   "This message was sent from the MSMAPI32.OCX." & vbCrLf & _
                   "This message contains 2 attachments."
    
               'Replace the AttachmentPathName values below as applicable...
               .AttachmentIndex = 0
               .AttachmentPosition = 0
               .AttachmentPathName = txtAttach.Text
              .AttachmentIndex = 1
               .AttachmentPosition = 1
               .AttachmentPathName = txtAttach.Text
    
              
               .Send
           End With
    
           MAPISession1.SignOff
    
    txtFrom.Text = ""
    
    txtTo.Text = ""
    
    txtSubject.Text = ""
    
    txtMessage.Text = ""
    txtAttach.Text = ""
    
    End Sub
    Code:
    Private Sub cmdBrowse_Click()
    
         Dim sFilenames()    As String
        Dim i               As Integer
        
        On Local Error GoTo Err_Cancel
      
        With cmDialog
            .FileName = ""
            .CancelError = True
            .Filter = "All Files (*.*)|*.*|HTML Files (*.htm;*.html;*.shtml)|*.htm;*.html;*.shtml|Images (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif"
            .FilterIndex = 1
            .DialogTitle = "Select File Attachment(s)"
            .MaxFileSize = &H7FFF
            .Flags = &H4 Or &H800 Or &H40000 Or &H200 Or &H80000
            .ShowOpen
            ' get the selected name(s)
            sFilenames = Split(.FileName, vbNullChar)
        End With
        
        If UBound(sFilenames) = 0 Then
            If txtAttach.Text = "" Then
                txtAttach.Text = sFilenames(0)
            Else
                txtAttach.Text = txtAttach.Text & ";" & sFilenames(0)
            End If
        ElseIf UBound(sFilenames) > 0 Then
            If Right$(sFilenames(0), 1) <> "\" Then sFilenames(0) = sFilenames(0) & "\"
            For i = 1 To UBound(sFilenames)
                If txtAttach.Text = "" Then
                    txtAttach.Text = sFilenames(0) & sFilenames(i)
                Else
                    txtAttach.Text = txtAttach.Text & ";" & sFilenames(0) & sFilenames(i)
                End If
            Next
        Else
            Exit Sub
        End If
        
    Err_Cancel:
    
    
    End Sub
    hope some one can help me please ...
    thank you very much
    i will attach my application
    Attached Files Attached Files

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    Re: email/attachment difficulity

    sorry this is the correct code for the send button:
    Code:
    Private Sub cmdSend_Click()
    'Start by telling the control that we are composing an e-mail
    
    'MAPIMessages1.Compose
    
    'Use whatever is in the Textboxes as the information for our e-mail.
    
    'MAPIMessages1.RecipDisplayName = txtTo.Text
    
    'MAPIMessages1.MsgSubject = txtSubject.Text
    
    'MAPIMessages1.MsgNoteText = txtMessage.Text
    'MAPIMessages1.AttachmentPathName = txtAttach.Text
    
    'MAPIMessages1.ResolveName
    
    'Send the e-mail message to the Recipient
    
    'MAPIMessages1.Send
    ' MAPISession1.SignOn
    
           'Create Message w/Attachments then Send
           With MAPIMessages1
       
               .Compose
               .MsgSubject = txtSubject.Text
    
               .RecipDisplayName = txtTo.Text
               .ResolveName
    
               'The value 2 is passed to the Space() function in the
               'next line because there are two attachments to add.
               'These spaces act as placeholders for the attachments.
               .MsgNoteText = Space(2) & vbCrLf & _
                   "This message was sent from the MSMAPI32.OCX." & vbCrLf & _
                   "This message contains 2 attachments."
    
               'Replace the AttachmentPathName values below as applicable...
                .AttachmentIndex = 0
               .AttachmentPosition = 0
               If txtAttach.Text <> vbNullString Then
               .AttachmentPathName = txtAttach.Text
              .AttachmentIndex = 1
               .AttachmentPosition = 1
               .AttachmentPathName = txtAttach.Text
               End If          
               .Send
           End With
    
           MAPISession1.SignOff
    
    txtFrom.Text = ""
    
    txtTo.Text = ""
    
    txtSubject.Text = ""
    
    txtMessage.Text = ""
    txtAttach.Text = ""
    
    End Sub

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: email/attachment difficulity

    just my 2 cents
    I use a different program for emails from vb that requires semi-colons ( as
    as seperators rather than spaces
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    Re: email/attachment difficulity

    any idea ?

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: email/attachment difficulity

    you can loop through the array to put all the attachment names into the textbox, to display, but you can not then use the textbox text to specify the attachments in the email better to just get them again from the commondialog.filename
    like
    vb Code:
    1. myarray = split(commondialog.filename, vbnullchar)
    2. for i = 1 to ubound(myarray)
    3.    .attachmentindex = i -1
    4.    .attachmentposition = i -1
    5.    .attachmentpathname = myarray(0) & myarray(i)
    6. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    Re: email/attachment difficulity

    hi westconn1

    thankx , but i tried what you told me about it''s still not working...

    did you see my zip file?
    can you show me where to change in my application?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    Re: email/attachment difficulity

    can someone help me please?

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: email/attachment difficulity

    what part of it is not working?
    do you get an error, if so where?
    if not what is happening that is not correct?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    Re: email/attachment difficulity

    i can now send more than one attachment , i got it done...


    my problem is now that i cannot send to more than one person....
    even if i put in the to text box semicolons or commas( example: [email protected];[email protected]) it will not work , when i debug it i get the error at
    Code:
    .ResolveName

    so how can i send to more than one user?

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: email/attachment difficulity

    looks like you have to add them in same way as the attachments

    To add a new recipient, set the RecipIndex to a value greater than or equal to the current recipient count while in the compose buffer. The RecipCount property is updated automatically to reflect the implied new number of recipients. For example, if the current RecipCount property has the value 3, setting the RecipIndex property to 4 adds 2 new recipients and increases the RecipCount property to 5.
    read the helpfiles
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    150

    Re: email/attachment difficulity

    i added a new text box...
    it will send me for to users , but when i click on the send buttons and when the text box are cleared and then i enter another emails i get the error:
    Invalid session

    Code:
    Code:
    .ResolveName
    i still cannot put the emails in the same text box seperated by commas
    this is my code:
    Code:

    Code:
       With MAPIMessages1
       
               .Compose
               .MsgSubject = txtSubject.Text
    
               '.RecipDisplayName = txtTo.Text
               '.ResolveName
    .RecipIndex = 0
    .RecipDisplayName = txtTo.Text
    .RecipAddress = txtTo.Text
    .RecipType = 1
    .ResolveName
    
    .RecipIndex = 1
    .RecipDisplayName = txtTo1.Text
    .RecipAddress = txtTo1.Text
    .RecipType = 1
    .ResolveName

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: email/attachment difficulity

    you can put all the recipients in a textbox separated by whatever you want, then when adding the recipients, split the textbox text on the separator, and add the recipients from the array

    vb Code:
    1. reciparr = split(txtTo.text, ";")
    2. for i = 0 to ubound(reciparr)
    3.     .recipindex = i
    4.     .recipdisplayname = reciparr(i)
    5.     .recipaddress = reciparr(i)
    6.     .resolvename
    7. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width