简体   繁体   中英

ASP.NET Form not sending email

I am trying to make a form send an email to myself, but for some reason it is not working, and I can't figure out why.

When I submit the form it displays the stack trace of:

at System.Net.Mail.SmtpClient.Send(MailMessage message) at emailform.index.send_Click(Object sender, EventArgs e) in C:\\Users\\PC\\Desktop\\Projects\\emailform\\emailform\\index.aspx.cs:line 28

Anyone have any ideas?

I just started learning C# and ASP.NET , so it could probably be something obvious.

Here's the code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="emailform.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <br /> <br />
        <table align="center" width="60%">
            <tr>
                <td>to:</td>
                <td><asp:TextBox ID="to" runat="server" Text="email@gmail.com" width="60%" ></asp:TextBox></td>
            </tr>

            <tr>
                <td>from:</td>
                <td><asp:TextBox ID="from" runat="server" Text="myemail@outlook.com" Width="60%"></asp:TextBox></td>
            </tr>

            <tr>
                <td>subject</td>
                <td><asp:TextBox ID="subject" runat="server" Text="TEST" Width="60%"></asp:TextBox></td>
            </tr>

            <tr>
                <td>body</td>
                <td><asp:TextBox ID="body" runat="server" Text="this is a test message" Height="30%" Width="60%" TextMode="MultiLine"></asp:TextBox></td>
            </tr>

            <tr>
                <td></td>
                <td><asp:Button ID="send" onClick="send_Click" runat="server" Text="send" /></td>
            </tr>

            <tr>
                <td></td>
                <td><asp:Label ID="status" runat="server"></asp:Label></td>
            </tr>
        </table>
    </form>
</body>
</html>

Here's the C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace emailform
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void send_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage message = new MailMessage(to.Text, from.Text, subject.Text, body.Text);
                message.IsBodyHtml = true;

                SmtpClient client = new SmtpClient("smtp.live.com", 25);
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential("myemail@outlook.com", "mypassword");
                client.Send(message);
                status.Text = "Message sent successfully!";
            }
            catch(Exception ex)
            {
                status.Text = ex.StackTrace;
            }
        }
    }
}

It seems you have a trouble with your SmtpClient setting.

You use port 25 and SSL.

But by default port 25 do not use SSL encryption. Please review next link for details.

Try to change your code like it:

client.EnableSsl = false;

I Tried Your Code and there is nothing wrong with your code.

I think you are entering Wrong Network Credential.

Just Change This

MailMessage message = new MailMessage(to.Text, from.Text, subject.Text, body.Text);

To This

MailMessage message = new MailMessage(from.Text, to.Text, subject.Text, body.Text);

It will Work.

图书馆也会检查它是否也是真实的电子邮件,并提交可能的真实电子邮件。

The problem occurs because you're either using wrong port number or wrong credential settings:

SmtpClient client = new SmtpClient("smtp.live.com", 25);

Try modifying server name and port number for SSL/TLS so that it looks like this:

SmtpClient client = new SmtpClient("smtp.live.com", 587);

// alternative
SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);

Additionally because you're using custom credentials, it is necessary to turn off default credential option before adding your own:

client.UseDefaultCredentials = false;

Finally, the complete SmtpClient code to send e-mail should be like example below:

SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("myemail@outlook.com", "mypassword");
client.Send(message);

References:

Sending email in C#

Send mail to outlook account ASP.Net C#

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