Guest User

Untitled

a guest
Feb 9th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Mail;
  6. using System.Runtime.InteropServices;
  7. using Outlook = Microsoft.Office.Interop.Outlook;
  8. using Office = Microsoft.Office.Core;
  9.  
  10. namespace FileOrganizer
  11. {
  12. class Program
  13. {
  14. private void CreateMailItem()
  15. {
  16. //Outlook.MailItem mailItem = (Outlook.MailItem)
  17. // this.Application.CreateItem(Outlook.OlItemType.olMailItem);
  18. Outlook.Application app = new Outlook.Application();
  19. Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);
  20. mailItem.Subject = "This is the subject";
  21. mailItem.To = "someone@example.com";
  22. mailItem.Body = "This is the message.";
  23. mailItem.Attachments.Add(logPath);//logPath is a string holding path to the log.txt file
  24. mailItem.Importance = Outlook.OlImportance.olImportanceHigh;
  25. mailItem.Display(false);
  26. }
  27. }
  28. }
  29.  
  30. Outlook.MailItem mailItem = (Outlook.MailItem)
  31. this.Application.CreateItem(Outlook.OlItemType.olMailItem);
  32.  
  33. Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
  34. Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
  35.  
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.Text;
  40.  
  41. using System.Net;
  42. using System.Configuration;
  43. using System.IO;
  44. using System.Net.Mail;
  45. using System.Runtime.InteropServices;
  46. using Outlook = Microsoft.Office.Interop.Outlook;
  47. using Office = Microsoft.Office.Core;
  48.  
  49. public enum BodyType
  50. {
  51. PlainText,
  52. RTF,
  53. HTML
  54. }
  55.  
  56. //....
  57.  
  58. public static bool sendEmailViaOutlook(string sFromAddress, string sToAddress, string sCc, string sSubject, string sBody, BodyType bodyType, List<string> arrAttachments = null, string sBcc = null)
  59. {
  60. //Send email via Office Outlook 2010
  61. //'sFromAddress' = email address sending from (ex: "me@somewhere.com") -- this account must exist in Outlook. Only one email address is allowed!
  62. //'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")
  63. //'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")
  64. //'sSubject' = email subject as plain text
  65. //'sBody' = email body. Type of data depends on 'bodyType'
  66. //'bodyType' = type of text in 'sBody': plain text, HTML or RTF
  67. //'arrAttachments' = if not null, must be a list of absolute file paths to attach to the email
  68. //'sBcc' = single email address to use as a Blind Carbon Copy, or null not to use
  69. //RETURN:
  70. // = true if success
  71. bool bRes = false;
  72.  
  73. try
  74. {
  75. //Get Outlook COM objects
  76. Outlook.Application app = new Outlook.Application();
  77. Outlook.MailItem newMail = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
  78.  
  79. //Parse 'sToAddress'
  80. if (!string.IsNullOrWhiteSpace(sToAddress))
  81. {
  82. string[] arrAddTos = sToAddress.Split(new char[] { ';', ',' });
  83. foreach (string strAddr in arrAddTos)
  84. {
  85. if (!string.IsNullOrWhiteSpace(strAddr) &&
  86. strAddr.IndexOf('@') != -1)
  87. {
  88. newMail.Recipients.Add(strAddr.Trim());
  89. }
  90. else
  91. throw new Exception("Bad to-address: " + sToAddress);
  92. }
  93. }
  94. else
  95. throw new Exception("Must specify to-address");
  96.  
  97. //Parse 'sCc'
  98. if (!string.IsNullOrWhiteSpace(sCc))
  99. {
  100. string[] arrAddTos = sCc.Split(new char[] { ';', ',' });
  101. foreach (string strAddr in arrAddTos)
  102. {
  103. if (!string.IsNullOrWhiteSpace(strAddr) &&
  104. strAddr.IndexOf('@') != -1)
  105. {
  106. newMail.Recipients.Add(strAddr.Trim());
  107. }
  108. else
  109. throw new Exception("Bad CC-address: " + sCc);
  110. }
  111. }
  112.  
  113. //Is BCC empty?
  114. if (!string.IsNullOrWhiteSpace(sBcc))
  115. {
  116. newMail.BCC = sBcc.Trim();
  117. }
  118.  
  119. //Resolve all recepients
  120. if (!newMail.Recipients.ResolveAll())
  121. {
  122. throw new Exception("Failed to resolve all recipients: " + sToAddress + ";" + sCc);
  123. }
  124.  
  125.  
  126. //Set type of message
  127. switch (bodyType)
  128. {
  129. case BodyType.HTML:
  130. newMail.HTMLBody = sBody;
  131. break;
  132. case BodyType.RTF:
  133. newMail.RTFBody = sBody;
  134. break;
  135. case BodyType.PlainText:
  136. newMail.Body = sBody;
  137. break;
  138. default:
  139. throw new Exception("Bad email body type: " + bodyType);
  140. }
  141.  
  142.  
  143. if (arrAttachments != null)
  144. {
  145. //Add attachments
  146. foreach (string strPath in arrAttachments)
  147. {
  148. if (File.Exists(strPath))
  149. {
  150. newMail.Attachments.Add(strPath);
  151. }
  152. else
  153. throw new Exception("Attachment file is not found: "" + strPath + """);
  154. }
  155. }
  156.  
  157. //Add subject
  158. if(!string.IsNullOrWhiteSpace(sSubject))
  159. newMail.Subject = sSubject;
  160.  
  161. Outlook.Accounts accounts = app.Session.Accounts;
  162. Outlook.Account acc = null;
  163.  
  164. //Look for our account in the Outlook
  165. foreach (Outlook.Account account in accounts)
  166. {
  167. if (account.SmtpAddress.Equals(sFromAddress, StringComparison.CurrentCultureIgnoreCase))
  168. {
  169. //Use it
  170. acc = account;
  171. break;
  172. }
  173. }
  174.  
  175. //Did we get the account
  176. if (acc != null)
  177. {
  178. //Use this account to send the e-mail.
  179. newMail.SendUsingAccount = acc;
  180.  
  181. //And send it
  182. ((Outlook._MailItem)newMail).Send();
  183.  
  184. //Done
  185. bRes = true;
  186. }
  187. else
  188. {
  189. throw new Exception("Account does not exist in Outlook: " + sFromAddress);
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. Console.WriteLine("ERROR: Failed to send mail: " + ex.Message);
  195. }
  196.  
  197. return bRes;
  198. }
  199.  
  200. List<string> arrAttachFiles = new List<string>() { @"C:UsersUserDesktopPicture.png" };
  201.  
  202. bool bRes = sendEmailViaOutlook("senders_email@somewhere.com",
  203. "john.doe@hotmail.com, jane_smith@gmail.com", null,
  204. "Test email from script - " + DateTime.Now.ToString(),
  205. "My message body - " + DateTime.Now.ToString(),
  206. BodyType.PlainText,
  207. arrAttachFiles,
  208. null);
  209.  
  210. Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);
  211.  
  212. Outlook.MailItem mailItem = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
Add Comment
Please, Sign In to add comment