System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add("[email protected]"); message.Subject = "This is the Subject line"; message.From = new System.Net.Mail.MailAddress("From@online..com");
message.Body = "This is the message body"; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); smtp.Send(message);
And make sure a valid smtp server is setup in your web.config
Send email in code
I have a contact form where once a user clicks submit i would like to redirect the user to another site and send an email.
Example:
User enters their details, selects Contact Webmaster (from a dropdown) and clicks submit.
The user is then redirected to a help page and the Webmaster receives an email.
Heres the code
public class Forms
{
public Forms()
{
}
public enum FormPages
{
Registration
}
public enum FormFieldsets
{
Details
}
[Form("ContactSection", ShowValidationSummary = true, MessageOnSubmit = "Message sent.")]
public class Registration : FormBase
{
public const string MemberTypeAlias = "Member";
public const string MemberGroupName = "Authenticated";
[Field(FormPages.Registration, FormFieldsets.Details, Mandatory = true)]
public string Name { get; set; }
[Field(FormPages.Registration, FormFieldsets.Details, Mandatory = true)]
public string Contact { get; set; }
[Field(FormPages.Registration, FormFieldsets.Details, Type = typeof(DropDownList), Caption=@"Select Type", Prevalues= new string[] {"Webmaster", "IT", "HR"}, Mandatory = true)]
public string ContactReason { get; set; }
public override void Submit()
{
if (ContactReason == "Webmaster")
{
HttpContext.Current.Response.Redirect("http://www.SomeURL.com");
}
else if (ContactReason == "IT")
{
HttpContext.Current.Response.Redirect("http://www.URL.com");
}
else if (ContactReason == "HR")
{
HttpContext.Current.Response.Redirect("http://www.AnotherURL.com");
}
}
}
}
How could i send an email in the code above (preferably within the if/ else if block).
Comment author was deleted
You'll need
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("From@online..com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Send(message);
And make sure a valid smtp server is setup in your web.config
is working on a reply...