Send email with Outlook 2016 from ASP.NET - asp.net

I need to send emails but preview them first before sending. I am using Visual Studio 2019 and my Outlook version is 2016 32-bit on a Windows 10 development machine.
I get an error
FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.
Yet I installed the COM libraries via browse to C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Tools.Excel.v9.0\15.0.0.0
I use Outlook version 16 and from what I can tell there is no version 16 of the COM library.
Is there another way to get this working?
private void SendEmail()
{
try
{
Outlook.MailItem mailItem = (Outlook.MailItem)this.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "someone#example.com";
mailItem.Body = "This is the message.";
mailItem.Importance = Outlook.OlImportance.olImportanceLow;
mailItem.Display(false);
}
catch (Exception eX)
{
throw new Exception("cDocument: Error occurred trying to create an Outlook e-mail"
+ Environment.NewLine + eX.Message);
}
}
This is the VB code I used to run Outlook on local machine.
Public Sub SendEmail(ByVal sTo As String, ByVal sSubject As String, ByVal sBody As String)
Try
If Not Process.GetProcessesByName("OUTLOOK").Any() Then
System.Diagnostics.Process.Start("Outlook.exe")
End If
'Dim oApp As Outlook._Application
'oApp = New Outlook.Application()
Dim oApp As Object
' Create a new MailItem.
'Dim oMsg As Outlook._MailItem
Dim oMsg As Object
Const olMailItem As Long = 0 'à commenter si marche pas
oApp = CreateObject("Outlook.Application") 'à commenter si marche pas
'oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
oMsg = oApp.CreateItem(olMailItem)
oMsg.Subject = sSubject
oMsg.Body = sBody
' TODO: Replace with a valid e-mail address.
oMsg.To = sTo
' Send
oMsg.Send()
' Clean up
oMsg = Nothing
oApp = Nothing
Catch ex As Exception
Debug.Print(ex.ToString)
End Try
End Sub

Is this really asp.net and a web based application???? You do realize the VAST difference between windows desktop software and web based, right? You can't use or display or show ANYTHING from a desktop program in a client side web browser. I have to assume you taking about desktop ONLY software here, and not web based? I mean, even if you could use Outlook in code, it would run on the web server, and the user would not see outlook running on that one web server computer box, would they?
And you certainly can't use and launch Outlook on the end user's client side machine. I mean, it would be really cool then if you come to my web site to view a cat picture, and I can now use YOUR copy of outlook, or Excel. Hey, why not launch your banking application and steal all that information? Or search your local hard drive for a Excel sheet called passwords? If you think about this, if that was possible, then ZERO people would EVER risk using the internet, right?
so, you have ZERO ability to use or mess with MY computer. You can't even search or look for a file on MY computer. It is my computer, and if just going to some web site could let you mess IN ANY WAY with my computer would be the worlds largest security hole.
You can certainly put up a nice big text box, even a send to box as a web form or web page. But sending that email will be based on YOUR own email settings and account that you setup on the web site. I mean, you think that YOUR web site is going to use MY email system and email password and information? Really? So I can now use YOUR software on YOUR computer from my web site? You really have to stop and think about what you asking? As noted, if you think about this, then you will well realize that this type of approach to web software is not possible, but it should ALSO be VERY obvious that such operations with web software could not be possible, since if it was, then no one would ever risk or even consider using the internet to do anything.
Even when you use a up-load file control, you can NEVER EVER even set the file to be picked for up-load - only the end user can do that. Your server side software cannot touch, see, know ANYTHING about the client side computer. Besides, that client computer might be a iPad, a smartphone, or your Apple Mac computer anyway - you have no idea, can't have any idea, and it can't matter.
so, client side software and files are 100% hands off. The end user has a browser sitting on their desktop. You can ONLY send it some HTML for that browser to then display.
And trying to use server side software? Well, if you did manage to do that, then on the server you would see outlook pop up on the computer. But the end user? They would not see anything - since you not able to see and use software.
It would be cool when you go to buy something on amazon, that you web browser lets YOU mess around with software on THEIR web server??
All you can do with a web system is create a web page and pass it down to the end user, and their browser will THEN render and display the markup. What you run on that web server (which is just a computer) is up to you, but you ONLY have the ability to pump out some HTML down to the users browser - nothing more, nothing less. Any windows program launched on the web server would only be seen if you were sitting at the console on and at the web server.
so, what would I do for a template? I would toss up a text box, or web form, and SAVE some template say to a database.
And if you REALLY smart you adopt ckedit, or a screen that allows you to enter markup. So, now you can drop in any graphics or fonts or whatever.
Say like this:
So, what I do with above is "save" my email templates into a database. I then have a simple routine that search/replace the fields like [Email] with the data from the database. So fonts, graphics etc? I can just edit and build and cut + paste in graphics.
But, I certainly can't really use outlook for this purpose. And the "sending" email address will have to be one for the web site - it can't be any old sending email.

Related

Trying to open Outlook using ASP.NET

I have created an application in ASP.NET using VB. I have written following code.
It is giving an error in localhost but when I am running it from my application, it is working fine.
I have checked DCOMCNFG --> Outlook/Office entry is not present.
Please advise me how to open Outlook with the attachment from my application at localhost/server?
ByVal recipients As List(Of String),
ByVal smtpAddress As String,
ByVal attachments As List(Of String))
' Create a new MailItem and set the To, Subject, and Body properties.
'Dim application As New Outlook.Application
Dim application As New Outlook.Application()
Threading.Thread.Sleep(2000)
Dim newMail As Outlook.MailItem = DirectCast(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
' Set up all the recipients.
For Each recipient In recipients
newMail.Recipients.Add(recipient)
Next
If newMail.Recipients.ResolveAll() Then
newMail.Subject = subject
newMail.Body = body
For Each attachment As String In attachments
newMail.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue)
Next
End If
newMail.Display(True)
You need to use any third-party components that can deal with .pst files without Outlook involved.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

COM Exception for client-side object called by Classic ASP page

I have tricky problem, which I'm struggling quite a bit with.
The current solution, consists of a Classic ASP site hosted on some Win2K3 server, that calls some Client-side DLL's on XP machines coded in VB6.
These client-side DLL's then again can call some other COM objects, in this particular case it calls IBM Louts Notes (Lotus Domino Objects 1.2).
Now for different reasons these DLL's has to be converted to .NET (still x86), at this stage, this is the only change to be done. This works quite well except for one piece of code which throws an error.
COMException when calling the Lotus Notes COM object
ASP script calling the DLL
Set objLotus = CreateObject("OpenLotusNotes_FU_v2.clsMain")
sRet = objLotus.OpenLotus_mail()
Client-side DLL
Dim session As NotesSession = New NotesSession() 'works well
Dim objNotesWrkSp As Object
objNotesWrkSp = Activator.CreateInstance(Type.GetTypeFromProgID("Notes.NotesUIWorkspace")) 'crashes
Exception
Retrieving the COM class factory for component with CLSID {29131502-2EED-1069-BF5D-
00DD011186B7} failed due to the following error: 80080005
Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
When I try to run this code in console application on the same computer, it works. So it has to be some permissions(?), I have tried changing basically everything I can think of.
Any help would be much appreciated!
Updated 01.09.2014
What I see is when I trigger the code from ASP, is that it creates a new process of Notes everytime, but only in the background, no UI what so ever. When I trigger the code from a console application, I get the Notes UI, which asks me for password, if I don't already have notes running.
I believe that I'm getting the Exception because it eventually times out.
Have a read of this article , your dealing with an Out Of Process COM component which may not initialize properly for some reason.
Another possibility is that the COM threading model is not supported in a free threaded component which .NET is by default compiled with. You can compile you DLL with a STA attribute, but as far am I'm aware that only effects console applications. You might find some additional information in this article from MS, if you have not already read it of course. Hope something there helps you solve your problem.
Consult with your admins first, but for this scenario you can set no password for the ID the Lotus Notes uses.
I did resolve this, so if anyone else would run in to this…
First of all configure the “Notes Link” Component Service to run as a “The interactive user” and that the process owner has permissions in "Launch and Activation Permissions" and "Access permissions".
Then this should be possible
Dim objNotesWrkSp As Object
Dim objWorkspace As Type = Type.GetTypeFromProgID("Notes.NotesUIWorkspace")
objNotesWrkSp = Activator.CreateInstance(objWorkspace)
As it turned out in this particular case I could only get it to work with late-binding, when I tried to this it just opened a conhost.exe process and then never responed:
Dim session as New NotesSession
session.Initialize()
Among other similar issues... So then I only used late-binding for all communication with Notes.
Dim mailServerPath, mailFile As String
objWorkspace.InvokeMember("OpenDatabase", Reflection.BindingFlags.InvokeMethod, Nothing, objNotesWrkSp, New Object() {mailServerPath, mailFile})
And so on...

Documents.Add fails on ASP.NET (VB.NET)

I am having an issue with opening a document using Microsoft Word from ASP.NET MVC.
This works perfectly on my developer machine, but not when deployed to IIS.
Dim word = New Microsoft.Office.Interop.Word.Application
'This line is failing to return a document object
Dim letter = word.Documents.Add(letter_doc_path)
'This line then fails due to [letter] being null
letter.MailMerge.OpenDataSource(csvPath)
I have added permissions in "Component Services" (dcomcnfg) to the NETWORK SERVICE user which allows the creation of the Word object in the first place, but I am completely stuck as what to do with this one.
I have also tried suppressing Word dialogs with the following line just in case
word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone
The issue isn't helped by not having an error (apart from the null object reference obviously) - maybe there's a way to query Word for a specific error message?
Word requires the normal.dot template file when opening any document, the problem was occurring because the IIS user didn't have anywhere to create the normal.dot so it was failing in the background.
This was fixed by setting the UserTemplate path for the newly created word instance (immediately after creating it).
The path must be writeable by the IIS user (NETWORK SERVICE in my case).
word.Options.DefaultFilePath(Microsoft.Office.Interop.Word.WdDefaultFilePath.wdUserTemplatesPath) = working_folder
So just for completeness, here's the original example with the winning line included:
Dim word = New Microsoft.Office.Interop.Word.Application
'this line fixed it
word.Options.DefaultFilePath(Microsoft.Office.Interop.Word.WdDefaultFilePath.wdUserTemplatesPath) = working_folder
Dim letter = word.Documents.Add(letter_doc_path)
I was having the same problem, and the settings that wheelibin suggested weren't enough to create documents using the NETWORK SERVICE account.
What I ended up doing is:
Create a user account for this
process to run under.
Login as the user and run Word (this
does various setup tasks in Word so
the application doesn't try putting
up modal dialogs when running as a
service).
Create a new application pool and set
the pool to run as the user account.
If you're using Windows
Authentication, and your server is
Windows 2003 (or 2000, presumably),
then this issue applies, and you
need to either change the SPN of the
server, which will break Windows
Authentication for any application
running under a different user
account, or you have to switch the
authentication provider over to NTLM
instead of Kerberos.
IIS 7 can use Kernel Mode Authentication to avoid the issue.
I am not sure how are you catching the errors.
Please take a look at the following pages if you find some clue from that.
Error while using Microsoft Office 2003 in web application
Error while calling MS-Word from ASP.NET
"There is insufficient memory or disk space. Save the document now" - Opening MS Word from ASP.NET

Programmatically open an email from a POP3 and extract an attachment

We have a vendor that sends CSV files as email attachments. These CSV files contain statuses that are imported into our application. I'm trying to automate the process end-to-end, but it currently depends on someone opening an email, saving the attachment to a server share, so the application can use the file.
Since I cannot convince the vendor to change their process, such as offering an FTP location or a Web Service, I'm stuck with trying to automate the existing process.
Does anyone know of a way to programmatically open an email from a POP3 account and extract an attachment? The preferred solution would reside on a Windows 2003 server, be written VB.NET and secure. The application can reside on the same server as the POP3 server, for example, we could setup the free POP3 server that comes with Windows Server and pull against the mail file stored on the file system.
BTW, we are willing to pay for an off-the-shelf solution, if one exists.
Note: I did look at this question but the answer points to a CodeProject solution that doesn't deal with attachments.
Try Mail.dll email component, it's very affordable, supports attachments national characters and is easy to use, it also supports SSL:
Using pop3 As New Pop3()
pop3.Connect("mail.server.com")
pop3.Login("user", "password")
Dim builder As New MailBuilder()
For Each uid As String In pop3.GetAll()
' Receive email message'
Dim mail As IMail = builder.CreateFromEml(pop3.GetMessageByUID(uid))
'Write out received message'
Console.WriteLine(mail.Subject)
'Here you can use mail.Attachmets collection'
For Each attachment As MimeData In mail.Attachments
Console.WriteLine(attachment.FileName)
attachment.Save("c:\" + attachment.FileName)
' you can also use attachment.Data here'
Next attachment
Next
pop3.Close(true)
End Using
You can download it here: http://www.lesnikowski.com/mail.
possible duplication of Reading Email using Pop3 in C#
Atleast, there's a shed load of suggestions there that you may find useful
I'll throw in a late suggestion for a more generalized "download POP3 messages and extract attachments" solution using existing software and minimal programming. I needed to do this for a client who switched to receiving faxes via email and was not pleased with manually saving the attachments to a location where they could be imported into an application.
For downloading messages on *nix systems fetchmail seems to be the standard and is very capable, but I chose mpop for both simplicity and Windows compatibility (but it is cross-platform). If mpop hadn't done the trick for me, I probably would have ended up doing something with the Python-based getmail, which was created when fetchmail's development stalled for a time (it's since resumed).
Mpop is controlled either via command line or configuration file, so I simply created multiple configuration files and specify via command line which file to load. I'm using it in "Exchange pickup directory" mode, which means it simply downloads the messages and drops them as text (.eml) files in a specified directory.
For extraction of the message attachments, UUDeview appears to be the standard (I'm using the Windows port of UUDeview) across just about any system you could want with just about any features you could want. My main alternative to this was a much-less-capable Python script that I'd developed for a different client back in 2007, but I'm happy to go with a precompiled executable over either installing Python or packaging with any of the Python-to-exe options.
Finally there's the configuration - along with the two mpop configuration files mentioned above (which I could do away with by using command-line options), I also have two 2-line .cmd files launched every 10 minutes by scheduled task - the first line to launch mpop to download into a working directory and the second line to launch UUDeview and extract attachments of specified types (.pdf or .tif) then delete each file from which it extracted attachments. Output is sent to another directory from which staff can directly attach files as needed.
This is overall not the most elegant way to reach these ends, but it was quick, simple, functional and reasonably robust - at each stage if something goes wrong it fails such that no data is lost. The only places where data could be lost are any non-attachment messages being sent to the dedicated fax email addresses, and even those will sit in the processing directory and be caught eventually.

Has anyone connected BizTalk with QuickBooks?

We use QuickBooks for financial management, and feed it from a variety of sources. I now need to hook it up to BizTalk, and I'd hate to reinvent the wheel. I've done searches, and as far as I can tell there's no QuickBooks adapter for BizTalk. Does anyone know of anything that'll do the job, preferably something that doesn't suck?
Doesn't the QB SDK require that Quickbooks be running on the client machine? Is there any way around it?
Quickbooks talks .NET quite easily. You'll need the QuickBooks SDK 7.0 and a copy of Visual Studio.NET, but after that it's very easy to do anything with Quickbooks.
Imports QBFC7Lib
Sub AttachToDB()
If isAttachedtoQB Then Exit Sub
Lasterror = "Unknown QuickBooks Error"
Try
QbSession = New QBSessionManager
QbSession.OpenConnection("", "Your Company Name")
QbSession.BeginSession("", ENOpenMode.omDontCare)
MsgReq = QbSession.CreateMsgSetRequest("UK", 6, 0)
MsgReq.Attributes.OnError = ENRqOnError.roeStop
Lasterror = ""
isAttachedtoQB = True
Catch e As Exception
If Not QbSession Is Nothing Then
QbSession.CloseConnection()
QbSession = Nothing
End If
isAttachedtoQB = False
Lasterror = "QuickBooks Connection Error. - " + e.Message + "."
End Try
End Sub
See http://developer.intuit.com/ for more information.
If you do build the integration code using .NET, you may want to consider leveraging the WCF Line-of-Business SDK:
http://www.microsoft.com/biztalk/technologies/wcflobadaptersdk.mspx
It's not a BizTalk-only technology, despite its categorization. The SDK is designed to make it easier to create a WCF channel to a LOB application, which can be consumed from almost any other platform.
Unfortunately it does. It also asks you to authorise any application you've built. (at least once.)
I don't know any way around it.
The QB SDK does not require that QuickBooks be running on the client machine. It does require that QuickBooks is installed on the client machine. You can access QuickBooks company files even if QuickBooks is not running though.
Have a look through the SDK docs. Additionally, when QuickBooks first prompts you to authorize the application, you need to make sure to tell it to allow access to the company file, even when QuickBooks isn't open/the company file isn't open.

Resources