-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1073.ps1
More file actions
369 lines (297 loc) · 11.2 KB
/
Copy path1073.ps1
File metadata and controls
369 lines (297 loc) · 11.2 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
##############################################################################
#.AUTHOR
# Josh Einstein
# Einstein Technologies, LLC
##############################################################################
##############################################################################
#.SYNOPSIS
# Opens a new mail message window (without actually sending it) with the
# To/CC/BCC/Subject/Body pre-filled with optional values and optional file
# attachments added to the message.
#
#.DESCRIPTION
# This function uses your default email client to display a new message
# window with the specified file(s) attached and a pre-filled subject and
# message body.
#
#.PARAMETER Path
# Specifies the path to one or more attachments to add. Wildcards are permitted.
#
#.PARAMETER LiteralPath
# Specifies the path to an item. Unlike Path, the value of LiteralPath is
# used exactly as it is typed. No characters are interpreted as wildcards.
# If the path includes escape characters, enclose it in single quotation marks.
# Single quotation marks tell Windows PowerShell not to interpret any
# characters as escape sequences.
#
#.PARAMETER To
# Specifies one or more recipients on the To line.
#
#.PARAMETER CC
# Specifies one or more recipients on the CC line.
#
#.PARAMETER BCC
# Specifies one or more recipients on the BCC line.
#
#.PARAMETER Subject
# The subject of the message.
#
#.PARAMETER Body
# The body of the message.
#
#.EXAMPLE
# Get-ChildItem C:\temp\*.txt | Send-MAPI -To josheinstein@hotmail.com
##############################################################################
function Send-MAPI {
[CmdletBinding(DefaultParameterSetName='Path')]
param(
[Parameter(ParameterSetName='Path', Position=1, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$Path,
[Alias("PSPath")]
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$LiteralPath,
[Parameter()]
[String[]]$To,
[Parameter()]
[String[]]$CC,
[Parameter()]
[String[]]$BCC,
[Parameter()]
[String]$Subject,
[Parameter()]
[String]$Body
)
begin {
$Message = New-Object MAPI.Message
$Message.AddRecipient($To)
$Message.AddCC($CC)
$Message.AddBCC($BCC)
$Message.Subject = $Subject
$Message.Body = $Body
}
process {
switch ($PSCmdlet.ParameterSetName) {
Path { $ResolvedPaths = @(Resolve-Path -Path:$Path) }
LiteralPath { $ResolvedPaths = @(Resolve-Path -LiteralPath:$LiteralPath) }
}
foreach ($ResolvedPath in $ResolvedPaths) {
Write-Verbose "$($PSCmdlet.MyInvocation.MyCommand.Name) Processing $ResolvedPath"
$Message.Attachments.Add($ResolvedPath.ProviderPath)
}
}
end {
if ($Message.Attachments.Count) {
if (-not $Message.Subject) { $Message.Subject = "Emailing: $($Message.Attachments | %{ [IO.Path]::GetFileName($_) })" }
if (-not $Message.Body) {
$Message.Body = @"
Your message is ready to be sent with the following file or link attachments:
$(($Message.Attachments | %{ [IO.Path]::GetFileName($_) }) -join "`n" )
Note: To protect against computer viruses, e-mail programs may prevent sending or receiving certain types of file attachments. Check your e-mail security settings to determine how attachments are handled.
"@
}
}
$Message.Show()
}
}
Add-Type @"
// Credit goes to David M Brooks
// http://www.codeproject.com/KB/IP/SendFileToNET.aspx
using System;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Threading;
namespace MAPI
{
public sealed class Message
{
public Message() {
Attachments = new List<string>();
}
public string Subject;
public string Body;
public List<string> Attachments;
public void AddRecipient(string[] email)
{
if (email == null || email.Length == 0) return;
foreach ( string e in email) {
AddRecipient(e, HowTo.MAPI_TO);
}
}
public void AddCC(string[] email)
{
if (email == null || email.Length == 0) return;
foreach ( string e in email ) {
AddRecipient(e, HowTo.MAPI_TO);
}
}
public void AddBCC(string[] email)
{
if (email == null || email.Length == 0) return;
foreach ( string e in email ) {
AddRecipient(e, HowTo.MAPI_TO);
}
}
public void Show() {
Thread t = new Thread((ThreadStart)delegate {
SendMail(Subject, Body, MAPI_LOGON_UI | MAPI_DIALOG);
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public void Send() {
Thread t = new Thread((ThreadStart)delegate {
SendMail(Subject, Body, MAPI_LOGON_UI);
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
[DllImport("MAPI32.DLL")]
static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);
int SendMail(string strSubject, string strBody, int how)
{
MapiMessage msg = new MapiMessage();
msg.subject = strSubject;
msg.noteText = strBody;
msg.recips = GetRecipients(out msg.recipCount);
msg.files = GetAttachments(out msg.fileCount);
m_lastError = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, how, 0);
if (m_lastError > 1)
throw new Exception("MAPISendMail failed! " + GetLastError());
Cleanup(ref msg);
return m_lastError;
}
bool AddRecipient(string email, HowTo howTo)
{
MapiRecipDesc recipient = new MapiRecipDesc();
recipient.recipClass = (int)howTo;
recipient.name = email;
m_recipients.Add(recipient);
return true;
}
IntPtr GetRecipients(out int recipCount)
{
recipCount = 0;
if (m_recipients.Count == 0)
return IntPtr.Zero;
int size = Marshal.SizeOf(typeof(MapiRecipDesc));
IntPtr intPtr = Marshal.AllocHGlobal(m_recipients.Count * size);
int ptr = (int)intPtr;
foreach (MapiRecipDesc mapiDesc in m_recipients)
{
Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);
ptr += size;
}
recipCount = m_recipients.Count;
return intPtr;
}
IntPtr GetAttachments(out int fileCount)
{
fileCount = 0;
if (Attachments == null)
return IntPtr.Zero;
if ((Attachments.Count <= 0) || (Attachments.Count > maxAttachments))
return IntPtr.Zero;
int size = Marshal.SizeOf(typeof(MapiFileDesc));
IntPtr intPtr = Marshal.AllocHGlobal(Attachments.Count * size);
MapiFileDesc mapiFileDesc = new MapiFileDesc();
mapiFileDesc.position = -1;
int ptr = (int)intPtr;
foreach (string strAttachment in Attachments)
{
mapiFileDesc.name = Path.GetFileName(strAttachment);
mapiFileDesc.path = strAttachment;
Marshal.StructureToPtr(mapiFileDesc, (IntPtr)ptr, false);
ptr += size;
}
fileCount = Attachments.Count;
return intPtr;
}
void Cleanup(ref MapiMessage msg)
{
int size = Marshal.SizeOf(typeof(MapiRecipDesc));
int ptr = 0;
if (msg.recips != IntPtr.Zero)
{
ptr = (int)msg.recips;
for (int i = 0; i < msg.recipCount; i++)
{
Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiRecipDesc));
ptr += size;
}
Marshal.FreeHGlobal(msg.recips);
}
if (msg.files != IntPtr.Zero)
{
size = Marshal.SizeOf(typeof(MapiFileDesc));
ptr = (int)msg.files;
for (int i = 0; i < msg.fileCount; i++)
{
Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiFileDesc));
ptr += size;
}
Marshal.FreeHGlobal(msg.files);
}
m_lastError = 0;
}
public string GetLastError()
{
if (m_lastError <= 26)
return errors[ m_lastError ];
return "MAPI error [" + m_lastError.ToString() + "]";
}
readonly string[] errors = new string[] {
"OK [0]", "User abort [1]", "General MAPI failure [2]", "MAPI login failure [3]",
"Disk full [4]", "Insufficient memory [5]", "Access denied [6]", "-unknown- [7]",
"Too many sessions [8]", "Too many files were specified [9]", "Too many recipients were specified [10]", "A specified attachment was not found [11]",
"Attachment open failure [12]", "Attachment write failure [13]", "Unknown recipient [14]", "Bad recipient type [15]",
"No messages [16]", "Invalid message [17]", "Text too large [18]", "Invalid session [19]",
"Type not supported [20]", "A recipient was specified ambiguously [21]", "Message in use [22]", "Network failure [23]",
"Invalid edit fields [24]", "Invalid recipients [25]", "Not supported [26]"
};
List<MapiRecipDesc> m_recipients = new List<MapiRecipDesc>();
int m_lastError = 0;
const int MAPI_LOGON_UI = 0x00000001;
const int MAPI_DIALOG = 0x00000008;
const int maxAttachments = 20;
enum HowTo{MAPI_ORIG=0, MAPI_TO, MAPI_CC, MAPI_BCC};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private class MapiMessage
{
public int reserved;
public string subject;
public string noteText;
public string messageType;
public string dateReceived;
public string conversationID;
public int flags;
public IntPtr originator;
public int recipCount;
public IntPtr recips;
public int fileCount;
public IntPtr files;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private class MapiFileDesc
{
public int reserved;
public int flags;
public int position;
public string path;
public string name;
public IntPtr type;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private class MapiRecipDesc
{
public int reserved;
public int recipClass;
public string name;
public string address;
public int eIDSize;
public IntPtr entryID;
}
}
}
"@