-
Notifications
You must be signed in to change notification settings - Fork 32
/
Program.cs
84 lines (69 loc) · 2.99 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Office.Interop.Outlook;
namespace Outlook_send_message
{
class Program
{
static Application app = new Application();
static string subject = "Subject";
static AutoResetEvent WaitForReply;
static string bodyEmail = "What ever first line\r\n" +
"Whatever Second Line \r\n" +
"And so on ...\r\n";
static string toEmail = "SourceEmail";
static void Main(string[] args)
{
WaitForReply = new AutoResetEvent(false);
//Use this to send with an attachment
/*
SendEmailWithAttachment(subject, toEmail, bodyEmail, new List<string> { @"C:\Files\attachment.doc" });
*/
//Sending the emails
SendEmail(subject, toEmail, bodyEmail);
//Waiting for a response
app.NewMailEx += App_NewMailEx;
//Wait until this thread is relased (which will happen when we receive the reply and delete it
WaitForReply.WaitOne();
}
private static void App_NewMailEx(string EntryIDCollection)
{
MailItem newMail = (MailItem)app.Session.GetItemFromID(EntryIDCollection, System.Reflection.Missing.Value);
//This is a cryptic way to get String.contains("") in case-insensitive way
if (newMail.Subject.IndexOf(subject, StringComparison.OrdinalIgnoreCase) >= 0)
{
newMail.Delete();
//Shut the application down, you can comment this if you want it to continue for deleting more replies
WaitForReply.Set();
}
}
public static void SendEmail(string subjectEmail,
string toEmail, string bodyEmail)
{
MailItem oMsg = app.CreateItem(OlItemType.olMailItem);
oMsg.DeleteAfterSubmit = true; //Delete the message from sent box
oMsg.Subject = subjectEmail;
oMsg.To = toEmail;
var ins = oMsg.GetInspector;
oMsg.HTMLBody = bodyEmail.Replace("\r\n", "<br />") + oMsg.HTMLBody; //Must append oMsg.HTMLBody to get the default signature
oMsg.Send();
}
public static void SendEmailWithAttachment(string subjectEmail, string toEmail, string bodyEmail, List<String> attachments)
{
MailItem oMsg = app.CreateItem(OlItemType.olMailItem);
oMsg.DeleteAfterSubmit = true;
oMsg.Subject = subjectEmail;
oMsg.To = toEmail;
var ins = oMsg.GetInspector;
foreach (var attachment in attachments)
{
oMsg.Attachments.Add(attachment, OlAttachmentType.olByValue);
}
oMsg.HTMLBody = bodyEmail.Replace("\r\n", "<br />") + oMsg.HTMLBody; //Must append oMsg.HTMLBody to get the default signature
oMsg.Send();
}
}
}