在 ASP .Net 中发送电子邮件

发布于 2024-12-17 12:03:01 字数 920 浏览 0 评论 0原文

如何使用outlook地址在ASP .Net上发送电子邮件?

我尝试过这段代码,但没有成功:

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))

mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"

Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)

smtpClient.Send(mailMessage) //--> got error here

但是当我执行代码时,它收到此错误消息:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

我尝试添加一行代码:

smtpClient.Credentials = New System.Net.NetworkCredential(用户名,密码)

但仍然无法发送电子邮件。

有人可以帮忙吗?

How to send e-mail on ASP .Net using outlook address??

I've tried this code but no luck:

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))

mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"

Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)

smtpClient.Send(mailMessage) //--> got error here

But while I'm execute the code, it got this error message:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

I've tried to add a line of code:

smtpClient.Credentials = New System.Net.NetworkCredential(username, password)

But still can't send the e-mail.

Anyone can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

薔薇婲 2024-12-24 12:03:01
  • 在设置新凭据之前尝试 smtpClient.UseDefaultCredentials = false;
  • 尝试根据您的环境将 smtpClient.EnableSsl 设置为 true / false
  • Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
  • Try to set smtpClient.EnableSsl to true / false depending on your environment
愿得七秒忆 2024-12-24 12:03:01

我猜您正在使用 Exchange 2007 或更高版本作为后端?

无论如何,您的邮件服务器不允许您匿名发送邮件。您需要在代码中提供用户名/密码,或者允许来自网络服务器的未经身份验证的中继。

与 IT 人员讨论他们的喜好。

I'm guessing you are using Exchange 2007 or later as backend?

Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.

Talk to your IT guys what they prefer.

夜唯美灬不弃 2024-12-24 12:03:01

我是用c#做的。这可能对你有帮助。请检查。

   MailMessage msg1 = new MailMessage();
                        msg1.From = strEFrom;
                        msg1.To = strETo;
                        msg1.Cc = strECC;
                        msg1.Subject = "Hi";
                        msg1.Priority = MailPriority.High;
                        msg1.BodyFormat = MailFormat.Html;
                        msg1.Body = strBody;
                        SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
                        SmtpMail.Send(msg1);

并在 web.config 文件中

<appSettings>
          <add key="MailServer" value=""/>
</appSettings>

I did it using c#.This may help you.Please check.

   MailMessage msg1 = new MailMessage();
                        msg1.From = strEFrom;
                        msg1.To = strETo;
                        msg1.Cc = strECC;
                        msg1.Subject = "Hi";
                        msg1.Priority = MailPriority.High;
                        msg1.BodyFormat = MailFormat.Html;
                        msg1.Body = strBody;
                        SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
                        SmtpMail.Send(msg1);

and in web.config file

<appSettings>
          <add key="MailServer" value=""/>
</appSettings>
诺曦 2024-12-24 12:03:01

尝试这些设置,检查 .EnableSSL 属性是否为 true/false 以及您的邮件服务器所在的smtp post number侦听外发邮件。

当您没有在 Outlook 中为 gmail Outlook 配置设置启用 SSL 设置时,它会给出相同的错误,但找到了 SSL 设置的原因。

我们尝试一下,可能会解决您的问题。

msg.IsBodyHtml = true;
            msg.Body = mailContent;

            SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
            NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = NetCrd;
            mailClient.EnableSsl = false;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(msg);

如果它不能解决您的问题,请检查您的邮件服务器/客户端的问题。

Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.

When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..

well try this may it will solve your problem..

msg.IsBodyHtml = true;
            msg.Body = mailContent;

            SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
            NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = NetCrd;
            mailClient.EnableSsl = false;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(msg);

if it does not solve your problem then check your mail server/ client for the problem.

伏妖词 2024-12-24 12:03:01
      public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
      {
        try
        {
         NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress(from);
         msg.To.Add(new MailAddress(to));
         msg.Subject = subject;
         msg.Body = message;
         msg.IsBodyHtml = false;
         SmtpClient client = new SmtpClient("smtp.gmail.com");
         client.EnableSsl = true;
         client.UseDefaultCredentials = false;
         client.Credentials = loginInfo;
         client.Send(msg);

         return true;
        }
       catch (Exception)
       {
         return false;
       }

   }

我使用此代码从我的 Gmail 帐户发送邮件。

      public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
      {
        try
        {
         NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress(from);
         msg.To.Add(new MailAddress(to));
         msg.Subject = subject;
         msg.Body = message;
         msg.IsBodyHtml = false;
         SmtpClient client = new SmtpClient("smtp.gmail.com");
         client.EnableSsl = true;
         client.UseDefaultCredentials = false;
         client.Credentials = loginInfo;
         client.Send(msg);

         return true;
        }
       catch (Exception)
       {
         return false;
       }

   }

I use this code to send mail from my gmail account.

残龙傲雪 2024-12-24 12:03:01

遇到同样的错误,移动了 client.UseDefaultCredentials = false;在设置 client.Credentials 之前一切正常。

was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文