How can I access to outlook folders which have MS Exchange Account - outlook

I have written the following code to access outlook folders.
MSOutlook.Application app = new MSOutlook.Application();
MSOutlook.NameSpace ns = app.GetNamespace("MAPI");
try
{
foreach (MSOutlook.Folder folder in ns.Folders)
{
...
}
}
catch (Exception ex)
{
...
}
but this code throw exception at ns.Folders and this exception means
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
However, I can get folders using the same code in the environment which has no exchange account.
How can I get the folders in the environment which has MS Exchange account?

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Outlook._NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder taskFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderTasks);
foreach (Microsoft.Office.Interop.Outlook.Folder folder in ns.Folders)
{
System.Diagnostics.Debug.WriteLine("Folder ----"+folder.Name.ToString());
}
This works for me fine..

Related

update profile image functionality is not working while hosting as jar

Hi I am new to Springboot I was trying to develop a application, One of its functionality is to upload profile Image. It was working fine in STS but when I pack it in jar and hosting it on AWS EC2 envirnment I am getting some error while processing that image
Error:
handler for profile picture:
#PostMapping("/process-contact")
public String processContact(#ModelAttribute Contact contact, #RequestParam("profileImage") MultipartFile file,
HttpSession session) {
try {
contact.setUser(user);
user.getContacts().add(contact);
// processing and uploading photo
if (file.isEmpty()) {
System.out.println("File is empty");
contact.setImage("contact.png");
} else {
//Processing Image
InputStream inputStream = file.getInputStream();
Path paths = Paths.get(new ClassPathResource("/static/img").getFile().getPath()+"/" +file.getOriginalFilename());
Files.copy(inputStream, paths, StandardCopyOption.REPLACE_EXISTING);
contact.setImage(file.getOriginalFilename());
}
// Success Message
session.setAttribute("message", new Message("Your contact is added...", "success"));
this.userRepository.save(user);
System.out.println("Successfully Added");
} catch (Exception E) {
E.printStackTrace();
// Failed message
session.setAttribute("message", new Message("Something went wrong "+E.getMessage(), "danger"));
}
return "normal/add_contact_form";
}
It is working fine in IDE after some research I found way of writing data in jar is diffrent could some please help me how can I implemenr it for jar also.
Thankyou
all you need to do is replace this line:
Path paths = Paths.get(new ClassPathResource("/static/img").getFile().getPath()+"/" +file.getOriginalFilename());
With:
Path paths = Paths.get(new FileSystemResource("/static/img").getFile().getPath()+"/" +file.getOriginalFilename());
THat will work like charm.

Microsoft.Office.Interop.Outlook does not work on web server but works on local machine

I am using below code in the button event, so that user can send mail through self machine outlook directly (nuget Microsoft. Office. Interop.Outlook). Code is working when I am debugging below code in my localhost and send mail from outlook. But problem is when I deployed the code into web server and browse through IE from my work station, mail not send through outlook.
This error message show in log:
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
How can I resolve this issue?
Web application reside into web server and users will access the application from IE and then they will send mail through self machine outlook.
public void SendEmailOutlook(string mailToRecipients, string mailCCRecipients, string subjectLine, [Optional] string attachments, string HTMLBody)
{
try
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Outlook.Recipients oRecips = oMsg.Recipients;
List<string> oTORecip = new List<string>();
List<string> oCCRecip = new List<string>();
var ToRecip = mailToRecipients.Split(',');
var CCRecip = mailCCRecipients.Split(',');
foreach (string ToRecipient in ToRecip)
{
oTORecip.Add(ToRecipient);
}
foreach (string CCRecipient in CCRecip)
{
oCCRecip.Add(CCRecipient);
}
foreach (string to in oTORecip)
{
Outlook.Recipient oTORecipt = oRecips.Add(to);
oTORecipt.Type = (int)Outlook.OlMailRecipientType.olTo;
oTORecipt.Resolve();
}
foreach (string cc in oCCRecip)
{
Outlook.Recipient oCCRecipt = oRecips.Add(cc);
oCCRecipt.Type = (int)Outlook.OlMailRecipientType.olCC;
oCCRecipt.Resolve();
}
oMsg.Subject = subjectLine;
if (attachments.Length > 0)
{
string sDisplayName = "MyAttachment";
int iPosition = 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
var Sendattachments = attachments.Split(',');
foreach (var attachment in Sendattachments)
{
Outlook.Attachment oAttach = oMsg.Attachments.Add(attachment, iAttachType, iPosition, sDisplayName);
}
}
if (HTMLBody.Length > 0)
{
oMsg.HTMLBody = HTMLBody;
}
oMsg.Save();
oMsg.Send();
oTORecip = null;
oCCRecip = null;
oMsg = null;
oApp = null;
}
catch (Exception e)
{
//print(e.Message);
}
}
Outlook, just like every other Office app, cannot be used from a service (such as IIS).
The Considerations for server-side Automation of Office article states the following:
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.
As a possible workaround you may consider using EWS or any other REST API (for example, Graph API) if you deal with Exchange server profiles only. See Explore the EWS Managed API, EWS, and web services in Exchange for more information.
I've had this issue too."Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))."
Server environment:Windows server 2019&iis
Local machine:Windows 10&iis
Tip:The Microsoft office doesn't support that use OutWork IIS or Asp.net
So,I give you right answer(It's worked):
1、Run "win+R" ,then inuput 'Dcomcnfg'
2、As this pic:
enter image description here

Outlook Redemption DLLs - Unable to delete emails in PST

I am using the below command to delete emails from PST.
foreach (Redemption.RDOMail oitem in filteredItems)
{
try
{
oitem.Delete();
}
catch (Exception ex)
{
PSTLog.Log("Exception in DeleteEmails: " + ex.Message);
}
}
The Redemption DLLs indicate the emails have been deleted successfully. If I try to read PST again using Redemption DLLs, I get lower email count which makes sense. However, I am still able to see the deleted emails in Outlook. Tried options like closing/reopening Outlook & detaching/re-attaching PST in Outlook, but it has not helped.
Is it possible Outlook is caching the results elsewhere and causing this discrepancy? Outlook version is 2016.
Any help would be appreciated!!
Do not use foreach loops if you are modifying the collection. Use a down "for" loop:
foreach ( int i = filteredItems.Count; i > 0; i--)
{
Redemption.RDOMail oitem = filteredItems[i];
try
{
oitem.Delete();
}
catch (Exception ex)
{
PSTLog.Log("Exception in DeleteEmails: " + ex.Message);
}
Marshal.ReleaseComObject(oitem);
}

UWP sideload app that use Microsoft.Office.Interop.Outlook COM

Hello Im updating a UWP app that we use in ouroffice.
Before when we use it to send email it uses the Windows.ApplicationModel.Email.EmailMessage however this i really limited. So i would like to use Microsoft.Office.Interop.Outlook instead to create the email directly with outlook
My test code looks like this.
try
{
List<string> lstAllRecipients = new List<string>();
//Below is hardcoded - can be replaced with db data
lstAllRecipients.Add("info#test.com");
//lstAllRecipients.Add("chandan.kumarpanda#testmail.com");
Outlook.Application outlookApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMailItem.GetInspector;
// Thread.Sleep(10000);
// Recipient
Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
foreach (String recipient in lstAllRecipients)
{
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
}
////Add CC
//Outlook.Recipient oCCRecip = oRecips.Add("THIYAGARAJAN.DURAIRAJAN#testmail.com");
//oCCRecip.Type = (int)Outlook.OlMailRecipientType.olCC;
//oCCRecip.Resolve();
//Add Subject
oMailItem.Subject = "Test Mail";
// body, bcc etc...
//Display the mailbox
oMailItem.Display(true);
}
catch (Exception objEx)
{
await new MessageDialog(objEx.Message, "Error email to Outlook").ShowAsync();
}
I have added the Microsoft.Office.Interop.Outlook.dll version to the app by finding it in C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Outlook\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Outlook.dll it is file version 15.0.4569.1507
My problem is when i run the code i get the following error
Creating an instance of the COM component with CLSID {0006F03A-0000-0000-C000-000000000046} using CoCreateInstanceFromApp failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). Please make sure your COM object is in the allowed list of CoCreateInstanceFromApp.
I guees i have to add a reference in the appxmanifest how ever i cannot figure out what.

DCom/OPC application does not connect when running in visual studio, but .exe connects perfectly

I'm writing an OPC client that connects to a remote server and reads data etc. I am using advosol's BGServer class. The issue is, when I run the program in visual studio I get the following error on adding a group.
"Exception from HRESULT: 0x80040202"
My problem is similar to (http://stackoverflow.com/questions/5978721/opc-server-access-remotely-using-opcda-net-tools), however, I know the DCom settings are configured correctly because when I run the same code by double clicking the .exe I connect and can add a group with no problems.
Therefore I'm guessing that visual studio is running under some strange user/group, and screws up the dcom permissions (mainly with callbacks).
edit: code
BGServer server;
private void Form1_Load(object sender, EventArgs e)
{
server = new BGServer(this);
server.Connect(new OPC.Common.Host() { HostName = "xp-devbox2", UserName = "OPCUser", Password = "OPCUser" }, "FactoryTalk Gateway", null, ServerConnected);
}
void ServerConnected(BGException ex, object tag)
{
if (ex != null)
{
label1.Text = ex.Message;
}
else
{
//we've connected to the server. let's start subscribing to stuff!
server.AddGroup("Tuner DataGroup", true, 1000, 0, null, null, new OnBGSrvAddGroup(GroupAdded));
}
}
private BGGroup dGroup;
void GroupAdded(BGException ex, BGGroup group, object tag)
{
if (ex != null)
{
label1.Text = ex.Message;
}
else label1.Text = "Group Added";
}
Are you using reg-free COM to communicate with the OPC? When you use the "Visual Studio Hosting Process" any external ".exe.manifest" files that you create the do reg-free COM will not be carried into the manifest of the vshost.exe process.

Resources