我正在尝试使用C#ASP.NET从网站发送包含字段名称,电子邮件,类别,主题和消息的电子邮件。我的要求是当用户点击发送按钮时,电子邮件将发送到给定的邮件地址。我在下面解释我的代码。
Contact.aspx:
<form id="form1" class="contactformdetils" runat="server" >
<fieldset>
<div class="clearfix">
<div class="input">
<label>Your name:</label>
<asp:TextBox ID="txtName" name="name" runat="server" ></asp:TextBox>
</div>
<div class="input">
<label>Your email:</label>
<asp:TextBox ID="txtEmail" runat="server" name="email"></asp:TextBox>
</div>
</div>
<div class="clearfix">
<div class="input">
<label>Category:</label>
<asp:TextBox ID="txtCategory" runat="server" name="category"></asp:TextBox>
</div>
<div class="input">
<label>Subject:</label>
<asp:TextBox ID="txtSubject" runat="server" name="subject"></asp:TextBox>
</div>
</div>
<label>Message:</label>
<asp:TextBox ID="txtMessage" runat="server" name="message" Rows="6" TextMode="MultiLine"></asp:TextBox>
<div class="clearfix captcha">
<asp:Button runat="server" Text="Send Message" class="button" id="email_submit" onClick="email_submit_Click" />
<div class="captcha-wrapper">
<asp:Label ID="lblResult" runat="server" Visible="false" />
</div>
</div>
</fieldset>
</form>
Contact.aspx.cs:
protected void email_submit_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
lblResult.Visible = true;
lblResult.Text = "Name field should not keep blank";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
if (txtEmail.Text == "")
{
lblResult.Visible = true;
lblResult.Text = "Email field should not keep blank";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
if (txtCategory.Text == "")
{
lblResult.Visible = true;
lblResult.Text = "Catagory field should not keep blank";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
if (txtSubject.Text == "")
{
lblResult.Visible = true;
lblResult.Text = "Subject field should not keep blank";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
if (txtMessage.Text == "")
{
lblResult.Visible = true;
lblResult.Text = "Message field should not keep blank";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
bool isEmail = Regex.IsMatch(txtEmail.Text.Trim(), @"\A(?:[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)\Z");
if (!isEmail)
{
lblResult.Visible = true;
lblResult.Text = "Enter Valid Email ID..";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
if (txtName.Text.Trim().Length > 0 && txtEmail.Text.Trim().Length > 0 && txtCategory.Text.Trim().Length > 0 && txtSubject.Text.Trim().Length > 0 && txtMessage.Text.Trim().Length > 0 )
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
smtpClient.Host = "localhost";
smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("*********@gmail.com");
message.Subject = txtSubject.Text;
message.IsBodyHtml = false;
message.Body = txtMessage.Text;
smtpClient.Send(message);
lblResult.Visible = true;
lblResult.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblResult.Visible = true;
lblResult.Text = "Send Email Failed.<br>" + ex.Message;
}
}
}
当用户点击发送按钮时,邮件将被发送到此表单上键入的电子邮件地址。
答案 0 :(得分:1)
这可能是由于端口号。我有一次类似的问题。尝试使用端口
587或465
smtpClient.Port = 587;
或
smtpClient.Port = 465;
应该可以正常工作。 如果它们不起作用,请尝试其他端口号。
答案 1 :(得分:1)
SmtpClient smtpClient= new SmtpClient("smtp.gmail.com");
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
答案 2 :(得分:0)
您的代码
smtpClient.Host = "localhost";
smtpClient.Port = 25;
替换为以下内容:
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
修改强>
smtpClient.EnableSsl = true;
<强>更新强>
smtpClient.Credentials = new System.Net.NetworkCredential("From EmailID","Password");
注意:
From EmailID
不是User enterd emailid
。用于向最终用户发送电子邮件的From EmailID
电子邮件。并提供该emailid密码。
并确保您尝试发送群发邮件,然后检查smtpClient.Port
是否应该阻止。
希望这对你有所帮助。
答案 3 :(得分:-1)
如果您使用Gmail
发送邮件,请使用
smtp.Host = "smtp.gmail.com";
而不是
smtpClient.Host = "localhost";