.Net smtp不发送电子邮件,没有错误

时间:2013-10-03 11:13:44

标签: c# asp.net .net email smtp

我正在尝试使用我的客户组织的电子邮件服务器通过Web应用程序发送电子邮件。以下是我正在使用的代码。

MailMessage MyMailMessage = new MailMessage();
MyMailMessage.Subject = "Email testing";
MyMailMessage.From = new MailAddress("name@mydomain.com", "My name");
MyMailMessage.To.Add(new MailAddress(strEmail, "Applicant"));

SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.EnableSsl = true;
mySmtpClient.Send(Accepted);

Web.config文件:

<mailSettings>
  <smtp deliveryMethod="Network" from=" name@mydomain.com">
    <network host="smtps.mydomain.com" port="465" enableSsl="true" defaultCredentials="true"
    userName="myName" password="myPassword" />
  </smtp>
</mailSettings>

当我使用gmail smtp详细信息或我当地组织的smtp详细信息时,它的工作正常。由于某种原因,它不起作用,也没有抛出任何错误。

我尝试过调试并检查了“超时”的异常。

我不确定还有什么要检查的。有人可以建议一个解决方案。

注意:我还检查过没有防火墙阻塞端口:465。

谢谢。

亲切的问候,

南基

5 个答案:

答案 0 :(得分:2)

您可以尝试通过在Command Promt(cmd)中键入“telnet smtps.mydomain.com 465”来测试连接。

此致

答案 1 :(得分:1)

您是否确认客户端SMTP服务器已安装有效的SSL证书并且可以接受端口465上的连接?

如果能够这样做,请运行WireShark等数据包嗅探器来检查数据包流。

SysIntenerals TcpView工具是一个更轻量级的实用程序,可以让你看到数据包状态。

答案 2 :(得分:0)

试试这个:

var sysLogin="yourlogin@gmail.com";
var sysPass="y0urP@ss";
var sysAddress = new MailAddress(sysLogin, "Message from me!");

var receiverAddress = new MailAddress("mike@hotmail.com");

var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",   //gmail example
            Port = 587,
            EnableSsl = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(sysLogin, sysPass)
        };

        using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
        {
            smtp.Send(message);
        }

答案 3 :(得分:0)

<system.net>
    <mailSettings>
      <smtp from="emialid.com">
        <network host="domain.com" port="25" userName="emialid.com" password="******" defaultCredentials="false"/>
      </smtp>
    </mailSettings>
    </system.net>



public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)

    {
        try
        {
            SmtpClient smtpClient = new SmtpClient();

            MailMessage message = new MailMessage();

            MailAddress fromAddress;

            fromAddress = new MailAddress(FromMail);

            smtpClient.Host = MailServer;
            smtpClient.Port = 25;

            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = SMTPUserInfo;

            message.From = fromAddress;

            message.To.Add(new MailAddress(To, DisplayName));
            if (CC != "")
                message.CC.Add(new MailAddress(CC, DisplayName));
            if (BCC != "")
                message.Bcc.Add(new MailAddress(BCC, DisplayName));

            message.Subject = Subject;

            message.IsBodyHtml = true;
            message.Body = "Your Password is : " + EmailMessage;

            if (Attachment != "")
                message.Attachments.Add(new Attachment(Attachment));

            message.Priority = MailPriority.High;

            smtpClient.Send(message);
            return "SendEmail";
        }
        catch (Exception ex)
        {
            return "Email :" + ex;
        }

    }
}

}

答案 4 :(得分:0)

谢谢大家的建议。我修好了,我只需要将端口号从465更改为25。