0

这是我的尝试我得到一个错误:

我的 SendMail.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="border: 1px solid" align="center">
            <tr>
                <td colspan="2" align="center">
                    <b>Send Mail with Attachment using asp.net</b>
                </td>
            </tr>
            <tr>
                <td>
                    From:
                </td>
                <td>
                    <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    To:
                </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Subject:
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Attach a file:
                </td>
                <td>
                    <asp:FileUpload ID="fileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td valign="top">
                    Body:
                </td>
                <td>
                    <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

我的 SendMail.aspx.cs

using System.Net.Mail;
using System.Net;

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(txtTo.Text);
            //mail.To.Add("amit_jain_online@yahoo.com");
            mail.From = new MailAddress(txtFrom.Text);
            mail.Subject = txtSubject.Text;
            mail.Body = txtBody.Text;
            mail.IsBodyHtml = true;

            //Attach file using FileUpload Control and put the file in memory stream
            if (fileUpload1.HasFile)
            {
                mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
            }
            SmtpClient smtp = new SmtpClient();
             //Or Your SMTP Server Address

            NetworkCredential nc = new NetworkCredential();
            nc.UserName="myusername@gmail.com";
            nc.Password="mypassword";
            smtp.Credentials = nc;
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.UseDefaultCredentials = false;
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }

我收到此错误不知道该怎么做:

SMTP 服务器需要安全连接或客户端未通过身份验证。>server 响应为:5.5.1 需要身份验证。了解更多信息

4

1 回答 1

0

请尝试使用端口 465 而不是 587。如果我没记错的话,gmail 对 TLS 使用 587,对 SSL 使用 465。

于 2013-05-27T09:56:47.673 回答