简体   繁体   English

ASP.NET表单不发送电子邮件

[英]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 在C:\\ Users \\ PC \\ Desktop \\ Projects \\ emailform \\ emailform \\ index.aspx.cs:line中的emailform.index.send_Click(Object sender,EventArgs e)的System.Net.Mail.SmtpClient.Send(MailMessage消息)处28

Anyone have any ideas? 有人有想法么?

I just started learning C# and ASP.NET , so it could probably be something obvious. 我刚刚开始学习C#和ASP.NET ,所以可能很明显。

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#: 这是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. 看来您的SmtpClient设置有麻烦。

You use port 25 and SSL. 您使用端口25和SSL。

But by default port 25 do not use SSL encryption. 但是默认情况下,端口25不使用SSL加密。 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: 尝试修改SSL / TLS的服务器名称和端口号,使其看起来像这样:

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代码应类似于以下示例:

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# 使用C#发送电子邮件

Send mail to outlook account ASP.Net C# 发送邮件到Outlook帐户ASP.Net C#

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

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