简体   繁体   English

如何从SMTP服务器发送电子邮件?

[英]How can I send emails from my SMTP server?

Hello I have a hosted Windows Server 2008 R2 Enterprise and a purchased domain name. 您好我有一个托管的Windows Server 2008 R2 Enterprise和一个购买的域名。 I have pointed everything on my domain to my server's dns and uploaded my ASP.Net site to the IIS. 我已经将我的域上的所有内容指向我的服务器的dns,并将我的ASP.Net站点上传到IIS。 I have also set up an SMTP Feature so now I'm trying to send emails from my SMTP but I can't because the application says that the user is not authenticated. 我还设置了一个SMTP功能,所以现在我正在尝试从我的SMTP发送电子邮件,但我不能,因为应用程序说用户未经过身份验证。 Here is the source code of my page: 这是我的页面的源代码:

MailMessage activationMail = new MailMessage();
                activationMail.From = new MailAddress("no-reply@mydomain.com", "MyGame");
                activationMail.To.Add(RegistrationEmail.Text);
                StreamReader sRead = new StreamReader(Server.MapPath("~/Mails/ActivationMail.html"));
                string readFile = sRead.ReadToEnd();
                string Strcontent = "";
                Strcontent = readFile;
                Strcontent = Strcontent.Replace("[Name]", RegistrationRealName.Text);
                Strcontent = Strcontent.Replace("[Username]", RegistrationUsername.Text);
                Strcontent = Strcontent.Replace("[Password]", RegistrationPasswordCreate.Text);
                Strcontent = Strcontent.Replace("[useractivation]", useractivation);
                Strcontent = Strcontent.Replace("[Site]", site);
                Strcontent = Strcontent.Replace("[Facebook]", "facebook");
                activationMail.Subject = "My Game - Registration";
                activationMail.Body = Strcontent.ToString();
                sRead.Close();
                activationMail.IsBodyHtml = true;
                activationMail.BodyEncoding = UTF8Encoding.UTF8;
                SmtpClient client = new SmtpClient();
                client.Host = "mydomain"; 
                NetworkCredential cr = new NetworkCredential();
                cr.UserName = "User from AD DS";
                cr.Password = "Password of User";
                client.UseDefaultCredentials = false;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Port = 25;
                client.Timeout = 10000;
                client.Send(activationMail);

Note: My domain name is not the same with my forest's domain. 注意:我的域名与我的林域名不同。 I have also tried to set my forest's domain as the client.Host but same result. 我也尝试将我的林域设置为client.Host但结果相同。 I have these settings on my Web.config 我在我的Web.config上有这些设置

    <mailSettings>
      <smtp from="no-reply@mydomain">
          <network defaultCredentials="false" host="localhost" />
      </smtp>
  </mailSettings>

My SMTP server's settings are these: 我的SMTP服务器的设置如下:

  • IP Address = My server's IP IP地址=我的服务器的IP
  • Authentication = Standar & Windows authentication are checked, TSL is checked, Default domain is set to my domain (not my MX record mail.domain). 验证=检查标准和Windows身份验证,检查TSL,默认域设置为我的域(不是我的MX记录mail.domain)。 Anonymous authentication is not checked. 不检查匿名身份验证。
  • Connection = Only my server's IP has access 连接=仅我的服务器的IP有权访问
  • Relay = Only my purchased domain (not MX record) has access 中继=只有我购买的域(不是MX记录)才能访问
  • Outbound security = Standar authentication (User from AD DS - same credentials with the ones at my cs file above), TLS enabled 出站安全性=标准身份验证(来自AD DS的用户 - 与上面我的cs文件中的凭据相同的凭据),启用了TLS
  • Advanced delivery options = I set my domain name there (not MX record - gives me an error) 高级交付选项=我在那里设置我的域名(不是MX记录 - 给我一个错误)

Does anybody know what am I doing wrong? 有人知道我做错了什么吗?

Add the following code as you haven't provided any credentials to the smtp: 添加以下代码,因为您没有向smtp提供任何凭据:

client.Credentials = cr; 

Like: 喜欢:

 NetworkCredential cr = new NetworkCredential();
 cr.UserName = "User from AD DS";
 cr.Password = "Password of User";
 client.Credentials = cr; 
 client.UseDefaultCredentials = false;
 client.DeliveryMethod = SmtpDeliveryMethod.Network;
 client.Port = 25;
 client.Timeout = 10000;
 client.Send(activationMail);

try adding these two line: 尝试添加这两行:

client.EnableSsl = false;
client.Credentials = cr;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM