无法使用 SmtpClient 发送邮件

发布于 2024-09-14 20:47:04 字数 1449 浏览 8 评论 0原文

无法发送邮件。这是我的 C# 源代码:

  var to = "[email protected]";
  var subject = "test";
  var body = "test mail";
  var message = new MailMessage(from, to, subject, body);
  var client = new SmtpClient { Credentials = new NetworkCredential("[email protected]", "mypassword") };
  client.Send(message);

这是 App.config

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network
          host="mail.mycompany.com"
          port="25"
          userName="[email protected]"
          password="mypassword"
        />
      </smtp>
    </mailSettings>
  </system.net>

到目前为止,如果 App.config 中的主机/端口错误,则会出现异常被抛出,但如果用户/密码错误则不会抛出(明显的安全原因)。但是,我已成功使用与源中相同的用户/密码从 Microsoft Outlook 登录。

未收到电子邮件,收件箱 中也未收到电子邮件,垃圾邮件 文件夹中也未收到电子邮件。如何验证服务器端(考虑到它是 microsoftonline.com 的一部分)?我缺少什么?请问我做错了什么?

Can't send mail. Here is my C# source:

  var to = "[email protected]";
  var subject = "test";
  var body = "test mail";
  var message = new MailMessage(from, to, subject, body);
  var client = new SmtpClient { Credentials = new NetworkCredential("[email protected]", "mypassword") };
  client.Send(message);

here is the App.config:

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network
          host="mail.mycompany.com"
          port="25"
          userName="[email protected]"
          password="mypassword"
        />
      </smtp>
    </mailSettings>
  </system.net>

So far, if the host/port in App.config are wrong an exception is thrown, but not if user/pass are wrong (obvious security reasons). However, I've succeeded to log-in from Microsof Outlook with just the same user/pass as in the source.

The email is not received, nor in Inbox, neither in Junk folder. How can I validate the server-side (considering it's a part of microsoftonline.com)? What am I missing? What am I doing wrong, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时常饿 2024-09-21 20:47:04

下面的链接有一个非常简单的发送电子邮件函数的示例,可以从控制台应用程序调用该函数进行测试。我知道这段代码可以工作,因此它可以隔离代码中的任何问题,并且也许可以更清楚地了解问题所在。

SendEmail() – 创建和发送电子邮件C#

The link below has an example of a very simple send email function that can be called from a console application to test. I know this code works, so it could isolate any issue with your code and maybe shed a little more light on where the issue is.

SendEmail() – Create and Send Email Messages in C#

妥活 2024-09-21 20:47:04

我非常确定,如果服务器出于任何原因拒绝电子邮件,您的代码将引发异常。如果不是,则意味着服务器正在接受具有所提供的用户名和密码的电子邮件,并且从技术上讲,邮件已成功“发送”。但这并不能保证任何人都会收到任何电子邮件。您需要找出电子邮件服务器对这些电子邮件执行的操作以及原因。

I am pretty sure that if server rejects the email for any reason your code would throw an exception. If it is not then that implies that the server is accepting the emails with the supplied user name and password and technically the mails are "sending" successfully. However this is no guarantee that anyone will receive any emails. You need to find out what the email server is doing with these emails and why.

淡淡離愁欲言轉身 2024-09-21 20:47:04

我在尝试使用 smtp 时也遇到了防火墙问题。如果您不想与 IT 人员合作并且安装了 Outlook,那么这是一个解决方法。此方法将使用您的默认电子邮件发送。您还需要添加引用(我在 COM 'Microsoft Outlook 14.0 Object Library' 下使用)

     using Outlook = Microsoft.Office.Interop.Outlook;

     private void sendEmail(string DistributionList, string AttachmentDestination)
      {
        //new outlook instance
        Outlook.Application app = new Outlook.Application();

        //new mail object
        Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);

        mail.Subject = "This is coming from a C# script";
        mail.To = DistributionList; //your distribution list "[email protected]"
        mail.Body = "This is the body of an email from a C# script";
        mail.Attachments.Add(AttachmentDestination); //location of attachment (can be ommitted)
        mail.Send();
        app.Quit();
    }

I was having firewall issues trying to use smtp as well. This is a work-around if you don't want to work with IT and do have outlook installed. This method will use your default email to send though. You will also need to add a reference (I'm using under COM 'Microsoft Outlook 14.0 Object Library')

     using Outlook = Microsoft.Office.Interop.Outlook;

     private void sendEmail(string DistributionList, string AttachmentDestination)
      {
        //new outlook instance
        Outlook.Application app = new Outlook.Application();

        //new mail object
        Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);

        mail.Subject = "This is coming from a C# script";
        mail.To = DistributionList; //your distribution list "[email protected]"
        mail.Body = "This is the body of an email from a C# script";
        mail.Attachments.Add(AttachmentDestination); //location of attachment (can be ommitted)
        mail.Send();
        app.Quit();
    }
你穿错了嫁妆 2024-09-21 20:47:04

从 IT 团队得到了答案 - 这是一个配置/安全问题。通过 IT 解决。非常感谢大家。

Got an answer from the IT team - it was a configuration/security issue. Solved with IT. Thanks a lot, everyone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文