【问题标题】:SMTP email for office 365 using C#使用 C# 的 Office 365 的 SMTP 电子邮件
【发布时间】:2016-04-09 04:34:44
【问题描述】:

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

【问题讨论】:

  • 添加更多有关错误的详细信息,否则没有人可以提供帮助。
  • 您好 Dara,您确定您的公司帐户用户拥有 Microsoft Exchange 许可证吗?您可以使用 mail.office365.com 与该用户发送/接收邮件吗?
  • 如果你不向在 Stackoverflow 上帮助你的人提供任何反馈,你将不会得到进一步的帮助,至少我不会。

标签: email smtp office365


【解决方案1】:
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.");
        }

【讨论】:

  • 此代码在我使用 Microsoft 帐户时有效,我还更改了 o365 教育的 SMTP 地址,它有效,但对于 o365 企业,它停止工作。
  • 发布你的代码源是一件好事,但你应该尽量让它独立于你自己的应用程序上下文。例如,mailbody 可以是一个简单的“hello world”而不使用VisitorNameBox.TextTo 字段也是如此。另一件事,您应该但最相关的材料为我们帮助您:您的应用程序引发的错误!关于这一点,向客户端显示错误消息是一件好事,但您也应该记录异常(stackoverflow.com/questions/14973642/…)。
【解决方案2】:
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);

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2019-12-22
  • 1970-01-01
  • 2011-09-08
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 2015-06-11
相关资源
最近更新 更多