提交时发送邮件'按钮点击
本文关键字:按钮 提交 | 更新日期: 2023-09-27 18:05:47
我有一个4页的ASP。. NET表单,它在会话中存储数据。当点击第三页上的"提交"按钮时,我希望输入的详细信息通过电子邮件发送到我的hotmail帐户,但我似乎无法让它工作,因为它总是直接落入我的catch
。
HTML
<table>
<tr>
<td>Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server" Columns="50"></asp:TextBox>
</td>
</tr>
<tr>
<td>Subject:</td>
<td>
<asp:DropDownList ID="ddlSubject" runat="server">
<asp:ListItem>Ask a question</asp:ListItem>
<asp:ListItem>Report a bug</asp:ListItem>
<asp:ListItem>Customer support ticket</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Message:</td>
<td>
<asp:TextBox ID="txtMessage" runat="server" Columns="40" Rows="6" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
</table>
我的onclick
代码是
using System.Net.Mail;
using System.Net;
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("**SHOULD THIS BE MY EMAIL ADDRESS?**");
//Configure the address we are sending the mail from **- NOT SURE IF I NEED THIS OR NOT?**
//MailAddress address = new MailAddress("SenderAddress@gmail.com");
//msg.From = address;
//Append their name in the beginning of the subject
msg.Subject = txtName.Text + " : " + ddlSubject.Text;
msg.Body = txtMessage.Text;
//Configure an SmtpClient to send the mail.
SmtpClient client = new SmtpClient("smtp.live.com", 465);
client.EnableSsl = true; //only enable this if your provider requires it
//Setup credentials to login to our sender email address ("UserName", "Password")
NetworkCredential credentials = new NetworkCredential("MY@EMAIL ADDRESS", "**SHOULD THIS BE MY PASSOWRD?**");
client.Credentials = credentials;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
lblResult.Text = "Your message was sent!";
//Clear the form
txtName.Text = "";
txtMessage.Text = "";
}
catch
{
//If the message failed at some point, let the user know
lblResult.Text = "Your message failed to send, please try again.";
}
}
有谁能帮我一下吗?
我从http://www.serversmtp.com/en/smtp-hotmail获得了SMTP详细信息,但它总是失败
我已经更新了你的代码,尝试使用相同的电子邮件From和to,因为你是发送给自己,也使用你的hotmail密码作为NetworkCredential实例化的一部分:
using System.Net.Mail;
using System.Net;
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("youremail@hotmail.com");
//Configure the address we are sending the mail from **- NOT SURE IF I NEED THIS OR NOT?**
MailAddress address = new MailAddress("youremail@hotmail.com");
msg.From = address;
//Append their name in the beginning of the subject
msg.Subject = txtName.Text + " : " + ddlSubject.Text;
msg.Body = txtMessage.Text;
//Configure an SmtpClient to send the mail.
SmtpClient client = new SmtpClient("smtp.live.com", 465);
client.EnableSsl = true; //only enable this if your provider requires it
//Setup credentials to login to our sender email address ("UserName", "Password")
NetworkCredential credentials = new NetworkCredential("youremail@hotmail.com", "YourPassword");
client.Credentials = credentials;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
lblResult.Text = "Your message was sent!";
//Clear the form
txtName.Text = "";
txtMessage.Text = "";
}
catch
{
//If the message failed at some point, let the user know
lblResult.Text = "Your message failed to send, please try again.";
}
}
还要确保smtp.live.com的465端口号是正确的。你可以试试587,而不是从雅虎发送电子邮件!, GMail, Hotmail (c#)