메일백업

This commit is contained in:
chi
2020-05-07 17:19:27 +09:00
parent ea20c93c27
commit f68c0a0ea4
18 changed files with 1289 additions and 438 deletions

View File

@@ -134,6 +134,12 @@
<Compile Include="Mail\fMailAutoAdd.Designer.cs">
<DependentUpon>fMailAutoAdd.cs</DependentUpon>
</Compile>
<Compile Include="Mail\fMailBackup.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Mail\fMailBackup.Designer.cs">
<DependentUpon>fMailBackup.cs</DependentUpon>
</Compile>
<Compile Include="Mail\fMailList.cs">
<SubType>Form</SubType>
</Compile>
@@ -339,6 +345,9 @@
<EmbeddedResource Include="Mail\fMailAutoAdd.resx">
<DependentUpon>fMailAutoAdd.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Mail\fMailBackup.resx">
<DependentUpon>fMailBackup.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Mail\fMailList.resx">
<DependentUpon>fMailList.cs</DependentUpon>
</EmbeddedResource>

View File

@@ -0,0 +1,217 @@
namespace FCM0000.Mail
{
partial class fMailBackup
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(436, 50);
this.button1.TabIndex = 0;
this.button1.Text = "실행!";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(71, 8);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(178, 21);
this.dateTimePicker1.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(36, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 2;
this.label1.Text = "범위";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(277, 12);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(14, 12);
this.label2.TabIndex = 2;
this.label2.Text = "~";
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(329, 8);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.Size = new System.Drawing.Size(178, 21);
this.dateTimePicker2.TabIndex = 1;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(71, 146);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(220, 16);
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "파일이 존재하는 경우 덮어씌웁니다.";
this.checkBox1.UseVisualStyleBackColor = true;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 37);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 12);
this.label3.TabIndex = 2;
this.label3.Text = "백업대상";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 33);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(436, 21);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "받은 편지함";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(71, 60);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(436, 21);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "지운 편지함,동기화 문제";
this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 64);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 5;
this.label4.Text = "제외대상";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(71, 87);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(436, 21);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "C:\\temp\\mailbak";
this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(12, 91);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(57, 12);
this.label5.TabIndex = 7;
this.label5.Text = "백업 위치";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(71, 114);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(436, 21);
this.textBox4.TabIndex = 10;
this.textBox4.Text = "{folder}\\{yyyy}\\{mm}\\{dd}_{sender}_{subject}_{datetime}.msg";
this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(12, 118);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(57, 12);
this.label6.TabIndex = 9;
this.label6.Text = "파일 규칙";
//
// fMailBackup
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(558, 226);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.label6);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label1);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "fMailBackup";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Outlook Mail backup";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label6;
}
}

View File

@@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetOffice;
using Outlook = NetOffice.OutlookApi;
using NetOffice.OutlookApi.Enums;
using NetOffice.OutlookApi;
namespace FCM0000.Mail
{
public partial class fMailBackup : Form
{
public fMailBackup()
{
InitializeComponent();
dateTimePicker1.Value = DateTime.Now.AddYears(-2);
dateTimePicker2.Value = DateTime.Now.AddYears(-1);
}
int cntp = 0;
int cnte = 0;
int cntb = 0;
List<string> okfolder;
List<string> nofolder;
private void button1_Click(object sender, EventArgs e)
{
this.okfolder = new List<string>();
this.nofolder = new List<string>();
textBox1.Text = textBox1.Text.Trim();
textBox2.Text = textBox2.Text.Trim();
var buf1 = textBox1.Text.Split(',');
var buf2 = textBox2.Text.Split(',');
foreach(var item in buf1)
{
if (string.IsNullOrEmpty(item)) continue;
if(okfolder.IndexOf(item.ToLower()) == -1) okfolder.Add(item.ToLower());
}
foreach (var item in buf2)
{
if (string.IsNullOrEmpty(item)) continue;
if (nofolder.IndexOf(item.ToLower()) == -1) nofolder.Add(item.ToLower());
}
if (FCOMMON.Util.MsgQ("진짜?") != DialogResult.Yes) return;
int cnt = 0;
var fd = new FolderBrowserDialog();
fd.SelectedPath = this.textBox3.Text;
if (fd.ShowDialog() != DialogResult.OK) return;
var path = fd.SelectedPath;
cntp = 0;
cntb = 0;
cnte = 0;
Outlook.Application oApp = new Outlook.Application();
var oNS = oApp.GetNamespace("mapi");
foreach (MAPIFolder folder in oNS.Folders)
{
if (folder.DefaultItemType != OlItemType.olMailItem) continue;
folderbak(path, folder);
}
this.button1.Text = "저장된 메일 " + cntb.ToString() + "건/" + cntp.ToString() + "건 처리";
this.button1.Refresh();
FCOMMON.Util.MsgI("백업 성공");
//get first message
//var newmail = ol.CreateItem(OlItemType.olMailItem) as Outlook.MailItem;
//newmail.Display();
//ol.
//foreach (var to in tolist)
//{
// if (to.isEmpty()) continue;
// var newMail = ol.CreateItem(OlItemType.olMailItem) as Outlook.MailItem;
// newMail.Display();
// newMail.Subject = this.tbSubject.Text.Trim(); // dr.title;
// newMail.To = to;
// newMail.CC = tbCC.Text.Trim();
// newMail.BCC = tbBCC.Text.Trim();
// // newMail.BodyFormat = OlBodyFormat.olFormatHTML;
// newMail.HTMLBody = this.tbBody.Html
// .Replace("{USER}", FCOMMON.info.Login.nameK)
// .Replace("{EUSER}", FCOMMON.info.Login.nameE)
// .Replace("{EMAIL}", FCOMMON.info.Login.email)
// .Replace("%7BEMAIL%7D", FCOMMON.info.Login.email)
// .Replace("{HP}", FCOMMON.info.Login.hp)
// .Replace("{TEL}", FCOMMON.info.Login.tel)
// .Replace("{ITEM}", tbSubject.Text) + newMail.HTMLBody;
//}
}
void folderbak(string basepatth, MAPIFolder folder)
{
if (folder.Folders.Count > 0)
{
foreach (var fold in folder.Folders)
{
folderbak(basepatth, fold);
}
}
else
{
//폴더명이 지정되었다면 그곳에 포함되어있는지 체크한다.
if(okfolder.Count > 0)
{
if (okfolder.Contains(folder.Name.ToLower()) == false)
{
return;
}
}
if(nofolder.Count > 0)
{
if (nofolder.Contains(folder.Name.ToLower()) == true)
{
return;
}
}
var oItems = folder.Items;
var path = basepatth;// + "\\" + folder.FolderPath.Replace(@"\\", "");
foreach (object miitem in oItems)
{
var mi = miitem as MailItem;
if (mi == null) continue;
cntp += 1;
if (cntp % 10 == 0)
{
this.button1.Text = "저장된 메일 " + cntb.ToString() + "건/" + cntp.ToString() + "건 처리";
this.button1.Refresh();
}
if (mi.Subject.StartsWith("회수:")) continue;
//var mailitem = oItems.GetFirst() as Outlook.MailItem;
//var nowitem = ol.ActiveInspector();
//var mailitem = nowitem.CurrentItem as Outlook.MailItem;
var sd = DateTime.Parse(dateTimePicker1.Value.ToString("yyyy-MM-dd 00:00:00"));
var ed = DateTime.Parse(dateTimePicker2.Value.ToString("yyyy-MM-dd 23:59:59"));
//기간을 벗어나면 처리하지 않음
if (mi.CreationTime < sd || mi.CreationTime > ed) continue;
var subject = mi.Subject.Replace("\t", "").Replace("\r", "").Replace("\n", "").Replace("!", "").Replace("*", "").Replace("\"", "").Replace("@", "").Replace("#", "").Replace("^", "").Replace("&", "").Replace(">", "").Replace("<", "").Replace("/", "").Replace(":", "").Replace(" ", "").Replace("\\", "").Replace("?", "").Replace("%", "").Replace("_", "").Replace(".", "");
var filename = textBox4.Text.Trim();
filename = filename.Replace("{yyyy}", mi.CreationTime.Year.ToString("0000"));
filename = filename.Replace("{mm}", mi.CreationTime.Month.ToString("00"));
filename = filename.Replace("{dd}", mi.CreationTime.Day.ToString("00"));
filename = filename.Replace("{datetime}", mi.CreationTime.ToString("yyyyMMddHHmmss"));
filename = filename.Replace("{sender}", mi.SentOnBehalfOfName);
filename = filename.Replace("{subject}", subject);
filename = filename.Replace("{messageid}", mi.ConversationID);
filename = filename.Replace("{folder}", folder.FolderPath.Replace(@"\\", ""));
//filename = string.Format("{0}\\{1}\\{2}_{5}_{6}_{3}.msg",
// mi.CreationTime.Year.ToString("000"),
// mi.CreationTime.Month.ToString("00"),
// mi.CreationTime.Day.ToString("00"),
// mi.CreationTime.ToString("yyyyMMddHHmmss"),
// mi.ConversationID,
// mi.SentOnBehalfOfName,
// mi.Subject.Replace("\t", "").Replace("\r", "").Replace("\n", "").Replace("!", "").Replace("*", "").Replace("\"", "").Replace("@", "").Replace("#", "").Replace("^", "").Replace("&", "").Replace(">", "").Replace("<", "").Replace("/", "").Replace(":", "").Replace(" ", "").Replace("\\", "").Replace("?", "").Replace("%", "").Replace("_", "").Replace(".", ""));
var finame = System.IO.Path.Combine(path, filename);
var fi = new System.IO.FileInfo(finame);
if (fi.Directory.Exists == false) fi.Directory.Create();
if ( checkBox1.Checked || fi.Exists == false)
{
try
{
cntb += 1;
mi.SaveAs(fi.FullName);
}
catch (System.Exception ex)
{
Console.WriteLine("mail save err:" + ex.Message);
}
}
}
}
}
}
}

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>