protected override void Execute(CodeActivityContext context) { Application app = new Application(); app.ActiveWindow(); Outlook.MailItem mail; if (!(TemplatePath.Get(context).Contains(".oft") || TemplatePath.Get(context).Contains(".msg"))) { throw new System.Exception("Invalid template format, please use a '.oft' or '.msg' template."); } mail = TemplatePath.Get(context) != ""?(app.CreateItemFromTemplate(TemplatePath.Get(context).Contains(":\\") ? TemplatePath.Get(context) : System.IO.Directory.GetCurrentDirectory() + '\\' + TemplatePath.Get(context)) as Outlook.MailItem):new MailItem(); mail.Subject = String.IsNullOrEmpty(Subject.Get(context))?(String.IsNullOrEmpty(mail.Subject)?"Untitled":mail.Subject):Subject.Get(context); mail.To = String.IsNullOrEmpty(To.Get(context))?mail.To:To.Get(context); mail.CC = String.IsNullOrEmpty(Cc.Get(context))?mail.CC:Cc.Get(context); mail.BCC = String.IsNullOrEmpty(Bcc.Get(context))?mail.BCC:Bcc.Get(context); if (String.IsNullOrEmpty(mail.To) && String.IsNullOrEmpty(mail.CC) && String.IsNullOrEmpty(mail.BCC)) { throw new System.Exception("Error, there is no recepients specified"); } mail.HTMLBody = mail.HTMLBody.Replace("{message}", Body.Get(context)); mail.HTMLBody = mail.HTMLBody.Replace("{table}", DT.Get(context) != null?(DT.Get(context).Rows.Count > 0?GetHTMLTable(DT.Get(context)):""):""); mail.Send(); app.GetNamespace("MAPI").SendAndReceive(true); var releaseResult = Marshal.ReleaseComObject(app); }
protected override void Execute(CodeActivityContext context) { String TO, CC, BCC, SUBJECT, BODY; if (TemplatePath.Get(context).Contains(".eml")) { Message emlMessage = ReadMessage(TemplatePath.Get(context).Contains(":\\") ? TemplatePath.Get(context) : System.IO.Directory.GetCurrentDirectory() + '\\' + TemplatePath.Get(context)); TO = emlMessage.To; CC = emlMessage.CC; BCC = emlMessage.BCC; SUBJECT = emlMessage.Subject; BODY = String.IsNullOrEmpty(emlMessage.HTMLBody)?emlMessage.TextBody:emlMessage.HTMLBody; } else if (TemplatePath.Get(context).Contains(".oft") || TemplatePath.Get(context).Contains(".msg")) { try { Outlook.Application app = new Outlook.Application(); Outlook.MailItem template = app.CreateItemFromTemplate(TemplatePath.Get(context).Contains(":\\") ? TemplatePath.Get(context) : System.IO.Directory.GetCurrentDirectory() + '\\' + TemplatePath.Get(context)) as Outlook.MailItem; TO = template.To; CC = template.CC; BCC = template.BCC; SUBJECT = template.Subject; BODY = String.IsNullOrEmpty(template.HTMLBody) ? template.Body : template.HTMLBody; } catch { throw new System.Exception("Error, .oft and .msg files can only be read if you have Outlook installed, try using a .eml as a template"); } } else { throw new System.Exception("Invalid template format, please use a '.oft', '.msg', or '.eml' template."); } SUBJECT = String.IsNullOrEmpty(Subject.Get(context)) ? (String.IsNullOrEmpty(SUBJECT) ? "Untitled" : SUBJECT) : Subject.Get(context); TO = String.IsNullOrEmpty(To.Get(context)) ? TO : To.Get(context); CC = String.IsNullOrEmpty(Cc.Get(context)) ? CC : Cc.Get(context); BCC = String.IsNullOrEmpty(Bcc.Get(context)) ? BCC : Bcc.Get(context); BODY = BODY.Replace("{message}", String.IsNullOrEmpty(Body.Get(context))?"":Body.Get(context)); BODY = BODY.Replace("{table}", DT.Get(context) != null ? (DT.Get(context).Rows.Count > 0 ? GetHTMLTable(DT.Get(context)) : "") : ""); MailMessage mail = new MailMessage(From.Get(context), TO, SUBJECT, BODY); mail.Bcc.Add(BCC); mail.IsBodyHtml = true; mail.CC.Add(CC); SmtpClient client = new SmtpClient { Port = Port.Get(context), DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, EnableSsl = EnableSSL, Host = Server.Get(context), Credentials = new System.Net.NetworkCredential(Email.Get(context), Password.Get(context)), Timeout = 30000 }; client.Send(mail); }
//[Category("Output")] //public OutArgument<double> ResultNumber { get; set; } protected override void Execute(CodeActivityContext context) { try { var body = Body.Get(context); var subject = Subject.Get(context); var port = Port.Get(context); var server = Server.Get(context); var email = Email.Get(context); var password = Password.Get(context); var bcc = Bcc.Get(context); var cc = Cc.Get(context); var to = To.Get(context); var from = From.Get(context); var name = Name.Get(context); var inputdirpath = InputDirPath.Get(context); String[] files = Directory.GetFiles(inputdirpath); MailMessage mail = new MailMessage(from, to); mail.Subject = subject; mail.Body = body; foreach (string file in files) { mail.Attachments.Add(new System.Net.Mail.Attachment(file)); } SmtpClient smtp = new SmtpClient(); smtp.Host = server; smtp.Port = port; smtp.Credentials = new NetworkCredential(email, password); smtp.EnableSsl = true; smtp.Send(mail); } catch (Exception e) { Console.WriteLine("Exception Caught at: " + e.Source); Console.WriteLine("Exception Message: " + e.Message); } }
public Task SendMailTask(AsyncCodeActivityContext context, Receiver reciever, MailMessage mailMessage, CancellationToken cancellationToken, bool isNewMessage, string body) { List <string> attachments = Attachments ?? new List <string>(); foreach (InArgument <string> file in Files) { AddAttachments(attachments, file.Get(context)); } foreach (string item in AttachmentsCollection.Get(context).EmptyIfNull()) { AddAttachments(attachments, item); } string to = To.Get(context); string cc = Cc.Get(context); string bcc = Bcc.Get(context); string sentOnBehalfOfName = SentOnBehalfOfName.Get(context); string account = Account.Get(context); return(Task.Factory.StartNew(delegate { OutlookAPI.SendMail(mailMessage, account, sentOnBehalfOfName, to, cc, bcc, attachments, IsDraft, IsBodyHtml); })); }
protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken, Application client) { List <string> inputs = Attachments.Select(x => x.Get(context)).ToList(); var app = new Application(TemplatePath.Get(context), Subject?.Get(context), To?.Get(context), Cc?.Get(context), Bcc?.Get(context), Body?.Get(context), From?.Get(context), Port.Get(context), DT?.Get(context), EnableSSL, Server.Get(context), Email.Get(context), Password.Get(context), inputs); await Task.Run(() => app.SendMail()); return(f => { }); }
// Module defining this command // Optional custom code for this activity /// <summary> /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run. /// </summary> /// <param name="context">The NativeActivityContext for the currently running activity.</param> /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns> /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks> protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create(); System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName); // Initialize the arguments if (Attachments.Expression != null) { targetCommand.AddParameter("Attachments", Attachments.Get(context)); } if (Bcc.Expression != null) { targetCommand.AddParameter("Bcc", Bcc.Get(context)); } if (Body.Expression != null) { targetCommand.AddParameter("Body", Body.Get(context)); } if (BodyAsHtml.Expression != null) { targetCommand.AddParameter("BodyAsHtml", BodyAsHtml.Get(context)); } if (Encoding.Expression != null) { targetCommand.AddParameter("Encoding", Encoding.Get(context)); } if (Cc.Expression != null) { targetCommand.AddParameter("Cc", Cc.Get(context)); } if (DeliveryNotificationOption.Expression != null) { targetCommand.AddParameter("DeliveryNotificationOption", DeliveryNotificationOption.Get(context)); } if (From.Expression != null) { targetCommand.AddParameter("From", From.Get(context)); } if (SmtpServer.Expression != null) { targetCommand.AddParameter("SmtpServer", SmtpServer.Get(context)); } if (Priority.Expression != null) { targetCommand.AddParameter("Priority", Priority.Get(context)); } if (Subject.Expression != null) { targetCommand.AddParameter("Subject", Subject.Get(context)); } if (To.Expression != null) { targetCommand.AddParameter("To", To.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (UseSsl.Expression != null) { targetCommand.AddParameter("UseSsl", UseSsl.Get(context)); } if (Port.Expression != null) { targetCommand.AddParameter("Port", Port.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { CancellationTokenSource cancellationTokenSource2 = (CancellationTokenSource)(context.UserState = new CancellationTokenSource()); Receiver reciever = new Receiver(To.Get(context), Cc.Get(context), Bcc.Get(context)); bool isNewMessage = false; MailMessage mailMessage = MailMessage.Get(context); string body = Body.Get(context); if (mailMessage == null) { mailMessage = new MailMessage(); mailMessage.Subject = Subject.Get(context); mailMessage.Body = body; isNewMessage = true; } mailMessage.IsBodyHtml = IsBodyHtml; int timeout = TimeoutMS.Get(context); Task task = SendMailTask(context, reciever, mailMessage, cancellationTokenSource2.Token, isNewMessage, body); TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>(state); TaskHandler(callback, task, taskCompletionSource, cancellationTokenSource2.Token, mailMessage, isNewMessage, timeout); return(taskCompletionSource.Task); }
protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken, Application client) { List <string> inputs = Attachments.Select(x => x.Get(context)).ToList(); if (!(TemplatePath.Get(context).Contains(".oft") || TemplatePath.Get(context).Contains(".msg"))) { throw new System.Exception("Invalid template format, please use a '.oft' or '.msg' template."); } Application app = new Application(); app.SendOutlook(TemplatePath.Get(context), Subject.Get(context), To.Get(context), Cc.Get(context), Bcc.Get(context), Body.Get(context), DT.Get(context), inputs); return(ctx => { }); }
/// <summary> /// Shared Mail logic for replying to a mail /// </summary> /// <param name="context"></param> protected override void Execute(CodeActivityContext context) { // ****************** getting the input values ************************ ExchangeService objExchangeService = ObjExchangeService.Get(context); Item mail = Mail.Get(context); string body = Body.Get(context); bool isBodyHTML = IsBodyHTML.Get(context); //string recipientEmail = RecipientEmail.Get(context); string cc = Cc.Get(context); string bcc = Bcc.Get(context); string sender = Sender.Get(context); string[] attachments = Attachments.Get(context); bool isReplyAll = ReplyAll.Get(context); //******** Sending mail Logic ****** EmailMessage email = EmailMessage.Bind(objExchangeService, mail.Id, new PropertySet(ItemSchema.Attachments)); ResponseMessage responseMessage = email.CreateReply(isReplyAll); //Check for if body is a HTML content if (isBodyHTML) { responseMessage.BodyPrefix = new MessageBody(BodyType.HTML, body); } else { responseMessage.BodyPrefix = body; } //If CC is available if (cc != null && cc.Length > 0) { //Adding recipients to mail string[] recipientsCC = cc.Split(';'); foreach (string recipient in recipientsCC) { responseMessage.CcRecipients.Add(recipient); } } //If BCC is available if (bcc != null && bcc.Length > 0) { //Adding recipients to mail string[] recipientsBcc = bcc.Split(';'); foreach (string recipient in recipientsBcc) { responseMessage.BccRecipients.Add(recipient); } } //Check if attachment is available //If attachments if (attachments != null && attachments.Length > 0) { FolderView view = new FolderView(10000); view.PropertySet = new PropertySet(BasePropertySet.IdOnly); view.PropertySet.Add(FolderSchema.DisplayName); view.Traversal = FolderTraversal.Deep; Mailbox mailbox = new Mailbox(sender); FindFoldersResults findFolderResults = objExchangeService.FindFolders(new FolderId(WellKnownFolderName.MsgFolderRoot, mailbox), view); foreach (Folder folder in findFolderResults) { if (folder.DisplayName == "Sent Items") { //Adding attachments to reply mail EmailMessage reply = responseMessage.Save(folder.Id); foreach (string attachment in attachments) { reply.Attachments.AddFileAttachment(attachment); } reply.Update(ConflictResolutionMode.AlwaysOverwrite); //Sending mail and saving to sent Items reply.SendAndSaveCopy(folder.Id); } } } else { FolderView view = new FolderView(10000); view.PropertySet = new PropertySet(BasePropertySet.IdOnly); view.PropertySet.Add(FolderSchema.DisplayName); view.Traversal = FolderTraversal.Deep; Mailbox mailbox = new Mailbox(sender); FindFoldersResults findFolderResults = objExchangeService.FindFolders(new FolderId(WellKnownFolderName.MsgFolderRoot, mailbox), view); foreach (Folder folder in findFolderResults) { if (folder.DisplayName == "Sent Items") { responseMessage.SendAndSaveCopy(folder.Id); } } } }