Csharp/C#教程:将已发送的MailMessage发送到“已发送文件夹”分享


将已发送的MailMessage发送到“已发送文件夹”

我正在使用Exchange Server发送带有SmtpClient(正在成功发送)的MailMessages,但希望我发送的电子邮件转到我发送给他们的电子邮件地址的已发送文件夹(未发生)。

using (var mailMessage = new MailMessage("fromaddress@blah.com", "toaddress@blah.com", "subject", "body")) { var smtpClient = new SmtpClient("SmtpHost") { EnableSsl = false, DeliveryMethod = SmtpDeliveryMethod.Network }; // Apply credentials smtpClient.Credentials = new NetworkCredential("smtpUsername", "smtpPassword"); // Send smtpClient.Send(mailMessage); } 

是否有我缺少的配置,以确保我发送的所有来自“fromaddress@blah.com”的电子邮件都到达他们的已发送文件夹?

我猜你的要求主要是围绕让用户看到已发送的电子邮件。 发送的项目文件夹将是允许这种情况发生的一种方法。 在过去,我通过添加一个BCC Address来解决这个问题,该BCC Address可以直接将电子邮件发送到分发列表,用户或共享邮箱,允许用户查看已发送的内容。

尝试使用某种outlook规则将项目移动到标记为读取的已发送项目文件夹…

 using (var mailMessage = new MailMessage( "fromaddress@blah.com", "toaddress@blah.com", "", "fromaddress@blah.com", "subject", "body")) { var smtpClient = new SmtpClient("SmtpHost") { EnableSsl = false, DeliveryMethod = SmtpDeliveryMethod.Network }; // Apply credentials smtpClient.Credentials = new NetworkCredential("smtpUsername", "smtpPassword"); // Send smtpClient.Send(mailMessage); } 

我已经这样做了,所以为了完整性,这里是如何正确地做到这一点。 使用托管交换Web服务( https://msdn.microsoft.com/en-us/library/dd633709%28EXCHG.80%29.aspx ):

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); // In case you have a dodgy SSL certificate: System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }; service.Credentials = new WebCredentials("username", "password", "MYDOMAIN"); service.Url = new Uri("https://exchangebox/EWS/Exchange.asmx"); EmailMessage em = new EmailMessage(service); em.Subject = "example email"; em.Body = new MessageBody("hello world"); em.Sender = new Microsoft.Exchange.WebServices.Data.EmailAddress("john.smith@example.com"); em.ToRecipients.Add(new Microsoft.Exchange.WebServices.Data.EmailAddress("bob.smith@example.com")); // Send the email and put it into the SentItems: em.SendAndSaveCopy(WellKnownFolderName.SentItems); 

我一直在寻找这个问题的答案,但不依赖于Exchange Server,而是使用IMAP服务器。 我不知道这是否超出了问题的范围,但我发现它搜索“将已发送的MailMessage发送到已发送的文件夹”,这首先是我的问题。

在我构建自己的解决方案的任何地方都找不到直接答案:

我正在实现save方法作为smtpClient的扩展,因此,我们将使用.SendAndSaveMessageToIMAP()而不是.SendAndSaveMessageToIMAP()

 public static class SmtpClientExtensions { static System.IO.StreamWriter sw = null; static System.Net.Sockets.TcpClient tcpc = null; static System.Net.Security.SslStream ssl = null; static string path; static int bytes = -1; static byte[] buffer; static System.Text.StringBuilder sb = new System.Text.StringBuilder(); static byte[] dummy; ///  /// Communication with server ///  /// The command beeing sent private static void SendCommandAndReceiveResponse(string command) { try { if (command != "") { if (tcpc.Connected) { dummy = System.Text.Encoding.ASCII.GetBytes(command); ssl.Write(dummy, 0, dummy.Length); } else { throw new System.ApplicationException("TCP CONNECTION DISCONNECTED"); } } ssl.Flush(); buffer = new byte[2048]; bytes = ssl.Read(buffer, 0, 2048); sb.Append(System.Text.Encoding.ASCII.GetString(buffer)); sw.WriteLine(sb.ToString()); sb = new System.Text.StringBuilder(); } catch (System.Exception ex) { throw new System.ApplicationException(ex.Message); } } ///  /// Saving a mail message before beeing sent by the SMTP client ///  /// The caller /// The address of the IMAP server /// The port of the IMAP server /// The username to log on to the IMAP server /// The password to log on to the IMAP server /// The name of the folder where the message will be saved /// The message being saved public static void SendAndSaveMessageToIMAP(this System.Net.Mail.SmtpClient self, System.Net.Mail.MailMessage mailMessage, string imapServer, int imapPort, string userName, string password, string sentFolderName) { try { path = System.Environment.CurrentDirectory + "\emailresponse.txt"; if (System.IO.File.Exists(path)) System.IO.File.Delete(path); sw = new System.IO.StreamWriter(System.IO.File.Create(path)); tcpc = new System.Net.Sockets.TcpClient(imapServer, imapPort); ssl = new System.Net.Security.SslStream(tcpc.GetStream()); ssl.AuthenticateAsClient(imapServer); SendCommandAndReceiveResponse(""); SendCommandAndReceiveResponse(string.Format("$ LOGIN {1} {2} {0}", System.Environment.NewLine, userName, password)); using (var m = mailMessage.RawMessage()) { m.Position = 0; var sr = new System.IO.StreamReader(m); var myStr = sr.ReadToEnd(); SendCommandAndReceiveResponse(string.Format("$ APPEND {1} (\Seen) {{{2}}}{0}", System.Environment.NewLine, sentFolderName, myStr.Length)); SendCommandAndReceiveResponse(string.Format("{1}{0}", System.Environment.NewLine, myStr)); } SendCommandAndReceiveResponse(string.Format("$ LOGOUT{0}", System.Environment.NewLine)); } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine("error: " + ex.Message); } finally { if (sw != null) { sw.Close(); sw.Dispose(); } if (ssl != null) { ssl.Close(); ssl.Dispose(); } if (tcpc != null) { tcpc.Close(); } } self.Send(mailMessage); } } public static class MailMessageExtensions { private static readonly System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic; private static readonly System.Type MailWriter = typeof(System.Net.Mail.SmtpClient).Assembly.GetType("System.Net.Mail.MailWriter"); private static readonly System.Reflection.ConstructorInfo MailWriterConstructor = MailWriter.GetConstructor(Flags, null, new[] { typeof(System.IO.Stream) }, null); private static readonly System.Reflection.MethodInfo CloseMethod = MailWriter.GetMethod("Close", Flags); private static readonly System.Reflection.MethodInfo SendMethod = typeof(System.Net.Mail.MailMessage).GetMethod("Send", Flags); ///  /// A little hack to determine the number of parameters that we /// need to pass to the SaveMethod. ///  private static readonly bool IsRunningInDotNetFourPointFive = SendMethod.GetParameters().Length == 3; ///  /// The raw contents of this MailMessage as a MemoryStream. ///  /// The caller. /// A MemoryStream with the raw contents of this MailMessage. public static System.IO.MemoryStream RawMessage(this System.Net.Mail.MailMessage self) { var result = new System.IO.MemoryStream(); var mailWriter = MailWriterConstructor.Invoke(new object[] { result }); SendMethod.Invoke(self, Flags, null, IsRunningInDotNetFourPointFive ? new[] { mailWriter, true, true } : new[] { mailWriter, true }, null); result = new System.IO.MemoryStream(result.ToArray()); CloseMethod.Invoke(mailWriter, Flags, null, new object[] { }, null); return result; } } 

所以Robert Reid的榜样将成为现实

  using (var mailMessage = new MailMessage("fromaddress@blah.com", "toaddress@blah.com", "subject", "body")) { //Add an attachment just for the sake of it Attachment doc = new Attachment(@"filePath"); doc.ContentId = "doc"; mailMessage.Attachments.Add(doc); var smtpClient = new SmtpClient("SmtpHost") { EnableSsl = false, DeliveryMethod = SmtpDeliveryMethod.Network }; // Apply credentials smtpClient.Credentials = new NetworkCredential("smtpUsername", "smtpPassword"); // Send smtpClient.SendAndSaveMessageToIMAP(mailMessage, "imap.mail.com", 993, "imapUsername", "imapPassword", "SENT"); } 

如果要在“已发送邮件”文件夹中包含已发送邮件,则需要从Outlook发送邮件。 此文件夹是Outlook(和许多其他邮件客户端)概念,而不是SMTP概念。

您可以使用Outlook Automation API要求Outlook创建电子邮件并发送它。

上述就是C#学习教程:将已发送的MailMessage发送到“已发送文件夹”分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/989505.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐