<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
<p>
Please Fill the Following to Send us an E-Mail. We will get back to you ASAP!
</p>
<p>
Your name:
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
ControlToValidate="YourName" ValidationGroup="save" /><br />
<asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
Your email address:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="YourEmail" ValidationGroup="save" /><br />
<asp:TextBox ID="YourEmail" runat="server" Width="250px" />
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
ValidationGroup="save" ForeColor="Red" /><br />
Subject:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
ControlToValidate="YourSubject" ValidationGroup="save" /><br />
<asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
Your Question:
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
ControlToValidate="Comments" ValidationGroup="save" /><br />
<asp:TextBox ID="Comments" runat="server"
TextMode="MultiLine" Rows="10" Width="400px" />
</p>
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Send" ValidationGroup="save" Height="36px" OnClick="btnSubmit_Click" Width="86px" />
</p>
</asp:Panel>
我的C#代码:
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(YourEmail.Text);
// Recipient e-mail address.
Msg.To.Add("*****@gmail.com");
Msg.Subject = YourSubject.Text;
Msg.Body = Comments.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("****@gmail.com", "*********");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
DisplayMessage.Text = "Thanks for Contacting us";
// Clear the textbox valuess
YourName.Text = "";
YourSubject.Text = "";
Comments.Text = "";
YourEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
任何帮助将不胜感激
答案 0 :(得分:0)
您需要输入正确的credential
来发送邮件。
见这里
ASPX代码
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
<p>
Please Fill the Following to Send us an E-Mail. We will get back to you ASAP!
</p>
<p>
Your name:
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
ControlToValidate="YourName" ValidationGroup="save" /><br />
<asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
Your email address:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="YourEmail" ValidationGroup="save" /><br />
<asp:TextBox ID="YourEmail" runat="server" Width="250px" />
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
ValidationGroup="save" ForeColor="Red" /><br />
Subject:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
ControlToValidate="YourSubject" ValidationGroup="save" /><br />
<asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
Your Question:
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
ControlToValidate="Comments" ValidationGroup="save" /><br />
<asp:TextBox ID="Comments" runat="server"
TextMode="MultiLine" Rows="10" Width="400px" />
</p>
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Send" ValidationGroup="save" Height="36px" OnClick="btnSubmit_Click" Width="86px" />
</p>
</asp:Panel>
CS CODE
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(YourEmail.Text);
// Recipient e-mail address.
Msg.To.Add("test@gmail.com");
Msg.Subject = YourSubject.Text;
Msg.Body = Comments.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("YOURGMAILID", "YOURGMAIL PASSWORD"); // IT SHOULD BE CORRECT TO WORK
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
// DisplayMessage.Text = "Thanks for Contacting us";
// Clear the textbox valuess
YourName.Text = "";
YourSubject.Text = "";
Comments.Text = "";
YourEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
希望它有所帮助。 :)