使用C#的Office 365的SMTP电子邮件

时间:2016-01-05 11:07:22

标签: email smtp office365

我正在开发一个应用程序,我想从WPF应用程序内部发送电子邮件。我使用了我的一个o365帐户来测试,它是成功的,但在尝试另一个(公司帐户)时,我收到了一个错误。可能有什么不对?

1 个答案:

答案 0 :(得分:0)

string mailbody = "Hello, "+ WhoToBox.Text + "\n" + VisitorNameBox.Text +" is waiting for you at the reception.\nMobile Number: "+VisitorPhoneBox.Text;
        string whoto = "";
        List<CompanyStaff> peopleList = CompanyStaff.GetStaffList();
        foreach (var item in peopleList)
        {
            if (item.FirstName == WhoToBox.Text)
            {
               whoto = item.Alias;
            }
        }
        string to = whoto;
        string from = "<emailaddy>";
        MailMessage message = new MailMessage(from, to);
        message.Subject = "You have a new Visitor";
        message.Body = mailbody;
        message.BodyEncoding = Encoding.UTF8;
        message.IsBodyHtml = false;
        SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);
        //use a Microsoft Account here...
        NetworkCredential basicCredential = new NetworkCredential("<emailaddy>", "<password>");
        client.EnableSsl = true;
        client.UseDefaultCredentials = true;
        client.Credentials = basicCredential;
        try
        { 
         client.Send(message);
         MessageBox.Show("Notification of your presence has been sent to " + WhoToBox.Text);
        }
        catch
        {
            MessageBox.Show("Sorry, we are unable to send notification of your presence. Please try again.");
        }