3

我正在使用 asp.net 3.5 和 C#。

我想从 asp.net 发送邮件,因为我从我的托管服务提供商那里得到了一些详细信息

这些是:

  • mail.MySite.net
  • 用户名
  • 密码

但是我无法通过这些详细信息发送邮件,我在 web.config 文件中进行了以下更改:

<system.net>
    <mailSettings>
        <smtp>
            <network
                 host="mail.MySite.net"
                 port="8080"
                 userName="UserName"
                 password="Password" />
        </smtp>
    </mailSettings>
</system.net>

另外,在后面的代码中我正在编写这个函数:

MailMessage mail = new MailMessage("webmaster@mySite.net", "XYZ@gmail.com");
mail.Subject = "Hi";
mail.Body = "Test Mail from ASP.NET";
mail.IsBodyHtml = false;

SmtpClient smp = new SmtpClient();
smp.Send(mail);

但由于消息发送失败,我收到错误消息。

请让我知道我做错了什么以及我必须做些什么才能使其正常工作。

提前致谢。

4

5 回答 5

2

您需要提供客户端凭据吗?

smp.Credentials = CredentialCache.DefaultNetworkCredentials;

或者

smp.Credentials = new NetworkCredential("yourUserID", "yourPassword", "yourDomainName");

此外,您得到的确切异常也会很有用。

如需更多帮助,请参阅Scott Guthrie 的帖子。

于 2010-05-26T21:15:37.353 回答
2

我怀疑端口 8080 是正确的 smtp 端口。也许是 25 或 587 端口。

于 2010-05-26T21:17:16.433 回答
1

通过asp.net c#发送邮件并不是一件复杂的事情……只要我们知道smtp端口和主机……

            MailAddress to = new MailAddress("Email Id");

            MailAddress from = new MailAddress("Email Id");

            MailMessage mail = new MailMessage(from, to);

            mail.Subject = "";
            mail.Body = "";


            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;

            smtp.Credentials = new NetworkCredential(
                "Email Id", "Password");
            smtp.EnableSsl = true;

            smtp.Send(mail);
于 2013-02-25T09:12:07.437 回答
1

不使用 SMTP,使用 Microsoft.Office.Interop.Outlook 添加;参考

        Application app = new Application();
        NameSpace ns = app.GetNamespace("mapi");
        ns.Logon("Email-Id", "Password", false, true);
        MailItem message = (MailItem)app.CreateItem(OlItemType.olMailItem);
        message.To = "To-Email_ID";
        message.Subject = "A simple test message";
        message.Body = "This is a test. It should work";

        message.Attachments.Add(@"File_Path", Type.Missing, Type.Missing, Type.Missing);

        message.Send();
        ns.Logoff();
于 2013-03-11T04:23:12.720 回答
0

我的代码与您的代码非常相似,我认为不同之处在于您需要在 SMTP 客户端的构造函数中为您的 SMTP 服务器提供 IP 地址。

        MailMessage Email = new MailMessage("donotreply@test.com", "receiver@test.com");
        Email.Subject = "RE: Hello World.";
        Email.Body = "Hello World";
        Email.IsBodyHtml = false;
        SmtpClient Client = new SmtpClient(SMTP_SERVER); //This will be an IP address
        Client.Send(Email);

希望有帮助!:)

(顺便说一句,我在 Winforms、Windows 服务和 ASP .NET 中使用过它。在 ASP .NET 中,我不需要在 aspx 页面中提供任何内容。)

于 2010-05-26T22:33:02.470 回答