13

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!

yossico
  • 3,421
  • 5
  • 41
  • 76
  • 2
    You could BCC the e-mail to yourself, if you only want to have a copy of the e-mail. – Max Jun 17 '14 at 07:02

1 Answers1

15

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();
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    Thanks! gee that was fast :) – yossico Jun 17 '14 at 07:04
  • 2
    Note: Need to change profile to `.NET Framework 4`. The `...Client Profile` is not sufficient. Then you need to add a reference to `Microsoft.Exchange.WebServices`, and add `using Microsoft.Exchange.WebServices` – Drew Chapin Sep 21 '16 at 17:48