我想在点击提交按钮时发送邮件通知。单击按钮时,标签显示your message has been sent
。但是当我检查我的邮件时,我没有收到那封邮件。
这是我的代码:
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("sathishsatu222@gmail.com");
//Configure the address we are sending the mail from **- NOT SURE IF I NEED THIS OR NOT?**
MailAddress address = new MailAddress("satheezkumar93@gmail.com");
msg.From = address;
//Append their name in the beginning of the subject
msg.Subject = "hy";
msg.Body = "hy hw ru?";
SmtpClient client = new SmtpClient("smtp-mail.gmail.com", 25);
NetworkCredential credentials = new NetworkCredential("satheezkumar93@gmail.com", "mypassword here");
client.Credentials = credentials;
client.Host = "smtp-mail.gmail.com";
client.Port = 25;
client.EnableSsl = true;
//Configure an SmtpClient to send the mail.
//Display some feedback to the user to let them know it was sent
lblResult.Text = "Your message was sent!";
}
catch
{
//If the message failed at some point, let the user know
lblResult.Text = "Your message failed to send, please try again.";
}
}
答案 0 :(得分:0)
您需要实际发送消息:
client.Send(msg);
作为旁注,似乎您将SmtpClient
配置为端口25并使用SSL,尽管可能(不使用gmail),但这并不常见。
由于您使用的是gmail,因此您应该启用ssl(您已经这样做了),并使用端口587,而不是25(执行client.Port = 587;
,或删除该行并更改25
in 587
)的构造函数。
另外,我认为Gmail主机是smtp.gmail.com
,而不是smtp-mail.gmail.com
答案 1 :(得分:0)
最简单的邮件方法
protected void btnEmail_Click(object sender, EventArgs e)
{
vr txtmsg = "hello";
var ToMail ="someone@some.com";
using (MailMessage mm = new MailMessage(txtmsg , ToMail ))
{
mm.Subject ="Comments";
mm.Body = "body text";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "yourhost";
smtp.EnableSsl = true;
//Provide those
NetworkCredential NetworkCred = new NetworkCredential("username","pass");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
Response.Write("<script>alert('Mail Sent')</script>");
}
}
Gmail不允许未经授权的登录发送邮件。
请检查OAuth以发送邮件。