0

I am developing an application where I want to send email from inside a WPF Application. I used one of my o365 accounts to test, it was successful, but on trying another (corporate account), I got an error. What could be wrong?

  • Add more details about error, no one will be able to help otherwise. – titogeo Jan 05 '16 at 11:44
  • Hi Dara, are you sure your company account user has a Microsoft exchange license? Can you send/receive mail with this user using mail.office365.com? – Benoit Patra Jan 05 '16 at 17:26
  • If you do not provide any feedback to people helping you on Stackoverflow you will not get further help, not from me at least. – Benoit Patra Jan 15 '16 at 09:13

2 Answers2

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.");
        }
  • This code works when I use a Microsoft Account, I also changed the SMTP address for o365 education, it work, but for o365 enterprise, it stopped working. – Dara Oladapo Jan 05 '16 at 11:14
  • Posting your code source is a good thing but you should try to keep it independent of your own application context. For example, the `mailbody` could be a simple "hello world" without using `VisitorNameBox.Text`, same thing for the `To` field. Another thing, you should but the most relevant material for us to help you: the error raised by your application! About this, displaying an error message to the client is a good thing but you should log exceptions as well( http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice). – Benoit Patra Jan 05 '16 at 17:36
0
SmtpClient smt = new SmtpClient("smtp.office365.com");
                smt.UseDefaultCredentials = false;
                smt.Credentials = new System.Net.NetworkCredential("name@yourdomian", "yourpassword");
                smt.Port = 587;
                smt.DeliveryMethod = SmtpDeliveryMethod.Network;
                smt.TargetName = "STARTTLS/smtp.office365.com";
                smt.EnableSsl = true;
                smt.Timeout = 60000;
                MailMessage emsg = new MailMessage();
                emsg.From = new MailAddress("senderemail");
                emsg.To.Add("recipientemail");
                emsg.Subject = "Subject";
                var str = "";
                str += "<html><body>";
                str += "<table rules='all' style='border-color: #666;' cellpadding='10' width='100%'>";
                str += "<tr  style='background: #eee;'><td colspan='2'><strong>HEADING</strong></td></tr>";
                str += "<tr><td><strong>Title1</strong> </td><td>value1</td></tr>";
                str += "<tr><td><strong>Title2</strong> </td><td>value_2</td></tr>";
                str += "<tr  style='background: #eee;'><td colspan='2'><strong>Remarks</strong></td></tr>";
                str += "<tr  style='background: #eee;'><td><strong>Support</strong> </td><td>ContactS</td></tr>";
                emsg.Body = (str);
                emsg.IsBodyHtml = true;
                smt.Send(emsg);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 05 '21 at 12:27