经过身份验证的邮件发送无法在.NET中运行

时间:2014-08-15 07:50:14

标签: asp.net authentication sendmail mailsettings

我有一个脚本从我的.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设置是正确的,但即使我手动将服务器设置输入到课程中 - 它也无法正常工作。

由于

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

原来,问题出在主机提供商方面。不知道他们做了什么,但最终开始工作。