Guest User

Untitled

a guest
May 4th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace ConsoleApp2
  6. {
  7. public enum BodyType
  8. {
  9. PlainText,
  10. RTF,
  11. HTML
  12. }
  13. #pragma warning disable CS0618 // Typ oder Element ist veraltet
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. const string filepath = @"C:\Robinsonfile\";
  19. const string configFile = @"C:\Robinsonfile\config.txt";
  20. const string emailTo = "data_puller@outlook.com";
  21. const string title = "test";
  22. const string sCc = "";
  23. const string content = "";
  24.  
  25. if (File.Exists(@"C:\Robinsonfile\config.txt") != true)
  26. {
  27. ConfigDoesNotExist(filepath, configFile, emailTo, sCc, title, content);
  28. }
  29. else
  30. {
  31. ConfigExists(filepath, configFile, emailTo, sCc, title, content);
  32. }
  33. }
  34.  
  35. private static void ConfigExists(string filepath, string configFile, string emailTo, string sCc, string title,
  36. string content)
  37. {
  38. System.IO.Directory.CreateDirectory(filepath);
  39. //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  40. string[] config = readConfigFile(configFile);
  41. if (config[0] == "true")
  42. {
  43. config[0] = "false";
  44.  
  45. if (config.Length == 1)
  46. {
  47. string[] data = new string[2];
  48. data[0] = config[0];
  49. Console.WriteLine("Bitte Email angeben und mit [Enter] bestätigen");
  50. data[1] = Console.ReadLine();
  51. sendMailAndSaveResponse(data, emailTo, sCc, title, content, filepath);
  52. File.WriteAllLines(configFile, data);
  53. }
  54. else
  55. {
  56. File.WriteAllLines(configFile, config);
  57. sendMailAndSaveResponse(config, emailTo, sCc, title, content, filepath);
  58. }
  59. }
  60. else
  61. {
  62. if (sendEmailViaOutlook(config[1], emailTo, sCc, title, content, BodyType.PlainText))
  63. {
  64. Console.WriteLine("Email wurde versandt");
  65. bool search = readMailsAndSaveContent(filepath);
  66. while (!search)
  67. {
  68. search = readMailsAndSaveContent(filepath);
  69. System.Threading.Thread.Sleep(1000 * 60);
  70. }
  71. }
  72. }
  73. }
  74.  
  75. private static void ConfigDoesNotExist(string filepath, string configFile, string emailTo, string sCc, string title,
  76. string content)
  77. {
  78. CreateConfigFile(filepath, configFile);
  79. //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  80. string[] config = readConfigFile(configFile);
  81. if (config[0] == "true")
  82. {
  83. config[0] = "false";
  84. if (config.Length == 1)
  85. {
  86. var data = newData(config, configFile);
  87. sendMailAndSaveResponse(data, emailTo, sCc, title, content, filepath);
  88. File.WriteAllLines(configFile, data);
  89. }
  90. else
  91. {
  92. File.WriteAllLines(configFile, config);
  93. sendMailAndSaveResponse(config, emailTo, sCc, title, content, filepath);
  94. }
  95. }
  96. else
  97. {
  98. sendMailAndSaveResponse(config, emailTo, sCc, title, content, filepath);
  99. }
  100. }
  101.  
  102. private static void CreateConfigFile(string filepath, string configFile)
  103. {
  104. Directory.CreateDirectory(filepath);
  105. File.CreateText(configFile).Close();
  106. File.WriteAllText(configFile, "true");
  107. }
  108.  
  109. private static string[] newData(string[] config, string configFile)
  110. {
  111. string[] data = new string[2];
  112. data[0] = config[0];
  113. Console.WriteLine("Bitte Email angeben und mit [Enter] bestätigen");
  114. data[1] = Console.ReadLine();
  115. File.WriteAllLines(configFile, data);
  116. return data;
  117. }
  118.  
  119. private static void sendMailAndSaveResponse(string[] data, string emailTo, string sCc, string title,
  120. string content,
  121. string filepath)
  122. {
  123. if (sendEmailViaOutlook(data[1], emailTo, sCc, title, content, BodyType.PlainText))
  124. {
  125. Console.WriteLine("Email wurde versandt");
  126. bool search = readMailsAndSaveContent(filepath);
  127. while (!search)
  128. {
  129. search = readMailsAndSaveContent(filepath);
  130. System.Threading.Thread.Sleep(1000 * 60);
  131. }
  132. }
  133. else
  134. {
  135. Console.WriteLine("Oh noes! Schreibe eine Email an Patrick Hentschel!");
  136. System.Threading.Thread.Sleep(1000 * 60);
  137. }
  138. }
  139.  
  140. public static bool sendEmailViaOutlook(string sFromAddress, string sToAddress, string sCc, string sSubject,
  141. string sBody, BodyType bodyType, List<string> arrAttachments = null, string sBcc = null)
  142. {
  143. //Send email via Office Outlook 2010
  144. //'sFromAddress' = email address sending from (ex: "me@somewhere.com") -- this account must exist in Outlook. Only one email address is allowed!
  145. //'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")
  146. //'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")
  147. //'sSubject' = email subject as plain text
  148. //'sBody' = email body. Type of data depends on 'bodyType'
  149. //'bodyType' = type of text in 'sBody': plain text, HTML or RTF
  150. //'arrAttachments' = if not null, must be a list of absolute file paths to attach to the email
  151. //'sBcc' = single email address to use as a Blind Carbon Copy, or null not to use
  152. //RETURN:
  153. // = true if success
  154. bool bRes = false;
  155.  
  156. try
  157. {
  158. //Get Outlook COM objects
  159. Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
  160. Microsoft.Office.Interop.Outlook.MailItem newMail =
  161. (Microsoft.Office.Interop.Outlook.MailItem) app.CreateItem(Microsoft.Office.Interop.Outlook
  162. .OlItemType.olMailItem);
  163.  
  164. //Parse 'sToAddress'
  165. if (!string.IsNullOrWhiteSpace(sToAddress))
  166. {
  167. string[] arrAddTos = sToAddress.Split(new char[] {';', ','});
  168. foreach (string strAddr in arrAddTos)
  169. {
  170. if (!string.IsNullOrWhiteSpace(strAddr) &&
  171. strAddr.IndexOf('@') != -1)
  172. {
  173. newMail.Recipients.Add(strAddr.Trim());
  174. }
  175. else
  176. throw new System.Exception("Bad to-address: " + sToAddress);
  177. }
  178. }
  179. else
  180. throw new System.Exception("Must specify to-address");
  181.  
  182. //Parse 'sCc'
  183. if (!string.IsNullOrWhiteSpace(sCc))
  184. {
  185. string[] arrAddTos = sCc.Split(new char[] {';', ','});
  186. foreach (string strAddr in arrAddTos)
  187. {
  188. if (!string.IsNullOrWhiteSpace(strAddr) &&
  189. strAddr.IndexOf('@') != -1)
  190. {
  191. newMail.Recipients.Add(strAddr.Trim());
  192. }
  193. else
  194. throw new System.Exception("Bad CC-address: " + sCc);
  195. }
  196. }
  197.  
  198. //Is BCC empty?
  199. if (!string.IsNullOrWhiteSpace(sBcc))
  200. {
  201. newMail.BCC = sBcc.Trim();
  202. }
  203.  
  204. //Resolve all recepients
  205. if (!newMail.Recipients.ResolveAll())
  206. {
  207. throw new System.Exception("Failed to resolve all recipients: " + sToAddress + ";" + sCc);
  208. }
  209.  
  210.  
  211. //Set type of message
  212. switch (bodyType)
  213. {
  214. case BodyType.HTML:
  215. newMail.HTMLBody = sBody;
  216. break;
  217. case BodyType.RTF:
  218. newMail.RTFBody = sBody;
  219. break;
  220. case BodyType.PlainText:
  221. newMail.Body = sBody;
  222. break;
  223. default:
  224. throw new System.Exception("Bad email body type: " + bodyType);
  225. }
  226.  
  227.  
  228. if (arrAttachments != null)
  229. {
  230. //Add attachments
  231. foreach (string strPath in arrAttachments)
  232. {
  233. if (File.Exists(strPath))
  234. {
  235. newMail.Attachments.Add(strPath);
  236. }
  237. else
  238. throw new System.Exception("Attachment file is not found: \"" + strPath + "\"");
  239. }
  240. }
  241.  
  242. //Add subject
  243. if (!string.IsNullOrWhiteSpace(sSubject))
  244. newMail.Subject = sSubject;
  245.  
  246. Microsoft.Office.Interop.Outlook.Accounts accounts = app.Session.Accounts;
  247. Microsoft.Office.Interop.Outlook.Account acc = null;
  248.  
  249. //Look for our account in the Outlook
  250. foreach (Microsoft.Office.Interop.Outlook.Account account in accounts)
  251. {
  252. if (account.SmtpAddress.Equals(sFromAddress, StringComparison.CurrentCultureIgnoreCase))
  253. {
  254. //Use it
  255. acc = account;
  256. break;
  257. }
  258. }
  259.  
  260. //Did we get the account
  261. if (acc != null)
  262. {
  263. //Use this account to send the e-mail.
  264. newMail.SendUsingAccount = acc;
  265.  
  266. //And send it
  267. ((Microsoft.Office.Interop.Outlook._MailItem) newMail).Send();
  268.  
  269. //Done
  270. bRes = true;
  271. }
  272. else
  273. throw new System.Exception("Account does not exist in Outlook: " + sFromAddress);
  274. }
  275. catch (System.Exception ex)
  276. {
  277. Console.WriteLine("ERROR: Failed to send mail: " + ex.Message);
  278. }
  279.  
  280. return bRes;
  281. }
  282.  
  283. public static string[] readConfigFile(string filepath)
  284. {
  285. return System.IO.File.ReadAllLines(filepath);
  286. }
  287.  
  288.  
  289. public static bool readMailsAndSaveContent(string filepath)
  290. {
  291. Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
  292. Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = app.GetNamespace("MAPI");
  293. Microsoft.Office.Interop.Outlook.MAPIFolder myInbox =
  294. mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
  295. if (myInbox.Items.Count > 0)
  296. {
  297. String subject = "";
  298. foreach (Microsoft.Office.Interop.Outlook.MailItem mail in myInbox.Items)
  299. {
  300. // Grab the Subject
  301. subject = mail.Subject;
  302. //Grab the Attachment Name
  303. if (mail.Subject == "ECG-Liste")
  304. {
  305. Console.WriteLine(mail.Attachments[1].FileName);
  306. System.Threading.Thread.Sleep(1000 * 10);
  307. mail.Attachments[1].SaveAsFile(filepath + mail.Attachments[1].FileName);
  308. return true;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. Console.WriteLine("Email noch nicht erhalten");
  315. return false;
  316. }
  317.  
  318. return false;
  319. }
  320. }
  321. }
Add Comment
Please, Sign In to add comment