为什么smtpclient发送的电子邮件不会出现在已发送的项目中

yos*_*ico 12 .net c# email exchange-server smtpclient

我已经实现了一个通过.Net SmtpClient发送电子邮件的服务器.邮件发送代码如下:

private static MailMessage SendMail(string to, string subject, string body)
{
 MailMessage mailToSend = new MailMessage();
 mailToSend.Body = body;
 mailToSend.Subject = subject;
 mailToSend.IsBodyHtml = true;
 mailToSend.To.Add(to);
 try
 {
  mailClient.Send(mailToSend);
 }
 catch (Exception ex)
 {
  //Log data...
 }
 mailToSend.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

在Web.config我已经把邮件的凭据,像这样:

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="autoemail@mailserver.org">
        <network host="smtp.mailserver.org" password="pswdpswd" port="25" userName="autoemail" clientDomain="the-domain" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)

电子邮件成功发送,一切正常,但当我登录交换服务器中的电子邮件用户时(例如通过Outlook Web-App)我看不到通过SmtpClient发送的邮件(通过代码)发送的项目夹.

如何在此文件夹中保留已发送邮件的副本?谢谢!

Pat*_*man 14

它们不会记录在已发送的项目中,因为它仅使用SMTP级别的用户帐户发送,它实际上并不使用邮箱发送电子邮件.

您唯一的选择是不使用SmtpClient和使用Exchange API发送邮件.

从他们的样本中引用:

ExchangeService service = new ExchangeService();  
service.AutodiscoverUrl("youremailaddress@yourdomain.com");  

EmailMessage message = new EmailMessage(service);  
message.Subject = subjectTextbox.Text;  
message.Body = bodyTextbox.Text;  
message.ToRecipients.Add(recipientTextbox.Text);  
message.Save();  

message.SendAndSaveCopy();
Run Code Online (Sandbox Code Playgroud)

  • 注意:需要将配置文件更改为`.NET Framework 4`。`...Client Profile` 是不够的。然后需要添加对`Microsoft.Exchange.WebServices`的引用,并添加`using Microsoft.Exchange.WebServices` (2认同)