Automatic Email Error with VB.net in Office 2013 Environment - vb.net

I have a vb.net script that sends automatic emails from Outlook. It currently works in the Office 2010 Suite. I upgraded my environments to 2013 and now it is not working. What I'm doing isn't complicated. All I want to do is grab a .pdf and attache it to an email. Then add the recipients and send the email. The place where it crashes is the actual sending of the email. It throws this execption
System.Runtime.InteropServices.COMEception occurred in Microsoft.VisualBasic.dll
Additional information: Operation aborted (Execption from HRESULT: 0x80004004 (E_ABORT))
I'm thinking because I upgraded environments something isn't referenced correctly. But I can't seem to find a fix. Below is my code, any help would be greatly appreciated! Thanks in advanced.
Imports Outlook = Microsoft.Office.Interop.Outlook
Module Module1
Sub main()
sendEmail()
End Sub
Public Sub sendEmail()
Dim ol As Object
Dim mail AS Object
ol = CreateObject("Outlook.Application")
mail = ol.CreateItem(0)
mail.To = "email#example.com"
mail.Subject = "Subject Line"
mail.Body = "Body text"
mail.Attachments.Add("path\to\attachment.pdf")
mail.Send() 'crashes on this line
mail = Nothing
ol = Nothing
End Sub
End Module
Also I am running on Windows 7 with the Office 2013 suite. Looking at the App.config my supported Runtime version is v4.0 and the .NetFramework version is v4.5

Since your solution is automating Outlook cross-process you first need to ensure that these types of applications are authorized so check your anti-virus state in Outlook's Trust Center:
https://msdn.microsoft.com/en-us/library/office/ff864479.aspx
I would also wrap the CreateObject call in a Try/Catch block to ensure that isn't failing to begin with. Also make sure that you have an Outlook profile created and that your application isn't running under a non-authorized security context like via a Task Scheduler process or in a web application.

Related

Draft messages created by Outlook VBA give "The operation failed" error when sent

At work we are using Outlook 2016 and have recently upgraded to Microsoft Exchange. We have various VBA programs, run from Excel or Access, which create Outlook emails and save them in the Drafts folder, ready to be checked and sent by hand after running the program. These programs used to work before we went over to Microsoft Exchange.
Now, when I double-click one of the emails that were saved in Drafts by whichever program I was running, Outlook refuses to send it, and reports the error "The operation failed." No other information or error codes are provided. One of my colleagues has the same problem. Others do not. In theory we should all have the same version of Outlook and Microsoft Exchange.
I have discovered that I can reproduce this problem by running code directly within Outlook. I did Alt-F11, enabled macros, inserted a module and pasted the following code into the window.
Sub TestEmail2()
Dim OutMail As Outlook.MailItem
Dim EmailTo As String
Dim EmailSubject As String
EmailTo = "me#myaddress.com"
EmailSubject = "Test Email 2"
Set OutMail = Application.CreateItem(0)
With OutMail
.To = EmailTo
.Subject = EmailSubject
.Save
.Close 0 ' olSave
End With
Set OutMail = Nothing
Debug.Print "Finished"
End Sub
If I set a breakpoint at "Set OutMail = Nothing", and run the code to that breakpoint (so "Set OutMail = Nothing" hasn't executed yet), then go to my Drafts folder and try to send the email, it will go without errors. If I rerun the code and allow it to run to completion, I get the error if I try to send the email it generates.
I would like to either (1) find a workaround that I can add to the code so I can send messages or (2) find out what is wrong with my Outlook profile so I can ask our IT helpdesk to change it, or change it myself. I don't want our IT helpdesk to reset my profile to the default settings and lose my customisations.
That usually indicates that the address book is corrupt. Try to use the Resolve or ResolveAll methods to resolve recipients against the address book.
This issue may also occur when a Google Apps Sync for Microsoft Outlook account and a Microsoft Exchange Server account are configured in the same Outlook profile. See "The operation failed" error when you send email messages in Outlook for more information.
Besides, it is also suggested to try repairing Office via Control Panel and see if the issue can be fixed. Also, you may try repairing your PST files.

CreateItemFromTemplate Type mismatch

I have code to generate an email based on data on a userform.
It worked for a long time across multiple machines, but when I changed machines it started throwing a run-time error 13 type mismatch on on the CreateItemFromTemplate method in the following section:
Private Sub AcaoEnviar_Click()
Dim OutlookApp As New Outlook.Application
Dim EmailKRI As Outlook.MailItem
Set EmailKRI = OutlookApp.CreateItemFromTemplate(PATH_EMAIL_TEMPLATE)
The code still runs on other machines, suggesting the problem is local. My Outlook library is the same as on the other machines, and the only difference I´m aware of is that I´m running it on a 64-bit system and 32-bit office, while most others have 32-bit systems (one other has the same 64-bit setup and runs fine).
Changing the object declaration from Outlook.MailItem to Object seems to solve the problem, which I think eliminates problems with Outlook's programmatic access, and in any case, my Antivirus status is valid.
I'm still confused as to why such an error would occur only with me.
Is there any solution so that I can go back to early binding for that email object?
You will get that error if CreateItemFromTemplate returns something other than MailItem object. Are you sure the OFT file is for a regular mail item.
As a test, declare EmailKRI as a generic Obejct and at run time check the value of the EmailKRI.Class property. It is supposed to be 43 (OlObjectClass.olMail) for the regular MailItem objects.

PIA Outlook Office library

I am having simple question.
I installed PIA Office 15.0 for outllook and gave reference in my winform .net application.
I just wanted to know if I deploy this application on machine which is having lower version of outlook(e.g. outlook 2007/2010) will my application work properly?
Sorry it is 14.0 outlook PIA refernece given by me to my app. and code is
` Dim OutlookMessage As outlook.MailItem
Dim AppOutlook As New outlook.Application
Try
Dim oApp As Microsoft.Office.Interop.Outlook._Application
oApp = New Microsoft.Office.Interop.Outlook.Application
Dim oMsg As Microsoft.Office.Interop.Outlook._MailItem
oMsg = oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
oMsg.Subject = P_Subj
oMsg.Body = P_Body
oMsg.To = P_To
'oMsg.CC = sCC
If Trim(P_AttachPath) <> "" Then
Dim sBodyLen As Integer = Int(P_Body.Length)
Dim oAttachs As Microsoft.Office.Interop.Outlook.Attachments = oMsg.Attachments
Dim oAttach As Microsoft.Office.Interop.Outlook.Attachment
oAttach = oAttachs.Add(P_AttachPath, , sBodyLen, P_AttachPath)
End If
oMsg.Send()
MsgBox("Mail sent to outlook successfully. ", MsgBoxStyle.Information, "")
oApp = Nothing
oMsg = Nothing '
it is giving error for office 2007.
I'd recommend using PIAs that correspond to the lowest version of Outlook/Office. Thus, you can be sure that you don't use methods and properties introduced in later versions. In general, if you embed interop types (read more below) it will run without issues.
Beginning with the .NET Framework 4, the common language runtime supports embedding type information for COM types directly into managed assemblies, instead of requiring the managed assemblies to obtain type information for COM types from interop assemblies. Because the embedded type information includes only the types and members that are actually used by a managed assembly, two managed assemblies might have very different views of the same COM type. Each managed assembly has a different Type object to represent its view of the COM type. The common language runtime supports type equivalence between these different views for interfaces, structures, enumerations, and delegates. You can read more about that in the Type Equivalence and Embedded Interop Types article in MSDN.
Also see Walkthrough: Embedding Types from Managed Assemblies (C# and Visual Basic).
I have been using the Microsoft Office 2010: Primary Interop Assemblies Redistributable in several of my applications, and I can report that deployment on Windows 8.1/8/7 running Office 2013/2010 is flawless. I have also had some success deploying to XP machines running Office 2003, but this is not guaranteed.
The 2010 PIA Redistributable is available for download at http://www.microsoft.com/en-us/download/details.aspx?id=3508

Sync to Outlook 2010 with redemption in vb.net

I am developing an application where I want to sync the outlook calendar, mail, etc.
The piece of code which is there with me is working fine for outlook 2003, 2007.
but I don't know what's the problem it is causing for outlook 2010.
I use the following:
redemption dll 5.4 version-32 bit
office object core 14 dll-32 bit
office -32 bit version
visual studio 2008
I am getting this error:
Cannot create ActiveX component.
On the following line:
session = CreateObject("Redemption.RDOSession")
Where session is:
Dim session As Redemption.RDOSession
After which the following error is displayed:
Either there is no default mail client or the current mail client cannot fulfill the messaging request
I have tried various things but it's same. I also reinstalled, repaired outlook, repaired registry.
Any ideas?
this worked for me:
first, I will say I used the Customize.exe that comes with redemption to create a dll called ffloader.dll
Next, I registered that dll as follows (32 bit)
regsvr32 ffloader.dll
Finally in the vb.net code I created the session as follows:
Dim Session As Redemption.RDOSession = CreateObject("ffloader.ffRDOSession")
Hope that helps!

Sending Multi-threaded SendEmail request returns General Mapi Failure

I'm using the MAPI code by Dave Brooks.
I am attempting to programatically send out a Crystal Report that is generated.
When I run through the code without threading everything runs fine. The problem is when I use threading I get the return error "General MAPI failure [2]".
I have never used threading before and understand that there are dangers involved. Can anyone provide any insight into this issue? NOTE: I've removed exception handling to make the code clearer.
Private Sub RunReport()
SetParameters()
SaveReportFile()
Dim operation As New ThreadStart(AddressOf SendEmail)
Dim theThread As New Thread(operation)
theThread.Start()
End Sub
Public Sub SendEmail()
Dim m As MAPI
m = New MAPI()
Dim email As String
For Each email In emailAddress
m.AddRecipientBCC(email)
Next email
m.AddAttachment(#"c:\temp\report.pdf")
m.SendMailPopup("Requested Report", "")
End Sub
A very late answer, but thought I'd add it anyway as I just encountered this and couldn't find an answer elsewhere.
You need to set your thread's appartment state to STA before it is started using:
theThread.SetApartmentState(ApartmentState.STA);
Note that threads from the threadpool (e.g. used by BackgroundWorker component) are MTA.
I encountered this same error (General MAPI failure [2]) and came across this solution early in my debugging; however, the cause for my error was due to running my application as administrator while outlook was running as my user. I had a hard time finding the cause of my error so hopefully this will help someone on the same search as me.
If the above answer doesn't solve your problem, try running your application without elevated privileges if possible or find a way to call MAPI using user impersonation.