我正在尝试使用ASP.NET和C#设计“联系我们”页面。
以下是“发送”按钮后面的代码:
try
{
if (Page.IsValid)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("SENDER@gmail.com");
mailMessage.To.Add("RECEIVER@gmail.com"); //can have multiple here
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = "<b>Sender Name:</b>" + txtName.Text + "<br/>"
+ "<b>Sender Email:</b>" + txtEmail.Text + "<br/>"
+ "<b>Comments:</b>" + txtComments.Text;
//for the HTML tags mentioned in the body above
mailMessage.IsBodyHtml = true;
//IN THE TUTORIAL, HE IS USING GMAIL'S SMTP SERVER:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials =
new System.Net.NetworkCredential("SENDER@gmail.com", "PASSWORD");
smtpClient.Send(mailMessage);
Label1.Text = "Thank you for contacting us";
Label1.ForeColor = System.Drawing.Color.Blue;
txtName.Enabled = false;
txtEmail.Enabled = false;
txtComments.Enabled = false;
txtSubject.Enabled = false;
Button1.Enabled = false;
}
}
catch (Exception ex)
{
// Log - Event Viewer or table
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "There is an unknown problem. Please try later";
}
在我所遵循的教程中,您需要指定发件人的电子邮件和电子邮件。 smtpClient.Credentials的密码。
有没有办法提交此电子邮件而无需知道发件人的密码?
答案 0 :(得分:0)
出于安全和/或配额原因,通常需要某种形式的身份验证才能通过SMTP服务器发送电子邮件。如果您绝对必须在没有身份验证的情况下发送和发送电子邮件,那么有很多“无身份验证”的SMTP服务器愿意为您发送电子邮件。