我想使用此代码发送电子邮件,但出现问题 所以我该如何解决这个问题
<asp:TextBox ID="To" runat="server"></asp:TextBox><br />
<asp:TextBox ID="from" runat="server"></asp:TextBox><br />
<asp:TextBox ID="subject" runat="server"></asp:TextBox><br />
<asp:TextBox ID="body" runat="server"></asp:TextBox><br />
<asp:Button ID="Button3" OnClick="send" runat="server" Text="send" />
protected void send(object sender, EventArgs e)
{
MailMessage message = new MailMessage(To.Text,from.Text,subject.Text,body.Text);
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("email","pass");
client.Send(message);
}
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at AmbulanceManagementSystem.Front_End_Paramedics.SavePatientInfo.send(Object sender, EventArgs e) in c:\Users\Zoulfikar\Documents\Visual Studio 2013\Projects\AmbulanceManagementSystem\AmbulanceManagementSystem\Front End Paramedics\SavePatientInfo.aspx.cs:line 161
答案 0 :(得分:1)
我只是遇到了同样的问题,您必须打开“安全性较低的访问权限”。假设密码是正确的,那么您的收件箱中应该有一封电子邮件,说明这一点。这是我正在使用的代码。
public void SendEmail(string address, string attatchment)
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("FromEmail@gmail.com");
mail.To.Add("ToEmail@gmail.com");
mail.Subject = "Report";
mail.Body = "Report";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment(attatchment));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("FromEmail@gmail.com", "Password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}