Initial commit – alle alten Commits entfernt
This commit is contained in:
180
MailSend_Lesebestätigung/Form1.cs
Normal file
180
MailSend_Lesebestätigung/Form1.cs
Normal file
@@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.DirectoryServices;
|
||||
using System.DirectoryServices.AccountManagement;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MailSend_Lesebestätigung
|
||||
{
|
||||
public partial class Form1 : FormEx
|
||||
{
|
||||
SynchronizationContext synccontext;
|
||||
|
||||
private string mail_path = $@"{Application.StartupPath}\Serienbriefe";
|
||||
private FHelp helpForm = new FHelp();
|
||||
|
||||
|
||||
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
Directory.CreateDirectory(mail_path);
|
||||
listViewEx1.DoubleBuffer(true);
|
||||
}
|
||||
|
||||
private void abtnAD_Click(object sender, EventArgs e)
|
||||
{
|
||||
synccontext = SynchronizationContext.Current;
|
||||
File.Delete("ad_users.csv");
|
||||
LoadADColumns();
|
||||
Task.Run(delegate
|
||||
{
|
||||
lADload.Loading("AD wird ausgelesen", "Fertig", () =>
|
||||
{
|
||||
foreach (UserPrincipal user in ActiveDirectory.GetUsers(new PrincipalContext(ContextType.Domain, "Domainname", Properties.Settings.Default.LDAPcontainer)))
|
||||
{
|
||||
if (user.Enabled != true || user.GetProperty("employeeID") == "")
|
||||
continue;
|
||||
string[] userinfo = Properties.Settings.Default.ADuserProps.Split(',').Select(prop => user.GetProperty(prop.Split(':')[1])).ToArray();
|
||||
//string[] userinfo = { user.GetProperty("givenname"), user.GetProperty("sn"), user.GetProperty("department"), user.EmailAddress, user.GetProperty("employeeID"), user.SamAccountName };
|
||||
File.AppendAllText("ad_users.csv", $@"{string.Join(";", userinfo)}{Environment.NewLine}");
|
||||
var item = new ListViewItemEx(userinfo[0]);
|
||||
item.SubItems.AddRange(userinfo.Skip(1).ToArray());
|
||||
listViewEx1.Invoke(new MethodInvoker(delegate { listViewEx1.Items.Add(item); }));
|
||||
}
|
||||
listViewEx1.Invoke(new MethodInvoker(delegate
|
||||
{
|
||||
listViewEx1.SortByColumn(1, SortOrder.Ascending);
|
||||
listViewEx1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void LoadADColumns()
|
||||
{
|
||||
listViewEx1.Clear();
|
||||
listViewEx1.Columns.AddRange(Properties.Settings.Default.ADuserProps.Split(',').Select(prop => new ColumnHeader() { Text = prop.Split(':')[0], Name = prop.Split(':')[1] }).ToArray());
|
||||
}
|
||||
|
||||
|
||||
private void LoadMailColumns()
|
||||
{
|
||||
listViewEx1.Clear();
|
||||
listViewEx1.Columns.Add("E-Mail");
|
||||
listViewEx1.Columns.Add("Dateiname");
|
||||
listViewEx1.Columns.Add("Fehler");
|
||||
}
|
||||
|
||||
async private void animationButton1_Click(object sender, EventArgs e)
|
||||
{
|
||||
await GetMails();
|
||||
foreach (ListViewItemEx item in listViewEx1.Items)
|
||||
{
|
||||
try
|
||||
{
|
||||
string mailaddress = item.Text;
|
||||
string filename = item.SubItems[1].Text;
|
||||
string fullfilename = $@"{mail_path}\{mailaddress}\{filename}";
|
||||
|
||||
var mail = new SendMail("[Mailserver]", 25, "[Mail-User]", "[Mail-User-Pass]");
|
||||
Attachment attachment = new Attachment(fullfilename, new ContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
|
||||
MailMessage mailmessage = new MailMessage(new MailAddress("[Mail-Adress]", "[From]"), new MailAddress(mailaddress)) { IsBodyHtml = true };
|
||||
mailmessage.Subject = "Bestätigung zur Einhaltung der Verhaltensregeln";
|
||||
|
||||
string mailbody = File.ReadAllText(Properties.Settings.Default.MailBody);
|
||||
|
||||
var user = ActiveDirectory.GetUser(new PrincipalContext(ContextType.Domain, "[Domainname], Properties.Settings.Default.LDAPcontainer), new MailAddress(mailaddress));
|
||||
|
||||
Properties.Settings.Default.MailBodyReplacements.Cast<string>().ToList().ForEach(replacement =>
|
||||
{
|
||||
mailbody = mailbody.Replace($"%{replacement.Split(':')[0]}%", user.GetProperty(replacement.Split(':')[1]));
|
||||
});
|
||||
|
||||
|
||||
mailmessage.Body = mailbody;
|
||||
//mailmessage.Body = File.ReadAllText(Properties.Settings.Default.MailBody).Replace("%UserName%", filename.Split(',')[0]);
|
||||
mailmessage.Attachments.Add(attachment);
|
||||
var send = mail.Send(mailmessage);
|
||||
if (send == MailSendStatus.Fail)
|
||||
{
|
||||
item.HeaderColor = Color.OrangeRed;
|
||||
item.SubItems.Add("Mail nicht versendet");
|
||||
}
|
||||
else
|
||||
{
|
||||
item.HeaderColor = Color.Honeydew;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
item.HeaderColor = Color.OrangeRed;
|
||||
item.SubItems.Add("Mail nicht versendet");
|
||||
File.WriteAllText("log.log", ex.ToString() + Environment.NewLine + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async private Task GetMails()
|
||||
{
|
||||
LoadMailColumns();
|
||||
var dir = new DirectoryInfo(mail_path);
|
||||
|
||||
foreach (var subdir in dir.GetDirectories())
|
||||
{
|
||||
FileInfo file = subdir.GetFiles()[0];
|
||||
var item = new ListViewItemEx(subdir.Name);
|
||||
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, file.Name) { Name = listViewEx1.Columns[item.SubItems.Count].Name });
|
||||
|
||||
listViewEx1.Invoke(new MethodInvoker(delegate
|
||||
{
|
||||
listViewEx1.Items.Add((ListViewItemEx)item);
|
||||
listViewEx1.EnsureVisible(listViewEx1.Items.Count - 1);
|
||||
}));
|
||||
|
||||
await Task.Delay(1);
|
||||
}
|
||||
listViewEx1.SortByColumn(1, SortOrder.Ascending);
|
||||
listViewEx1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void abtnPath_Click(object sender, EventArgs e)
|
||||
{
|
||||
Process.Start(mail_path);
|
||||
}
|
||||
|
||||
private void abtnHelp_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(helpForm.Visible == false)
|
||||
helpForm.Show();
|
||||
else
|
||||
helpForm.Hide();
|
||||
|
||||
}
|
||||
|
||||
async private void abtnShowMails_Click(object sender, EventArgs e)
|
||||
{
|
||||
await GetMails();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user