使用 powershell 将附件附加到日历事件

问题描述 投票:0回答:1

我正在使用 powershell 创建日历事件。理想情况下,如果可能的话,我想避免使用模块,因为我觉得我想要做的事情是很有可能的,但如果模块是我最好的选择,我愿意尝试它。

以下是我正在努力完成的几件事。

  1. 附上日历邀请
  2. 理想情况下,我想要一封 HTML 或 RTF 电子邮件,这样我就可以很好地格式化文本正文。
  3. 我想将 2 个文件附加到包含日历邀请的电子邮件正文中。

这是我遇到的障碍。我能够完成 1 和 2,一切都运行良好,但如果我尝试向文件添加附件,它要么会破坏日历,要么不会附加文件。

这是我尝试过的。 如果我尝试将文件作为邮件附件附加,请参阅下面的示例(这会将文件附加到邮件,但会破坏我的日历)

$attachment = New-Object System.Net.Mail.Attachment -ArgumentList $File1
$Mail.Attachments.Add($attachment)

如果我尝试对电子邮件中的附件进行编码并将变量传递到我的字符串生成器中并以这种方式附加它,它似乎无法正常工作(无附件),但日历似乎仍然可以正常发送。

这是我正在使用的示例:

CLS

$Path = (Split-Path $script:MyInvocation.MyCommand.Path)

$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.Host = "SMTP.domain.com"

$Mail = New-Object System.Net.Mail.MailMessage

$Mail.From = "[email protected]"
$Mail.To.Add("[email protected]")
$Mail.IsBodyHtml = $true

#====================== Files we will be attaching to our Calendar invite

if($NULL -eq $File1)
{
    $File1 = "$Path\File1.docx"
}
if($NULL -eq $File2)
{
    $File2 = "$Path\File2.xlsx"
}

#We will encode our files above into the calendar invite

if($NULL -eq $EncFile1)
{
    $EncFile1 = [convert]::ToBase64String((Get-Content -path "$File1" -Encoding byte))
}

if($NULL -eq $EncFile2)
{
    $EncFile2 = [convert]::ToBase64String((Get-Content -path "$File2" -Encoding byte))
}

#======================

$Body = "<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=us-ascii'><meta name=Generator content='Microsoft Word 15 (filtered medium)'><style><!--
/* Font Definitions */
@font-face
    {font-family:'Cambria Math';
    panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0in;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:'Calibri',sans-serif;}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:#0563C1;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:#954F72;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:'Calibri',sans-serif;
    color:windowtext;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-family:'Calibri',sans-serif;}
@page WordSection1
    {size:8.5in 11.0in;
    margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext='edit' spidmax='1026' />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext='edit'>
<o:idmap v:ext='edit' data='1' />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link='#0563C1' vlink='#954F72'><div class=WordSection1><p class=MsoNormal><b>Header:<o:p></o:p></b></p><p class=MsoNormal>This is a test<o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><b>HELLO WORLD!!!!<o:p></o:p></b></p><p class=MsoNormal><o:p>&nbsp;</o:p></p></div></body></html>"

$Mail.Body = $Body
$Mail.Subject = "This is just a test"

#String Builder Stuff
$sb = [System.Text.StringBuilder]::new()
$sb.AppendLine("BEGIN:VCALENDAR") | Out-Null
$sb.AppendLine("PRODID:-//Schedule a Meeting") | Out-Null
$sb.AppendLine("VERSION:2.0") | Out-Null
$sb.AppendLine("METHOD:REQUEST") | Out-Null

#Configuration for correct Timezone
$sb.AppendLine("BEGIN:VTIMEZONE") | Out-Null
$sb.AppendLine("TZID:Central Standard Time") | Out-Null
$sb.AppendLine("BEGIN:STANDARD") | Out-Null
$sb.AppendLine("DTSTART:16011104T020000") | Out-Null
$sb.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11") | Out-Null
$sb.AppendLine("TZOFFSETFROM:-0500") | Out-Null
$sb.AppendLine("TZOFFSETTO:-0600") | Out-Null
$sb.AppendLine("END:STANDARD") | Out-Null
$sb.AppendLine("BEGIN:DAYLIGHT") | Out-Null
$sb.AppendLine("DTSTART:16010311T020000") | Out-Null
$sb.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3") | Out-Null
$sb.AppendLine("TZOFFSETFROM:-0600") | Out-Null
$sb.AppendLine("TZOFFSETTO:-0500") | Out-Null
$sb.AppendLine("END:DAYLIGHT") | Out-Null
$sb.AppendLine("END:VTIMEZONE") | Out-Null

$sb.AppendLine("BEGIN:VEVENT") | Out-Null

#Attachments to the calendar invite
$sb.AppendLine("ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=File1.docx:$EncFile1") | Out-Null
$sb.AppendLine("ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=File2.xlsx:$EncFile2") | Out-Null

$sb.AppendLine([String]::Format("DTSTART;TZID='Central Standard Time':{0:yyyyMMddTHHmmssZ}",[datetime]::Now.AddMinutes(30))) | Out-Null
$sb.AppendLine([String]::Format("DTSTAMP;TZID='Central Standard Time':{0:yyyyMMddTHHmmssZ}", [datetime]::Now)) | Out-Null
$sb.AppendLine([String]::Format("DTEND;TZID='Central Standard Time':{0:yyyyMMddTHHmmssZ}", [datetime]::Now.AddMinutes(60))) | Out-Null
$sb.AppendLine("LOCATION: " + "Secret Boardroom") | Out-Null
$sb.AppendLine([String]::Format("UID:{0}", $(New-Guid).Guid)) | Out-Null
$sb.AppendLine([String]::Format("DESCRIPTION:{0}", $Mail.Body)) | Out-Null
$sb.AppendLine([String]::Format("X-ALT-DESC;FMTTYPE=text/html:{0}", $Mail.Body)) | Out-Null
$sb.AppendLine([String]::Format("SUMMARY:{0}", $Mail.Subject)) | Out-Null
$sb.AppendLine([String]::Format("ORGANIZER:MAILTO:{0}", $Mail.From.Address)) | Out-Null

$sb.AppendLine([String]::Format("ATTENDEE;RSVP=TRUE:mailto:{0}", $Mail.To[0].Address)) | Out-Null

$sb.AppendLine("BEGIN:VALARM") | Out-Null
$sb.AppendLine("TRIGGER:-PT15M") | Out-Null
$sb.AppendLine("ACTION:DISPLAY") | Out-Null
$sb.AppendLine("DESCRIPTION:Reminder") | Out-Null
$sb.AppendLine("END:VALARM") | Out-Null
$sb.AppendLine("END:VEVENT") | Out-Null
$sb.AppendLine("END:VCALENDAR") | Out-Null

#Configure mimetype to support our Calendar within the body of the email
$contype = New-Object System.Net.Mime.ContentType("text/calendar")
$contype.Parameters.Add("method", "REQUEST")
$contype.Parameters.Add("name", "Test.ics");
$avCal = [Net.Mail.AlternateView]::CreateAlternateViewFromString($sb.ToString(),$contype)

#Configure mimetype to support HTML in the body of the email
$EnableHTML = New-Object System.Net.Mime.ContentType("text/html")
$aHTML = [Net.Mail.AlternateView]::CreateAlternateViewFromString($Body.ToString(),$EnableHTML)

$Mail.AlternateViews.Add($aHTML)
$Mail.AlternateViews.Add($avCal)

#Send Email
$SmtpClient.Send($Mail)

#Clear resources
$Mail.Dispose()
$SmtpClient.Dispose()

任何帮助将不胜感激。希望我可以轻松添加 2 个文件附件,而不会破坏日历邀请或消息。 如果我必须放弃上面列出的任何功能(例如电子邮件正文中的 HTML),那么我可以接受。附件和日历是最重要的部分,但电子邮件正文中的格式设置将是“最好的”。

powershell email stringbuilder mailmessage system.net
1个回答
0
投票

找到了一种附加文件的方法。不确定是否有更好的方法,但我必须创建一个新的链接资源。以下是附加 xlsx 和 docx 文件的 2 个示例。

为了获取 mime 类型,我使用此链接作为参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types

$testFile2 = New-Object System.Net.Mail.LinkedResource($File2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
$testFile2.ContentId = "Test2"
$testFile2.ContentType.Name = "Test2.xlsx"

$testFile1 = New-Object System.Net.Mail.LinkedResource($File1, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
$testFile1.ContentId = "Test1"
$testFile1.ContentType.Name = "Test1.docx"

$aHTML.LinkedResources.Add($testFile1)
$aHTML.LinkedResources.Add($testFile2)
© www.soinside.com 2019 - 2024. All rights reserved.