0

我有一个脚本从我的 .net 应用程序发送邮件。

如果我不想延迟接收电子邮件(目前延迟几乎是发送后的一天),我的托管服务提供商要求对电子邮件进行身份验证。

我已经构建了带有身份验证的邮件发送脚本,但它不起作用 - 它不会产生任何错误或异常,但仍然没有立即收到电子邮件。

另一方面,在经典 asp 作品中制作的类似脚本,我立即收到邮件。

我错过了什么吗?

这是asp脚本(有效):

<%
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "sender@mydomain.com"
objEmail.To = "recipient@gmail.com"
objEmail.Subject = "Test Mail"
objEmail.Textbody = "Test Mail"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myusername"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
objEmail.Configuration.Fields.Update
objEmail.Send
If Not err.number=0 Then
Response.write "ERROR: " & err.Description
err.Clear
end if
%>

这是 .NET(不起作用):

public static void SendEmail(string senderName, string senderEmail, string recipient, string comments, string subject, bool inTemplate)
{
    MailAddress from = new MailAddress(senderEmail);
    MailAddress to = new MailAddress(recipient);
    MailMessage mail = new MailMessage(from, to);
    mail.ReplyToList.Add(senderEmail);
    mail.Subject = subject;
    mail.IsBodyHtml = true;
    mail.Body = comments;

    Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
    MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
    NetworkCredential basicCredentials = new NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.Host = mailSettings.Smtp.Network.Host;
    smtpClient.Port = mailSettings.Smtp.Network.Port;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = basicCredentials;

    try
    {
        smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

这些是我的 web.config 设置:

  <system.net>
    <mailSettings>
      <smtp>
        <network host="mail.mydomain.com" userName="myusername" password="mypassword" port="25" />
      </smtp>
    </mailSettings>
  </system.net>

web.config 设置是正确的,但即使我将服务器设置手动输入到课程中 - 它也不起作用。

谢谢

4

2 回答 2

0

事实证明,问题出在托管服务提供商方面。不知道他们做了什么,但它最终开始起作用。

于 2014-10-02T08:16:29.653 回答
0

尝试这个:

 MailMessage mailMsg = new MailMessage();
    mailMsg.To.Add("test@hotmail.com");
                // From
    MailAddress mailAddress = new MailAddress("you@hotmail.com");
    mailMsg.From = mailAddress;

    // Subject and Body
    mailMsg.Subject = "subject";
    mailMsg.Body = "body";

    // Init SmtpClient and send on port 587 in my case. (Usual=port25)
    SmtpClient smtpClient = new SmtpClient("mailserver", 587);
    System.Net.NetworkCredential credentials = 
       new System.Net.NetworkCredential("username", "password");
    smtpClient.Credentials = credentials;

    smtpClient.Send(mailMsg);
于 2014-08-15T07:54:29.523 回答