I am a ASP novice and am troubleshooting a form for work. None of us here are ASP experts as we use PHP. But I am on the bottom of PHP experience as well, mostly working with HTML/CSS alone. My current forms credentials look like:
rotected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsValid Then Exit Sub
Dim SendResultsTo As String = "email to"
Dim smtpMailServer As String = "email server"
Dim smtpUsername As String = "email username"
Dim smtpPassword As String = "password"
Dim MailSubject As String = "Form Results"
How would I go about making this form send to a Gmail address? I know I must include the port (587) somewhere, but cannot figure out where, as this form doesn’t match the syntax of any other forms I have seen. Any help would be greatly appreciated!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can add this in your web.config file
<system.net>
<mailSettings>
<smtp from="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671e0812152a060e0b2e0327000a060e0b4904080a">[email protected]</a> ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="552c3a202738343c39153238343c397b363a38">[email protected]</a>" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
Method 2
protected void SendMail()
{
MailMessage msg = new MailMessage();
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
try
{
msg.Subject = "Add Subject";
msg.Body = "Add Email Body Part";
msg.From = new MailAddress("Valid Email Address");
msg.To.Add("Valid Email Address");
msg.IsBodyHtml = true;
client.Host = "smtp.gmail.com";
System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("Valid Email Address", "Password");
client.Port = int.Parse("587");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicauthenticationinfo;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(msg);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
Method 3
Create a System.Net.Mail.SmtpClient object, set the SMTP server info you are using.
Then create a System.Smtl.MailMessage with the message data and send it:
using (System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient()) {
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("from*where.com", "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295d46695e414c5b4c074a4644">[email protected]</a>") {
IsBodyHtml = true,
Subject = "Subject text",
Body = messageBody,
}) {
mail.Send(message);
} // using
You can configure your SmtpClient in the constructor, we use web.comfig, so I don’t have that code.
Method 4
Google now blocks sign-ins from an app (even over SSL) to send an email. You have two options:
- Enable less-secure apps for the account you are using; OR
- Generate an app-password from Google (recommended)
For option 2, you will have to enable 2-factor authentication before you can generate an app pasword.
Here are the steps I took to get this working on my ASP.NET web app
How to configure an ASP.NET web app with Gmail SMTP
Method 5
Try this.
Dim client As New Net.Mail.SmtpClient
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e596808b818097a58288848c89cb868a88">[email protected]</a>", "password")
client.Host = "smtp.gmail.com"
client.Port = 587
client.EnableSsl = True
client.Send("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d2e383339382f1d3a303c3431733e3230">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dba9beb8b2beadbea99bbcb6bab2b7f5b8b4b6">[email protected]</a>","subject","body")
Method 6
If you’re using 2-step verification, don’t forget to generate an application specific password and use that, not the password you use to log on to Gmail.
(Sorry I can’t add this as a comment. Not enough rep at the time of posting this.)
Method 7
There’s no shortage of tutorials on how to send an email from within .NET.
Essentially you want a System.Net.Mail.SmtpClient object to interact with the SMTP server, a System.Net.Mail.MailMessage object to hold the message data, and configuration data in your config file to direct the client on how/where to send the message.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0