dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 4 123 ... LastLast
Results 1 to 15 of 48

Thread: email problem

  1. #1
    Join Date
    Mar 2008
    Posts
    23

    email problem

    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 user@----.com 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

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX
    Quote Originally Posted by nawaray
    2-and if i want to send an email withoout an attachment i get attachment not found
    For this, try
    Code:
           '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  '<====== add this IF
                  .AttachmentPathName = txtAttach.Text
                  .AttachmentIndex = 1
                  .AttachmentPosition = 1
               End If
               .Send
           End With

  3. #3
    Join Date
    Mar 2008
    Posts
    23
    HI Hack..

    i tried what you told me but i still get the same error.

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Also, why do you have this line twice?
    Code:
             .AttachmentIndex = 0
               .AttachmentPosition = 0
               .AttachmentPathName = txtAttach.Text  '<=======
              .AttachmentIndex = 1
               .AttachmentPosition = 1
               .AttachmentPathName = txtAttach.Text '<========

  5. #5
    Join Date
    Mar 2008
    Posts
    23
    if i don't write this second line
    .AttachmentPathName = txtAttach.Text '<========

    i get the same error
    attachment not found..


    i will attach my application
    Attached Files Attached Files

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Did you try the IF statement?

  7. #7
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    hey nawaray:

    hope this can help :

    1- .. r u trying to put the TO email as something like :
    [ email1@domain1.com , email2@domain2.com ]
    I think the error is here because ".ResolveName" wasn't able to resolve this as the standard email format ...

    2- this is simple because you don't write in the attachment text anything and in the same time you want the program to execute the attachment statement ... you should as Hack told you put a check or some condition :
    first you got TWO attachments because you have :
    Code:
               .AttachmentIndex = 0
               .AttachmentPosition = 0
               .AttachmentPathName = txtAttach.Text
              .AttachmentIndex = 1
               .AttachmentPosition = 1
               .AttachmentPathName = txtAttach.Text
    which means if you have an attachment path in "txtAttach.text" then put it in the index number 0 and attach it to the message then put it again in the index number 1 and attach it again ... this shouldn't happens like that ..
    here is what you should make your code work like :

    Code:
    for i=0 to NUMBERofAttachments do
                  .AttachmentIndex = i
               .AttachmentPosition = i
               .AttachmentPathName = txtAttach(i).Text
    next i
    or some work like that .. each attachment path should be in a different textbox ... and you assign ONCE the index, position and path if there is any .. otherwise .. if you don't want any attachments dont do anything .. that's all
    Last edited by Amahdy; 03-09-2008 at 10:54 AM.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by Amahdy
    if you don't want any attachments dont do anything .. that's all
    Well, there may be attachments for one message but not others so I think the best way to handle this situation is to wrap the attachment portion in an IF/Then structure.

  9. #9
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    ok fine you r right ,that's exactly what this do :

    for i=0 to NUMBERofAttachments do

    if he can handle "NUMBERofAttachments" correctly then when its value is ZERO it will not iterate the loop at all .. just workin as the "if" system .. (we know that there is an embed "if" statement before each loop iteration)
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  10. #10
    Join Date
    Mar 2008
    Posts
    23
    but i did put the if statement i still get the same error when i don't attach a file i'll get :
    error : attachment not found....


    did you see my zip file?

  11. #11
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Put a break on the code and step through it to see exactly what it is doing behind the scenes.

  12. #12
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    I won't download anything .. your first post was just fine and better to get answers than asking to download files ... my question is do you understand what the code do exactly and how it works ?
    we gave you exact solutions for what make the mentioned problems ... we won't rewrite the code for you, you should know and learn how to work around this .. and feel free to ask on "how to" again .. that's pretty fine .
    brief :
    No attachment then those lines should not be executed :
    .AttachmentIndex = X
    .AttachmentPosition = X
    .AttachmentPathName = XXX

    One or more attachments then the previous lines should be executed the same number of attachments with different values of course in each execution ...

    One good method to do that is simply a "For Loop" .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  13. #13
    Join Date
    Mar 2008
    Posts
    23
    First of all thankx
    Amahdy and
    Hack

    for replying ...

    Second:
    I do understand the code , and i doing what You are telling me to do (Amahdy)(putting it in a loop)
    but i'm still getting the same error....

    sorry for being a pain in a neck but as you know i'm am new to visual basic thats why i'm getting confused

  14. #14
    Join Date
    Mar 2008
    Posts
    23
    hi again...

    i did it , it can send now if i don't attach anything....thank you for your help

    now how can i send to more than one person?
    and i did put another text box for another attachment file, it works , but it will send it as 2 emails when i attach 2 attachments....(it will not send the 2 attachments in one email) i want it in one email
    for example( if i attach 1.doc + 2.doc)
    it will send it but as 2 emails....

    and it still sends 2 attachments in each email...
    i mean the first email the one that has 1.doc it will attach 1.doc twice.



    thank you
    sorry for disturbing

  15. #15
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by nawaray
    now how can i send to more than one person?
    Use your existing code
    Code:
    .RecipDisplayName = txtTo.Text
    But put more than one email address in there separated by commas (or semi-colons, I can't remember which)

Similar Threads

  1. Stand alone email client for pop3 email
    By luckyluk in forum VB Classic
    Replies: 1
    Last Post: 03-18-2006, 06:36 PM
  2. Reroute All Outbound Email
    By Brian in forum Enterprise
    Replies: 0
    Last Post: 08-13-2003, 02:22 PM
  3. convert email messages to tasks utomatically
    By Anthony Meo in forum Enterprise
    Replies: 0
    Last Post: 01-15-2002, 05:44 PM
  4. Replies: 0
    Last Post: 04-03-2001, 05:22 PM
  5. Replies: 9
    Last Post: 01-26-2001, 12:42 PM

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