Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace ConsoleApp2
- {
- public enum BodyType
- {
- PlainText,
- RTF,
- HTML
- }
- #pragma warning disable CS0618 // Typ oder Element ist veraltet
- class Program
- {
- static void Main(string[] args)
- {
- const string filepath = @"C:\Robinsonfile\";
- const string configFile = @"C:\Robinsonfile\config.txt";
- const string emailTo = "data_puller@outlook.com";
- const string title = "test";
- const string sCc = "";
- const string content = "";
- if (File.Exists(@"C:\Robinsonfile\config.txt") != true)
- {
- ConfigDoesNotExist(filepath, configFile, emailTo, sCc, title, content);
- }
- else
- {
- ConfigExists(filepath, configFile, emailTo, sCc, title, content);
- }
- }
- private static void ConfigExists(string filepath, string configFile, string emailTo, string sCc, string title,
- string content)
- {
- System.IO.Directory.CreateDirectory(filepath);
- //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- string[] config = readConfigFile(configFile);
- if (config[0] == "true")
- {
- config[0] = "false";
- if (config.Length == 1)
- {
- string[] data = new string[2];
- data[0] = config[0];
- Console.WriteLine("Bitte Email angeben und mit [Enter] bestätigen");
- data[1] = Console.ReadLine();
- sendMailAndSaveResponse(data, emailTo, sCc, title, content, filepath);
- File.WriteAllLines(configFile, data);
- }
- else
- {
- File.WriteAllLines(configFile, config);
- sendMailAndSaveResponse(config, emailTo, sCc, title, content, filepath);
- }
- }
- else
- {
- if (sendEmailViaOutlook(config[1], emailTo, sCc, title, content, BodyType.PlainText))
- {
- Console.WriteLine("Email wurde versandt");
- bool search = readMailsAndSaveContent(filepath);
- while (!search)
- {
- search = readMailsAndSaveContent(filepath);
- System.Threading.Thread.Sleep(1000 * 60);
- }
- }
- }
- }
- private static void ConfigDoesNotExist(string filepath, string configFile, string emailTo, string sCc, string title,
- string content)
- {
- CreateConfigFile(filepath, configFile);
- //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- string[] config = readConfigFile(configFile);
- if (config[0] == "true")
- {
- config[0] = "false";
- if (config.Length == 1)
- {
- var data = newData(config, configFile);
- sendMailAndSaveResponse(data, emailTo, sCc, title, content, filepath);
- File.WriteAllLines(configFile, data);
- }
- else
- {
- File.WriteAllLines(configFile, config);
- sendMailAndSaveResponse(config, emailTo, sCc, title, content, filepath);
- }
- }
- else
- {
- sendMailAndSaveResponse(config, emailTo, sCc, title, content, filepath);
- }
- }
- private static void CreateConfigFile(string filepath, string configFile)
- {
- Directory.CreateDirectory(filepath);
- File.CreateText(configFile).Close();
- File.WriteAllText(configFile, "true");
- }
- private static string[] newData(string[] config, string configFile)
- {
- string[] data = new string[2];
- data[0] = config[0];
- Console.WriteLine("Bitte Email angeben und mit [Enter] bestätigen");
- data[1] = Console.ReadLine();
- File.WriteAllLines(configFile, data);
- return data;
- }
- private static void sendMailAndSaveResponse(string[] data, string emailTo, string sCc, string title,
- string content,
- string filepath)
- {
- if (sendEmailViaOutlook(data[1], emailTo, sCc, title, content, BodyType.PlainText))
- {
- Console.WriteLine("Email wurde versandt");
- bool search = readMailsAndSaveContent(filepath);
- while (!search)
- {
- search = readMailsAndSaveContent(filepath);
- System.Threading.Thread.Sleep(1000 * 60);
- }
- }
- else
- {
- Console.WriteLine("Oh noes! Schreibe eine Email an Patrick Hentschel!");
- System.Threading.Thread.Sleep(1000 * 60);
- }
- }
- public static bool sendEmailViaOutlook(string sFromAddress, string sToAddress, string sCc, string sSubject,
- string sBody, BodyType bodyType, List<string> arrAttachments = null, string sBcc = null)
- {
- //Send email via Office Outlook 2010
- //'sFromAddress' = email address sending from (ex: "me@somewhere.com") -- this account must exist in Outlook. Only one email address is allowed!
- //'sToAddress' = email address sending to. Can be multiple. In that case separate with semicolons or commas. (ex: "recipient@gmail.com", or "recipient1@gmail.com; recipient2@gmail.com")
- //'sCc' = email address sending to as Carbon Copy option. Can be multiple. In that case separate with semicolons or commas. (ex: "recipient@gmail.com", or "recipient1@gmail.com; recipient2@gmail.com")
- //'sSubject' = email subject as plain text
- //'sBody' = email body. Type of data depends on 'bodyType'
- //'bodyType' = type of text in 'sBody': plain text, HTML or RTF
- //'arrAttachments' = if not null, must be a list of absolute file paths to attach to the email
- //'sBcc' = single email address to use as a Blind Carbon Copy, or null not to use
- //RETURN:
- // = true if success
- bool bRes = false;
- try
- {
- //Get Outlook COM objects
- Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
- Microsoft.Office.Interop.Outlook.MailItem newMail =
- (Microsoft.Office.Interop.Outlook.MailItem) app.CreateItem(Microsoft.Office.Interop.Outlook
- .OlItemType.olMailItem);
- //Parse 'sToAddress'
- if (!string.IsNullOrWhiteSpace(sToAddress))
- {
- string[] arrAddTos = sToAddress.Split(new char[] {';', ','});
- foreach (string strAddr in arrAddTos)
- {
- if (!string.IsNullOrWhiteSpace(strAddr) &&
- strAddr.IndexOf('@') != -1)
- {
- newMail.Recipients.Add(strAddr.Trim());
- }
- else
- throw new System.Exception("Bad to-address: " + sToAddress);
- }
- }
- else
- throw new System.Exception("Must specify to-address");
- //Parse 'sCc'
- if (!string.IsNullOrWhiteSpace(sCc))
- {
- string[] arrAddTos = sCc.Split(new char[] {';', ','});
- foreach (string strAddr in arrAddTos)
- {
- if (!string.IsNullOrWhiteSpace(strAddr) &&
- strAddr.IndexOf('@') != -1)
- {
- newMail.Recipients.Add(strAddr.Trim());
- }
- else
- throw new System.Exception("Bad CC-address: " + sCc);
- }
- }
- //Is BCC empty?
- if (!string.IsNullOrWhiteSpace(sBcc))
- {
- newMail.BCC = sBcc.Trim();
- }
- //Resolve all recepients
- if (!newMail.Recipients.ResolveAll())
- {
- throw new System.Exception("Failed to resolve all recipients: " + sToAddress + ";" + sCc);
- }
- //Set type of message
- switch (bodyType)
- {
- case BodyType.HTML:
- newMail.HTMLBody = sBody;
- break;
- case BodyType.RTF:
- newMail.RTFBody = sBody;
- break;
- case BodyType.PlainText:
- newMail.Body = sBody;
- break;
- default:
- throw new System.Exception("Bad email body type: " + bodyType);
- }
- if (arrAttachments != null)
- {
- //Add attachments
- foreach (string strPath in arrAttachments)
- {
- if (File.Exists(strPath))
- {
- newMail.Attachments.Add(strPath);
- }
- else
- throw new System.Exception("Attachment file is not found: \"" + strPath + "\"");
- }
- }
- //Add subject
- if (!string.IsNullOrWhiteSpace(sSubject))
- newMail.Subject = sSubject;
- Microsoft.Office.Interop.Outlook.Accounts accounts = app.Session.Accounts;
- Microsoft.Office.Interop.Outlook.Account acc = null;
- //Look for our account in the Outlook
- foreach (Microsoft.Office.Interop.Outlook.Account account in accounts)
- {
- if (account.SmtpAddress.Equals(sFromAddress, StringComparison.CurrentCultureIgnoreCase))
- {
- //Use it
- acc = account;
- break;
- }
- }
- //Did we get the account
- if (acc != null)
- {
- //Use this account to send the e-mail.
- newMail.SendUsingAccount = acc;
- //And send it
- ((Microsoft.Office.Interop.Outlook._MailItem) newMail).Send();
- //Done
- bRes = true;
- }
- else
- throw new System.Exception("Account does not exist in Outlook: " + sFromAddress);
- }
- catch (System.Exception ex)
- {
- Console.WriteLine("ERROR: Failed to send mail: " + ex.Message);
- }
- return bRes;
- }
- public static string[] readConfigFile(string filepath)
- {
- return System.IO.File.ReadAllLines(filepath);
- }
- public static bool readMailsAndSaveContent(string filepath)
- {
- Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
- Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = app.GetNamespace("MAPI");
- Microsoft.Office.Interop.Outlook.MAPIFolder myInbox =
- mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
- if (myInbox.Items.Count > 0)
- {
- String subject = "";
- foreach (Microsoft.Office.Interop.Outlook.MailItem mail in myInbox.Items)
- {
- // Grab the Subject
- subject = mail.Subject;
- //Grab the Attachment Name
- if (mail.Subject == "ECG-Liste")
- {
- Console.WriteLine(mail.Attachments[1].FileName);
- System.Threading.Thread.Sleep(1000 * 10);
- mail.Attachments[1].SaveAsFile(filepath + mail.Attachments[1].FileName);
- return true;
- }
- }
- }
- else
- {
- Console.WriteLine("Email noch nicht erhalten");
- return false;
- }
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment