问题描述
亲爱的所有
我想创建一个访问者向网站管理员发送评论的页面。但使用以下代码将邮件发送给发件人而不是站点管理员。请问我在这里犯了什么错误。我的代码在这里。
public static string SendMail(String to,String subject,String body,Boolean isBodyHtml,MailPriority mailPriority)
{
尝试
{
//配置邮件客户端(可能需要额外的
//经过身份验证的SMTP服务器的代码)
SmtpClient mailClient = new SmtpClient(smtp.gmail.com,587);
//设置网络凭证
mailClient.Credentials = new NetworkCredential([email protected],myadminpassword);
//启用ssl
mailClient.EnableSsl = true;
//创建邮件消息(来自,to,subject,body)
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress([email protected]);
mailMessage.To.Add(to);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.Priority = mailPriority;
//发送邮件
mailClient.Send(mailMessage);
返回消息已成功发送;
}
catch(例外情况)
{
返回ex.Message;
}
}
protected void btnSendmail_Click(object sender, EventArgs e)
{
string s = SendMail(txtSender.Text,txtSubj.Text,txtMessage.Text,true,mailPriority);
lblResponse.Text = s;
}
public MailPriority mailPriority {get;组; }
}
Dear All
I want to crate a page on which the visitor send his comments to the website admin. but with the following code the mail is sent to the sender instead of the site admin. plz identify what mistake I am doing here. my code is here.
public static string SendMail(String to, String subject, String body, Boolean isBodyHtml, MailPriority mailPriority)
{
try
{
// Configure mail client (may need additional
// code for authenticated SMTP servers)
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
// set the network credentials
mailClient.Credentials = new NetworkCredential("[email protected]", "myadminpassword");
//enable ssl
mailClient.EnableSsl = true;
// Create the mail message (from, to, subject, body)
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("[email protected]");
mailMessage.To.Add(to);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.Priority = mailPriority;
// send the mail
mailClient.Send(mailMessage);
return "Message have been sent successfully";
}
catch (Exception ex)
{
return ex.Message;
}
}
protected void btnSendmail_Click(object sender, EventArgs e)
{
string s= SendMail(txtSender.Text,txtSubj.Text,txtMessage.Text,true,mailPriority);
lblResponse.Text = s;
}
public MailPriority mailPriority { get; set; }
}
这篇关于发送邮件有访客评论的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!