【问题标题】:Uploaded ASP.NET web application does not send email上传的 ASP.NET Web 应用程序不发送电子邮件
【发布时间】:2015-10-27 17:14:38
【问题描述】:

我正在开发一个网络应用程序,计划向新成员发送确认电子邮件。我是使用 ASPET 开发电子邮件服务的新手。该应用程序运行时没有错误或异常,并且显然在 try 和 catch 块返回异常时发送电子邮件。但是,甚至没有人收到一封邮件。这是代码:

using System;
using System.Net;
using System.Web;
using System.Net.Mail;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Text.RegularExpressions;

public void SendEmailAddressVerificationEmail(string Username, string To)
    {
        MEFManager.Compose(this);
        string encryptedName = Username.Encrypt("verify");

        string msg = "Please click on the link below or paste it into a browser to verify your email account.<BR><BR>" +
                        "<a href=\"" + _configuration.RootURL + "Account/VerifyEmail.aspx?a=" +
                        encryptedName + "\">" +
                        _configuration.RootURL + "Account/VerifyEmail.aspx?a=" +
                        encryptedName + "</a>";

        SendEmail(To, "", "", "Account created! Email verification required.", msg);
    }


public void SendEmail(string To, string CC, string BCC, string Subject, string Message)
    {
        MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS,To);
        if(!string.IsNullOrEmpty(CC))
            mm.CC.Add(CC);

        if (!string.IsNullOrEmpty(BCC))
            mm.Bcc.Add(BCC);

        mm.Subject = Subject;
        mm.Body = Message;
        mm.IsBodyHtml = true;

        Send(mm);
    }


private void Send(MailMessage Message)
    {
        try
        {
        //During developement we will not be sending mails
#if !DEBUG
        SmtpClient smtp = new SmtpClient();
        smtp.Send(Message);
        Console.Write("E-mail sent!");
#endif

        }
        catch(Exception ex)
        {
            Console.Write("Could not send the e-mail - error: " + ex.Message);
        }
    }

web.config 和 web.release.config 文件如下:

Web.config:

<system.net>
 <mailSettings>
  <smtp>
            <network host="localhost" port="25" defaultCredentials="true"/>
  </smtp>
 </mailSettings>
</system.net>

web.release.config:

<smtp from="postmaster@itok.com">
  <network
      host="mail.itok.com"  
      defaultCredentials="true"
      enableSsl="false"             
      port="25"
      userName="postmaster@itok.com"
      password="*********"
      xdt:Transform="Replace"
    />
</smtp>

谁能帮我找出原因?有没有人有想法或建议在哪里寻找答案?这种问题的根源可能是什么?

【问题讨论】:

    标签: asp.net email


    【解决方案1】:

    我无法回答您的问题,但这里有一些提示太长,无法发表评论。

    在您的开发环境中,启用使用传递方法specifiedPickupDirectory 发送邮件,而不是使用条件编译来禁用发送邮件(IF !DEBUG...)。

    这将使您能够在您的开发环境中测试您的代码 - 只需检查电子邮件文件是否在指定目录中创建(该目录必须存在并且可由 Web 应用程序写入)。

    <smtp deliveryMethod="SpecifiedPickupDirectory" from="postmaster@itok.com">
      <network host="localhost"/>
      <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp\mails\"/>
    </smtp>
    

    如果它在您的开发环境中工作,那么您可以假设您的代码可能没问题,问题出在部署的配置上。

    您使用Console.Write 在 Web 应用程序中记录错误看起来很可疑:您希望在哪里看到这些错误?

    【讨论】:

    • 嗨,乔,感谢一百万的回复。我已应用更改,现在我可以在 C:\Temp\mails\ 目录中找到电子邮件。所以现在我确信我的代码可以完美运行(在生成链接电子邮件时)。你知道从哪里去寻找问题的解决方案吗?我打电话给 ISP 的人,他们认为配置是他们已经给我的配置!!!
    • @بابکخلیفهسلطان 接下来我会尝试删除 try/catch 块。如果您的配置有问题,我希望会引发异常,并且我看不到 Console.Write 将如何帮助您查看异常详细信息。
    • 我的问题在这里。该代码要么不抛出任何异常,要么抛出但我无法检测到它。但显然一切似乎都很好。它给出的消息是:“邮件已发送”!!! “Console.Write”也是我的错误。我正在测试一些东西,但我忘了删除它。
    猜你喜欢
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2012-09-24
    • 2014-02-03
    • 1970-01-01
    • 2013-09-21
    • 2015-03-26
    相关资源
    最近更新 更多