简体   繁体   中英

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. There's nothing in your code that calls it.

You could have a button on your markup that invokes sendEmail when it is clicked.

For example:

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

Also, SmtpMail is obsolete . Use SmtpClient

There doesn't seem to be any authentication set for your SMTP server.

You may want to consider the newer 'SmtpClient' class instead. Details are here: 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. If you have a GMail account, try with the following code, which will authenticate against and mail using GMail's SMTP server. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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