简体   繁体   English

在asp.net发送电子邮件

[英]send email in asp.net

In between the head and body I have : 在头部和身体之间,我有:

<script language="C#" runat="server"> 

private void sendmail(Object sender, EventArgs e)
        {

        try
        {

            MailMessage mailObj = new MailMessage();
            mailObj.From = "no-reply@domain.be";
            mailObj.To = "nick@domain.be";
            mailObj.Subject = "Email via site ";
            mailObj.Body = "Dit is een email verstuurd via ASP.net .";
            //mailObj.BodyFormat = MailFormat.Text;
            SmtpMail.SmtpServer = "smtp.one.com";
            SmtpMail.Send(mailObj);
            Response.Write("Email werd succesvol vestuurd");
        }
        catch (Exception x)
        {
            Response.Write("Email werd niet verstuurd: " + x.Message);
        }
    } 
    </script> 

and in my body I have 在我的身体里

<form id="Form1" method="post" runat="server"> 

But no email is being send, why not ?? 但是没有发送电子邮件,为什么不呢?

Because you need to trigger the sendmail method. 因为你需要触发sendmail方法。 There's nothing in your code that calls it. 您的代码中没有任何内容可以调用它。

You could have a button on your markup that invokes sendEmail when it is clicked. 您可以在标记上有一个按钮,在单击时调用sendEmail

For example: 例如:

<asp:button id="btnSend" OnClick="sendmail" runat="server" Text="Send" />

Also, SmtpMail is obsolete . 此外, SmtpMail已经过时 Use SmtpClient 使用SmtpClient

There doesn't seem to be any authentication set for your SMTP server. 似乎没有为您的SMTP服务器设置任何身份验证。

You may want to consider the newer 'SmtpClient' class instead. 您可能想要考虑更新的'SmtpClient'类。 Details are here: http://msdn.microsoft.com/en-us/library/swas0fwc.aspx 详细信息如下: http//msdn.microsoft.com/en-us/library/swas0fwc.aspx

I will virtually guarantee you that either that smtp server does not exist, or you are not passing proper credentials. 我实际上会向您保证,smtp服务器不存在,或者您没有传递正确的凭据。 If you have a GMail account, try with the following code, which will authenticate against and mail using GMail's SMTP server. 如果您有GMail帐户,请尝试使用以下代码,该代码将使用GMail的SMTP服务器进行身份验证和邮件。 Once that is working, work back until you are properly authenticating against your own. 一旦工作正常,请回过头直到您正确地对自己进行身份验证。

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Timeout = 3000;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Host = "smtp.gmail.com";
client.Port = 587;

System.Net.NetworkCredential myCreds = new System.Net.NetworkCredential(
     "Your@emailhere.com"
     "YourPasswordHere"
                "");


client.Credentials = myCreds;
client.EnableSsl = true;

client.Send(message);

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

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