Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using MAPISESSION controls 2

Status
Not open for further replies.

jh0401

Computer
Apr 8, 2002
32
US
I am trying to automate the process of sending a drawing via email to customers. When I sign on to the mapi session, a message box pops up to ask what to sign on to. Does anyone know how to avoid this? Also, I get this message when I try to generate the email(from OUTLOOK)
"No transport provider was available for delivery to this recipient." I've noticed that for some reason the mail recipient's address has ' ' around it. Could anyone please help? Here's the code:

Public Sub Send_eMail()

If MAPISession1.SessionID = 0 Then 'SEE IF IN SESSION
MAPISession1.SignOn
End If

MAPIMessages1.SessionID = MAPISession1.SessionID

'Compose new message
MAPIMessages1.Compose

'Address Message
MAPIMessages1.RecipDisplayName = g_By
MAPIMessages1.RecipAddress = g_Email

'Create the message
MAPIMessages1.MsgSubject = "Drawing request: " & g_Num
MAPIMessages1.MsgNoteText = " Your Drawing is attached in an EDRW(eDrawings) format."

'Add attachment
MAPIMessages1.AttachmentPathName = App.Path & "\eDrawings\" & g_Num & ".EDRW"

'Send the message
MAPIMessages1.Send False

If MAPISession1.SessionID > 0 Then 'SEE IF IN SESSION
MAPISession1.SignOff
End If

End Sub

Thanks,

Josh
 
Replies continue below

Recommended for you

Okay, I was able to get it to work, but now I can add only one attachment per email. When I try to add two or more attachments, the last attachment added is the only one that gets sent. If anyone knows how to add multiple attachments
I would greatly appreciate some advice.(I'm using Microsoft Outlook with the following code in a VB app.)

MAPIMessages1.Compose
MAPIMessages1.MsgSubject = "Drawing request: " & g_Num
MAPIMessages1.MsgNoteText = " Your Drawing is attached in PDF format.(TEST)...Thanks, JOSH"
'Add attachment(s)
Do
New_File3 = Dir(App.Path & "\*.PDF")
If New_File3 = "" Then
Exit Do
End If
FileCopy App.Path & "\" & New_File3, App.Path & "\EMAILED\" & New_File3
Kill App.Path & "\" & New_File3
File2.Refresh
Text2 = g_By
Text3 = g_Email
Text4 = New_File3
MAPIMessages1.AttachmentPosition = MAPIMessages1.AttachmentPosition + 10
MAPIMessages1.AttachmentPathName = App.Path & "\EMAILED\" & New_File3
Loop Until New_File3 = "BEAN HEAD"
MAPIMessages1.RecipDisplayName = g_Email
MAPIMessages1.AddressResolveUI = True
MAPIMessages1.ResolveName
MAPIMessages1.Send

The program creates pdf files from drawings and saves them in app.path. I do not know how many files there will be until they are created, so I use the above do loop to get all of the pdf files that were created. It overrides the attachment every pass instead of adding an extra attacment.
Any suggestions?

Thanks,

Josh
 
jh0401,

I believe your problem has to do with indexing the attachments within the DO loop. Since you are not changing the value of the currently indexed attachment, each new attachment defined using MAPIMessages1.AttachmentPathName gets assigned to the same index. Therefore, when the loop ends, only the last named attachment exists. I've added a line of code (highlighted) to yours that I believe will work, based on documentation in Microsoft's MSDN Library. Hope this helps.


Code:
MAPIMessages1.Compose
MAPIMessages1.MsgSubject = "Drawing request: " & g_Num
MAPIMessages1.MsgNoteText = "                              Your Drawing is attached in PDF format.(TEST)...Thanks, JOSH"
'Add attachment(s)
Do
    New_File3 = Dir(App.Path & "\*.PDF")
    If New_File3 = "" Then
        Exit Do
    End If
    FileCopy App.Path & "\" & New_File3, App.Path & "\EMAILED\" & New_File3
    Kill App.Path & "\" & New_File3
    File2.Refresh
    Text2 = g_By
    Text3 = g_Email
    Text4 = New_File3
Code:
'Index the current attachment (AttachmentCount will be 0 initially and 'is automatically updated. AttachmentIndex can range from 0 to 'AttachmentCount - 1)
    MAPIMessages1.AttachmentIndex = MAPIMessages1.AttachmentCount
Code:
MAPIMessages1.AttachmentPosition = MAPIMessages1.AttachmentPosition + 10
    MAPIMessages1.AttachmentPathName = App.Path & "\EMAILED\" & New_File3
Loop Until New_File3 = "BEAN HEAD"
MAPIMessages1.RecipDisplayName = g_Email
MAPIMessages1.AddressResolveUI = True
MAPIMessages1.ResolveName
MAPIMessages1.Send

 
msmith,

Thanks for the tip. You were right about the indexing, but this was not my only problem. I also needed to set the attachment position to an incremented variable because each pass the .AttachmentPosition defaults to zero. I used your tip on setting the .AttachmentIndex = .AttachmentCount and changed the .AttachmentPosition to = g_AttPass(variable). Here's the code that does the trick if you're interested.

'Add attachment(s)
g_AttPass = -1
Do
New_File3 = Dir(App.Path & "\*.PDF")
If New_File3 = "" Then
Exit Do
End If
FileCopy App.Path & "\" & New_File3, App.Path & "\EMAILED\" & New_File3
Kill App.Path & "\" & New_File3
File2.Refresh
Text4 = New_File3
g_AttPass = g_AttPass + 1
MAPIMessages1.AttachmentIndex = MAPIMessages1.AttachmentCount
MAPIMessages1.AttachmentPosition = g_AttPass
MAPIMessages1.AttachmentPathName = App.Path & "\EMAILED\" & New_File3
Loop Until New_File3 = "BEAN HEAD" 'never !!!
MAPIMessages1.Send

Thanks again,

Josh
 
I have another question.
Haw can I send email to several recipience?
Please give me a tip if you know.

Thanks
 
Nick_kh,

I have not tried to send emails to numerous
recipients. I'll see what I can dig up. If
you find anything let me know.

Thanks

Josh H
 
Create an e-mail Group and put the Group name into the .RecipAddress = "GroupName"
 
Thanks LiveIt... I'll give it a try!

Josh H
 
I am trying to downloadmail attachement and copy it to the specified folder i like... I hope someone can help me.
 
Hi! It seems to me like i'm having the problem that jh0401 had. Quotation:
"When I sign on to the mapi session, a message box pops up to ask what to sign on to. Does anyone know how to avoid this?"
I do pass UserName and Password properties correctly, however it doesn't work. Identity login still comes out. I'm having W2K Pro comp, VB6, OE6. Also, i configured several users of Outlook Express. One of them is my application. Here is the code:

Private Sub cmdSend_Click()
With ms ' MAPISession
.UserName = txtUsr
.Password = txtPsw
.LogonUI = False
.NewSession = True
.SignOn
mm.SessionID = .SessionID
End With
With mm ' MAPIMessages
.Compose
.RecipAddress = txtTo
.MsgSubject = txtSubject
.MsgNoteText = txtNote
.Send
End With
ms.SignOff
End Sub

I do not get it! It seems properly writen...
 
FaxFolder

I had the 'question' problem as well. The only way I've
found to avoid getting the pop up question is to already
have Outlook running at the time you send an email. You can always check to see if Outlook is running and shell it
if it is not, but we just have our users keep Outlook
running while they work. Anyway, this is the only way
I've been able to avoid the 'question'. If you find a
better way I would be interested.

Thanks,

Josh
 
hello jh0401,

if you have the solution for the pop up dialog which comes everytime you send a mail from the vb application...please do send it to me,i am facing same problem.
Also if you know any other way to send mails from vb application where no vb nor outlook is installed on the computer...let me know.

thanks
-balaji
 
>> The only way I've found to avoid getting the pop up
>> question is to already have Outlook running at the
>> time you send an email. You can always check to see
>> if Outlook is running and shell it if it is not, but
>> we just have our users keep Outlook running while they
>> work. Anyway, this is the only way I've been able to
>> avoid the 'question'.

We also have a little file called CheckAdminSettings.reg
that will add information to the registry and prevent
the notification - "A program is trying to send an email
on your behalf ... "
- if you are interested.
[/color]

Public Sub Send_Email()

'Sign On
If MAPISession1.SessionID = 0 Then
MAPISession1.SignOn
End If

'Set session ID
MAPIMessages1.SessionID = MAPISession1.SessionID

'Create the message
MAPIMessages1.Compose

'Set the email address
MAPIMessages1.RecipDisplayName = "email_1@blank.com"
MAPIMessages1.RecipAddress = "email_1@blank.com"
MAPIMessages1.AddressResolveUI = True

'If you want to send to another person then increment
'the index & reset the .RecipDisplayName & .RecipAddress
'properties the the new address
MAPIMessages1.ResolveName
MAPIMessages1.RecipIndex = MAPIMessages1.RecipIndex + 1
MAPIMessages1.RecipDisplayName = "email_2@blank.com"
MAPIMessages1.RecipAddress = "email_2@blank.com"
MAPIMessages1.ResolveName

'Set the subject line
MAPIMessages1.MsgSubject = "This is a test."

'Set the body
MAPIMessages1.MsgNoteText = "TEST TEST TEST"

'Send the email
MAPIMessages1.Send

'Sign off
If MAPISession1.SessionID > 0 Then
MAPISession1.SignOff
End If

End Sub
 
Thanks to various posts and msdn.microsoft.com, I have this gotten to work.
- in Outlook, define the various profiles you want to use, eg "profileA" and "profileB"
- the trick is not to specify a userid/pwd in the program, but only the profile name. In the profile (=outlook function), specify the mailbox.

code:
MAPISession1.NewSession = True
MAPISession1.UserName = "profileA"
MAPISession1.LogonUI = False
MAPISession1.DownLoadMail = False
MAPISession1.SignOn

works like a breeze, even when I have outlook running on that machine with a 3rd profile.
 
Hi guys

I was passed this link by a collegue as i'm having difficulty getting my MAPI controls to work. I used JH0401's code but I have three questions if you can help:

1. I placed the code in a module but when called it simply errored out saying "Object Required at the first line. If I place the code behind a button (On_Click) it works fine. However:

2. It asks for a profile. I don't want that as it's going to be run on clients machines remotely.

3. Is there a way to CC: and BCC: automatically...?

I hope you guys can help as I'm really chuffed with JH's code, Just need to "Tweak" it a little...!
 
1. The reason that it fails in a module is because the MAPI controls are controls on a Form, therefore, in a module they are out of scope. In order to reference them, you'll have to explicity use the Form's object name.

FormName.MapiControl.<property/method>

2. I believe that if you also specify a password, then you will not be asked for a profile.
Code:
With lMpi_Session
   .NewSession = True
   .LogonUI = True
   .UserName = <user name>
   .Password = <user password>
   .DownLoadMail = False
   .SignOn
End With
3. As you are building your recipient collection, set the .RecipType property to indicate to, cc, or bcc.

 
I think the reason your code errors in the module is
because the mapi control instance is specific to the
form on which it is placed. If you referenced the form
then the code would work in a module.

ex:

'**** MODULE CODE ****

Public Sub Send_Email(ByVal ReferenceForm as Form)

'Sign On
If ReferenceForm.MAPISession1.SessionID = 0 Then
ReferenceForm.MAPISession1.SignOn
End If

'Set session ID
ReferenceForm.MAPIMessages1.SessionID = ReferenceForm.MAPISession1.SessionID

'Create the message
ReferenceForm.MAPIMessages1.Compose

'Set the email address
ReferenceForm.MAPIMessages1.RecipDisplayName = "email_1@blank.com"
ReferenceForm.MAPIMessages1.RecipAddress = "email_1@blank.com"
ReferenceForm.MAPIMessages1.AddressResolveUI = True

'If you want to send to another person then increment
'the index & reset the .RecipDisplayName & .RecipAddress
'properties the the new address
ReferenceForm.MAPIMessages1.ResolveName
ReferenceForm.MAPIMessages1.RecipIndex = ReferenceForm.MAPIMessages1.RecipIndex + 1
ReferenceForm.MAPIMessages1.RecipDisplayName = "email_2@blank.com"
ReferenceForm.MAPIMessages1.RecipAddress = "email_2@blank.com"
ReferenceForm.MAPIMessages1.ResolveName

'Set the subject line
ReferenceForm.MAPIMessages1.MsgSubject = "This is a test."

'Set the body
ReferenceForm.MAPIMessages1.MsgNoteText = "TEST TEST TEST"

'Send the email
ReferenceForm.MAPIMessages1.Send

'Sign off
If ReferenceForm.MAPISession1.SessionID > 0 Then
ReferenceForm.MAPISession1.SignOff
End If

End Sub


'//// CODE TO CALL PROCEDURE ////
Send_Email THE_FORM_LOADED_IN_MEMORY_WITH_MAPI_CONTROLS.FRM
 
CajunCenturion already answered your question as I was responding!

 
Sorry Guys, but I still can't get it to reference the Module. I know I don't "Need" to - It's just tidier. Also, It means I can use the module in other projects.

There's another problem I discovered though:

It send mail OK and it gets droppped into the "Sent Items. However, I don't receive the mail.

I checked the sent item and it marked the e-mail Type as the recipients address and not "SMTP" which I believe it should have.

Also, If you have the time - I could really do with sending an attachment if possible, It's only one file and will always reside in the same place on the client's HDD.

Many thanks for your patience . . .
 
'Add attachment(s) - ALL .PDF files within app.path directory - destroy file after adding as attachment and copying to fileserver
Dim g_AttPass% '// Attacment Index
Dim New_File3$ '// File string
g_AttPass = -1
Do while dir(app.path & "\*.PDF") > ""
New_File3 = Dir(App.Path & "\*.PDF")
FileCopy App.Path & "\" & New_File3, "\\Fileserver1\FILESER1_E\web solidworks dwg_pdf\" & New_File3
Kill App.Path & "\" & New_File3
g_AttPass = g_AttPass + 1 'Increment attachment index
MAPIMessages1.AttachmentIndex = MAPIMessages1.AttachmentCount
MAPIMessages1.AttachmentPosition = g_AttPass
MAPIMessages1.AttachmentPathName = "\\Fileserver1\FILESER1_E\web solidworks dwg_pdf\" & New_File3
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top