本文介绍了通过C#从我的网站发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code发送电子邮件:

i use the following code to send email :

        public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
        {
            try
            {
                MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
                MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
                var smtp = new SmtpClient
                {
                    Host = "smtp.datagts.net",
                    Port = 587,
                    EnableSsl = false,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = true,
                    Credentials = new System.Net.NetworkCredential("MeEmail@...", "Password")
                };

                using (MailMessage message = new MailMessage(FromAddr, ToAddr)
                {
                    Subject = Subject,
                    Body = Body,
                    IsBodyHtml = IsBodyHTML,
                    BodyEncoding = System.Text.Encoding.UTF8,

                })
                {
                    smtp.Send(message);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

它可以在本地,当我用我的网站在我的本地IIS,但是当我把它上传到我的网站上它不工作,不发送电子邮件,甚至出现任何错误。

It works on local and when i use my web site under my local IIS but when i upload it to my website it does not work and does not send email even any error occurs.

有没有人在那里帮助我这件事?

is there anybody out there to help me about this ?

UPDATE1 :我删除尝试捕捉和捕获错误此消息:发送邮件失败

UPDATE1 : i remove the try catch and catch an error with this message : Failure sending mail

UPDATE2 :我改变我的STMP服务器,并使用我的Gmail帐户,看看这个code:

UPDATE2 : I change my stmp server and use my Gmail account , look at this code :

public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
            {
                try
                {
                    MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
                    MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
                    var smtp = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new System.Net.NetworkCredential("[email protected]", "Password")
                    };

                    using (MailMessage message = new MailMessage(FromAddr, ToAddr)
                    {
                        Subject = Subject,
                        Body = Body,
                        IsBodyHtml = IsBodyHTML,
                        BodyEncoding = System.Text.Encoding.UTF8,

                    })
                    {
                        smtp.Send(message);
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }

现在我得到一个错误尚未:(

and now i get an error yet :(

我得到的MustIssueStartTlsFirst在此的。
我现在想检查@EdFS点,并使用端口25

I get the "MustIssueStartTlsFirst" error that mentioned in this link.I am now trying to check @EdFS point and use port 25

UPDATE3 :这是因为我使用的共享服务器,我才可以更改端口为25,和钢铁这是行不通的给出了同样的错误,我试图从获得支持我服务器备份团队

UPDATE3: It is because i use the shared server , i just can change the port to 25 , and steel it does not work an give the same error, I am trying to get support from my server backup team

推荐答案

假设 SMTP服务器(smtp.datagts.net)运行良好,一些检查项目:

Assuming the SMTP server (smtp.datagts.net) is running fine, some items to check:


  1. 您$​​ C $ C似乎要使用到 UseDefaultCredentials = TRUE ,但在下一行,您将可以提供凭证

  2. 由于在评论中提到检查端口587不会被阻止在您的虚拟主机

  3. 如果您在共享服务器(非专用机)托管,它的可能的ASP.Net设置为中等信任如果这样,你的。

  1. Your code seems to be using UseDefaultCredentials=true, but on the next line your are providing credentials
  2. As mentioned in the comments check that Port 587 isn't blocked at your web host
  3. If you are hosted on a shared server (not a dedicated machine), it's likely ASP.Net is set for medium trust. IF so, you cannot use any port for SMTP other than Port 25.

更新:

要尝试,并获得了错误。在本地(开发)机,将它添加到你的web.config:

To try and get to the error. In your LOCAL (development) machine, add this to your web.config:

<system.web>
...
    <securityPolicy>
      <trustLevel name="Medium" />
    </securityPolicy>
...

ASP.Net你的本地计算机上的完全信任运行。以上设置使当前网站/应用程序,您都在运行工作中的中等信任。您可以删除/必要的注释。这个练习的要点是的尝试的搭配您的虚拟主机设置是什么(这显然是不同的,如果事情在你的本地计算机工作,然后公布时死亡)。这将是很好,只是从你的网络host..but获取信息直到那时....

ASP.Net on your local machine runs in FULL TRUST. The above setting makes the current web site/application you are working on run in medium trust. You can remove/comment as necessary. The point of this exercise is to try and match what your web host settings are (it's obviously different if things work in your local machine then dies when published). It would be nice to just obtain the info from your web host..but until then....

然后尝试两个端口587和25。

Then try both Port 587 and 25.


  • 应该在端口587失败,安全异常(因为中等信任

  • 如果您的邮件服务器的只有的接受端口587 SMTP连接,那么当然,端口25将无法正常工作或者(但你应该得到一个不同的错误)。问题的关键是的...它仍然无法正常工作......的在这种情况下SMTP服务器(smtp.datagts.net)的只有的接受上的连接端口587

  • It should fail on port 587 with a security exception (because of medium trust)
  • If your mail server only accepts SMTP connections on port 587, then of course, port 25 will not work either (but you should get a different error). The point being "...it still doesn't work..." in this case is that the SMTP server (smtp.datagts.net) only accepts connections on port 587

Gmail是同样的故事。您无法使用端口587如果ASP.Net您的虚拟主机设置为中等信任。我经历过很多次 - 在我的本地机器上,将工作的,但只要我能中等信任在我的本地开发机器,它会失败。

GMAIL is the same story. You cannot use Port 587 if your web host settings for ASP.Net is medium trust. I have been through this many times - it will "work" in my local machine, but as soon as I enable medium trust in my local development machine, it will fail.

您应该问你的虚拟主机什么的设置是ASP.Net - 如果它的一些定制设置,您可以要求复制和使用,在你的开发框为好。

You should ask your web host what their settings are for ASP.Net - if its some "custom" setting you can ask for a copy and use that in your dev box as well.

这篇关于通过C#从我的网站发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:06