C# – Why emails sent by smtpclient does not appear in sent items

cemailexchange-servernetsmtpclient

I have implemented a server that sends emails via .Net SmtpClient.
the mail sending code looks like that:

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();
}

and in Web.config i've put the mail's credentials, someting like that:

<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>

The emails sent successfuly and everything works fine BUT when I'm logging in to the email user in the exchange server (in example via Outlook Web-App) I can't see the mail sent via SmtpClient (via code) in the sent items folder.

how can I keep a copy of the sent mails in this folders?
Thanks!

Best Answer

They are not recorded in the sent items since it is only send using the account from the user on SMTP level, it doesn't really use the mailbox to send the email.

The only option you have is not to use SmtpClient and use the Exchange API to send mail.

From their sample referenced:

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();
Related Topic