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 void Execute(CodeActivityContext context) { string username = Email.Get(context); //发送端账号 string password = Password.Get(context); //发送端密码(这个客户端重置后的密码) string server = Server.Get(context); //邮件服务器 Int32 port = Port.Get(context); //端口号 string from = From.Get(context); //发送者地址(一般与发送账号相同) string name = Name.Get(context); //发送者名称 string[] receivers_To = Receivers_To.Get(context); //收件人 string[] receivers_Cc = Receivers_Cc.Get(context); //抄送 string[] recervers_Bcc = Receivers_Bcc.Get(context); //密送 string mailTopic = MailTopic.Get(context); //邮件的主题 string mailBody = MailBody.Get(context); //发送的邮件正文 //string[] attachFiles = AttachFiles.Get(context); //附件列表 MimeMessage transMsg = TransMailMessage.Get(context); try { 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); } MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient(); client.Connect(server, port, SecureConnection); client.Authenticate(username, password); var multipart = new Multipart("mixed"); MimeMessage msg = new MimeMessage(); if (name == null && from == null) { msg.From.Add((new MailboxAddress(username))); } else if (name == null && from != null) { msg.From.Add((new MailboxAddress(from))); } else if (name != null && from == null) { msg.From.Add((new MailboxAddress(name, username))); } else { msg.From.Add((new MailboxAddress(name, from))); } if (receivers_To != null) { foreach (string receiver in receivers_To) { msg.To.Add((new MailboxAddress(receiver))); } } if (receivers_Cc != null) { foreach (string receiver in receivers_Cc) { msg.Cc.Add((new MailboxAddress(receiver))); } } if (recervers_Bcc != null) { foreach (string receiver in recervers_Bcc) { msg.Bcc.Add((new MailboxAddress(receiver))); } } if (mailBody != null) { if (!IsBodyHtml) { var plain = new TextPart(TextFormat.Plain) { Text = mailBody }; multipart.Add(plain); } else { var Html = new TextPart(TextFormat.Html) { Text = mailBody }; multipart.Add(Html); } } else if (transMsg != null) { if (!IsBodyHtml) { var plain = new TextPart(TextFormat.Plain) { Text = transMsg.TextBody }; multipart.Add(plain); } else { var Html = new TextPart(TextFormat.Html) { Text = transMsg.HtmlBody }; multipart.Add(Html); } } else { var plain = new TextPart(TextFormat.Plain) { Text = "" }; multipart.Add(plain); } if (mailTopic != null) { msg.Subject = mailTopic; } else if (transMsg != null) { msg.Subject = transMsg.Subject; } msg.Priority = msgProperty; if (Files != null) { foreach (var p in Files) { try { if (!string.IsNullOrEmpty(p.ToString())) { var attimg = new MimePart() { Content = new MimeContent(File.OpenRead(p.ToString()), ContentEncoding.Default), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = Path.GetFileName(p.ToString()) }; multipart.Add(attimg); } } catch (FileNotFoundException ex) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "文件未找到,请检查有效路径", ex.Message); } } } if (transMsg != null) { foreach (MimeEntity part in transMsg.Attachments) { multipart.Add(part); } } msg.Body = multipart; try { client.Send(msg); Debug.WriteLine("发送成功"); } catch (System.Net.Mail.SmtpException ex) { Debug.WriteLine(ex.Message, "发送邮件出错"); } } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "发送邮件失败", e.Message); } }