Click here to Skip to main content
15,860,972 members
Home / Discussions / C#
   

C#

 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
Gerry Schmitz3-Sep-20 10:23
mveGerry Schmitz3-Sep-20 10:23 
QuestionConnect POST server call with HttpListener C# Pin
jdamiancabello2-Sep-20 0:21
jdamiancabello2-Sep-20 0:21 
AnswerRe: Connect POST server call with HttpListener C# Pin
Afzaal Ahmad Zeeshan3-Sep-20 5:19
professionalAfzaal Ahmad Zeeshan3-Sep-20 5:19 
QuestionInheritance problem Pin
Croccodillo19711-Sep-20 5:22
Croccodillo19711-Sep-20 5:22 
AnswerRe: Inheritance problem Pin
Richard Deeming1-Sep-20 7:26
mveRichard Deeming1-Sep-20 7:26 
GeneralRe: Inheritance problem Pin
Croccodillo19711-Sep-20 21:58
Croccodillo19711-Sep-20 21:58 
AnswerRe: Inheritance problem Pin
BillWoodruff9-Sep-20 21:16
professionalBillWoodruff9-Sep-20 21:16 
QuestionHow to reliably exit Outlook using Interop.Outlook? Pin
Server Automator30-Aug-20 14:43
professionalServer Automator30-Aug-20 14:43 
This is the scenario & I was able to make 1/2 of it work: Programmatically launch Outlook 365, send an message & quit Outlook; it must exit. The quitting is the problematic part-- it likes to keep running so I'm thinking I'm not releasing the resources fully forcing it to keep running.

Tons of samples to launch & send a simple message & this is not about that. Outlook 365 has to quit.

This works (simplified for this post though & should be accurate enough): Outlook launches, sends, quits---

using ol = Microsoft.Office.Interop.Outlook;
void Send(string Subject, string EmailBody, string BCC)
{
	ol.Application olOutlook = null;
	ol.MailItem olMsg = null;

	olOutlook = new ol.Application();    //new Outlook app
	olMsg = ( ol.MailItem )olOutlook.CreateItem( ol.OlItemType.olMailItem );
	olMsg.BodyFormat = ol.OlBodyFormat.olFormatHTML;
	olMsg.HTMLBody = EmailBody;
	olMsg.Subject = Subject;
	olMsg.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceNormal;
        olMsg.BCC = BCC;
		
         olMsg.Send();
					
	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olMsg );
	olMsg = null;

	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olOutlook.Session );
	olOutlook.Quit();
	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olOutlook.Application );
	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olOutlook );
	olOutlook = null;
	GC.WaitForPendingFinalizers();
	GC.Collect();
	GC.WaitForPendingFinalizers();
	GC.Collect();


Question part 1: Outlook does not quit whenever the EventHandler is subscribed to so the reminders window is suppressed. I'm adding & immediately removing the EventHandler because that's what I've narrowed it down to as the issue for not closing for me. Commenting out the 2 event lines is what's above btw.

using ol = Microsoft.Office.Interop.Outlook;
void Send(string Subject, string EmailBody, string BCC)
{
	ol.Application olOutlook = null;
        
        olOutlook.Reminders.BeforeReminderShow += Reminders_BeforeReminderShow;
        olOutlook.Reminders.BeforeReminderShow -= Reminders_BeforeReminderShow;

	ol.MailItem olMsg = null;

	olOutlook = new ol.Application();    //new Outlook app
	olMsg = ( ol.MailItem )olOutlook.CreateItem( ol.OlItemType.olMailItem );
	olMsg.BodyFormat = ol.OlBodyFormat.olFormatHTML;
	olMsg.HTMLBody = EmailBody;
	olMsg.Subject = Subject;
	olMsg.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceNormal;
        olMsg.BCC = BCC;
		
         olMsg.Send();
					
	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olMsg );
	olMsg = null;

	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olOutlook.Session );
	olOutlook.Quit();
	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olOutlook.Application );
	System.Runtime.InteropServices.Marshal.FinalReleaseComObject( olOutlook );
	olOutlook = null;
	GC.WaitForPendingFinalizers();
	GC.Collect();
	GC.WaitForPendingFinalizers();
	GC.Collect();


The EventHandler:
private void Reminders_BeforeReminderShow(ref bool Cancel)
{
    Cancel = true;  //suppresses Outlook reminders from opening
}


Part 2 of this question is where I am right now: Once I add an attachment & send the message, Outlook again does not quit.

What am I missing with releasing these resources? Why is adding & then removing this EventHandler causing issues with quitting Outlook? This is tricky because there are a gazillion "how to send mail" posts but much fewer "how to send mail & quit Outlook" posts.

Also, anything related to killing a task is not being considered because that's a dirty shutdown & will only cause deeper issues with mailboxes etc. over time, so don't post it as a solution.

Thank you

Smile | :)
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Gerry Schmitz31-Aug-20 4:50
mveGerry Schmitz31-Aug-20 4:50 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Dave Kreskowiak31-Aug-20 12:12
mveDave Kreskowiak31-Aug-20 12:12 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Mycroft Holmes31-Aug-20 12:15
professionalMycroft Holmes31-Aug-20 12:15 
Questionfind TIMES in string of text Pin
free-pizza30-Aug-20 13:52
free-pizza30-Aug-20 13:52 
AnswerRe: find TIMES in string of text Pin
Richard Andrew x6430-Aug-20 15:03
professionalRichard Andrew x6430-Aug-20 15:03 
GeneralRe: find TIMES in string of text Pin
free-pizza30-Aug-20 16:26
free-pizza30-Aug-20 16:26 
AnswerRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 19:47
mveOriginalGriff30-Aug-20 19:47 
JokeRe: find TIMES in string of text Pin
Peter_in_278030-Aug-20 19:59
professionalPeter_in_278030-Aug-20 19:59 
GeneralRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 20:08
mveOriginalGriff30-Aug-20 20:08 
GeneralRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 20:11
mveOriginalGriff30-Aug-20 20:11 
GeneralRe: find TIMES in string of text Pin
Peter_in_278030-Aug-20 20:43
professionalPeter_in_278030-Aug-20 20:43 
GeneralRe: find TIMES in string of text Pin
free-pizza31-Aug-20 3:52
free-pizza31-Aug-20 3:52 
GeneralRe: find TIMES in string of text Pin
OriginalGriff31-Aug-20 4:08
mveOriginalGriff31-Aug-20 4:08 
QuestionWinforms Inherited form - Resources Pin
MarkB12327-Aug-20 1:19
MarkB12327-Aug-20 1:19 
AnswerRe: Winforms Inherited form - Resources Pin
GenJerDan27-Aug-20 1:50
GenJerDan27-Aug-20 1:50 
GeneralRe: Winforms Inherited form - Resources Pin
MarkB12327-Aug-20 1:51
MarkB12327-Aug-20 1:51 
GeneralRe: Winforms Inherited form - Resources Pin
MarkB12327-Aug-20 2:41
MarkB12327-Aug-20 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.