powershell -NoProfile -ExecutionPolicy Unrestricted .\mail_rec.ps1 > log.txt.NET用メール送受信クラスライブラリ (TKMP.DLL) ▼ さくらインターネットのメールでテストしています mail_rec.ps1
$code = @"
using System;
using TKMP.Net;
using TKMP.Reader;
public class MyClass {
public static void mail_rec() {
//public class MyClass
//{
// public static void Main()
// {
ImapClient client = null;
BasicImapLogon logon = new BasicImapLogon("ユーザ@さくらユーザ.sakura.ne.jp", "パスワード");
client = new ImapClient(logon, "さくらユーザ.sakura.ne.jp", 993);
client.AuthenticationProtocol = AuthenticationProtocols.SSL;
try
{
if (!client.Connect())
{
Console.WriteLine("接続できませんでした");
return;
}
}
catch (Exception ex)
{
Console.WriteLine("接続エラーが発生しました" + ex.Message );
return;
}
// メールデータ一覧を格納するオブジェクト
IMailData[] mailData = client.GetMailList();
// データがありません
if (mailData == null)
{
Console.WriteLine("データがありません");
return;
}
int maxCount = 100;
int mailCounter = 0;
// メールデータの数
Console.WriteLine(mailData.Length);
mailCounter = mailData.Length;
if (mailCounter < maxCount)
{
maxCount = mailCounter;
}
// 読込み
int idx = 0;
// メールデータを表示するループ
foreach (var data in mailData)
{
idx++;
if (idx > maxCount)
{
break;
}
data.ReadBody();
Console.WriteLine("■■■【" + idx +"開始】------------------------");
// 本文無し( 本文が必要な場合は、false で、reader.MainText )
MailReader reader = new MailReader(data.DataStream, false);
if (reader.FileCount == 0)
{
Console.WriteLine("添付ファイルはありません");
}
//添付ファイルのコレクションを検査します
foreach (TKMP.Reader.File file in reader.FileCollection)
{
// 保存場所は事前に作成する必要があります
// (メールとの関連は、アプリケーション側で工夫する必要があります)
file.FileSave(@"c:\temp\data\");
}
// UID ( ユニークなメールのID )
// ▼ tkmp リファレンス : http://uwa.potetihouse.com/library/tkmp/document/html/P_TKMP_Net_MailData_Imap_UID.htm
// この値は各メールデータ毎にユニークであることをサーバーが保証しているので、既読メールの識別にはこの値を使用してください
Console.WriteLine("UID : " + data.UID);
// メールサイズ
Console.WriteLine("サイズ : " + data.Length);
// 本文( 本来はファイルかデータベースに書きだす )
Console.WriteLine("▼【本文】------------------------");
Console.WriteLine(reader.MainText);
Console.WriteLine("▲【本文】------------------------");
// ヘッダの一覧より、目的のヘッダを探す
// http://uwa.potetihouse.com/library/tkmp/document/html/AllMembers_T_TKMP_Reader_Header_HeaderString.htm
foreach (TKMP.Reader.Header.HeaderString headerdata in reader.HeaderCollection)
{
if ( headerdata.Name == "From" || headerdata.Name == "Subject" || headerdata.Name == "Date" ) {
if (headerdata.Name == "From") {
Console.WriteLine("【FROM】" + headerdata.Data);
}
if (headerdata.Name == "Subject") {
Console.WriteLine("【件名】" + headerdata.Data);
}
if (headerdata.Name == "Date") {
Console.WriteLine("【日付】" + headerdata.Data);
string target = headerdata.Data;
target = target.Replace(" (JST)", "");
target = target.Replace(" (PDT)", "");
try
{
DateTime dt = System.DateTime.ParseExact(target, "ddd, d MMM yyyy HH':'mm':'ss zzz", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None);
Console.WriteLine("【編集日付】" + string.Format("{0}", dt));
}
catch (Exception ex)
{
Console.WriteLine("【変換エラー】" + headerdata.Data);
Console.WriteLine(headerdata.Data + " はフォーマット変換できませんでした" + ex.Message);
}
}
}
else {
Console.WriteLine("【" + headerdata.Name + "】=>" + headerdata.Data);
}
}
Console.WriteLine("■■■【" + idx +"終了】------------------------");
Console.WriteLine("");
}
// 受信終了
client.Close();
}
}
"@
$path = "C:\user\dll"
Add-Type -Path ("${path}\TKMP.dll")
Add-Type -Language CSharp -TypeDefinition $code -ReferencedAssemblies ("${path}\TKMP.dll")
[MyClass]::mail_rec()
関連する記事 PowerShell2.0 : PowerShell 内で VBのコードを記述(TKMP.dllを使用)して、exe を作成した後実行してメールを送信する
|
|




























