这是我的尝试,我得到了一个错误:
我的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服务器需要安全连接,或者客户端未经过身份验证。 &gt;服务器响应为:5.5.1需要身份验证。
了解更多信息
答案 0 :(得分:0)
请尝试端口465而不是587。 如果我没记错的话,gmail使用587表示TLS,465表示SSL。