4

I'm making a small program that can send emails and read emails. I can currently send emails however I'm not sure how I can access my inbox using .Net.Mail. Is there a way of doing this?

My code is as fallows

try
{
    SmtpClient mySmtpClient = new SmtpClient("smtp.live.com");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
    System.Net.NetworkCredential basicAuthenticationInfo = new
        System.Net.NetworkCredential("example@live.com", "password");
    mySmtpClient.Credentials = basicAuthenticationInfo;
    mySmtpClient.EnableSsl = true;

    // add from,to mailaddresses
    MailAddress from = new MailAddress("example@live.com");
    MailAddress to = new MailAddress("example@example.eu");
    MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
    MailMessage msg; 

    // set subject and encoding
    myMail.Subject = "Test message";
    myMail.SubjectEncoding = System.Text.Encoding.UTF8;

    // set body-message and encoding
    myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
    myMail.BodyEncoding = System.Text.Encoding.UTF8;
    // text or html
    myMail.IsBodyHtml = true;

    mySmtpClient.Send(myMail);
}

catch (SmtpException ex)
{
    throw new ApplicationException
        ("SmtpException has occured: " + ex.Message);
}
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • 2
    Have you tested it? What is the issue? – rory.ap Jan 24 '17 at 17:54
  • If you want to access the stored email folders for Microsoft mail accounts consider the REST API @ https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations - POP won't cut it. – Alex K. Jan 24 '17 at 17:58
  • This is not possible with the framework's System.Net.Mail. You'll need a library like [MailKit](https://github.com/jstedfast/MailKit) with IMAP or POP3 support. – Mike Zboray Jan 24 '17 at 18:11

1 Answers1

1

You cannot get emails with SMTP. You need to use IMAP

Consider using a library like https://github.com/andyedinborough/aenetmail AEMail

For more info go here: Accessing Imap in C#

Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41