Reputation: 1
I'm new to ASP.NET C# and I've been trying to figure out how to do this for a long time. I found a video on Youtube - https://www.youtube.com/watch?v=Baw6O3ME8Go - that shows how to make a simple email app in ASP.NET C#. However, when I run the script, I keep getting different errors. This is what my code looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
namespace my_site
{
public partial class contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void send_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text);
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new System.Net.NetworkCredential("jameshollan79@gmail.com", "************");
client.EnableSsl = false;
client.Send(message);
status.Text = "Thanks for the feedback!";
}
catch (Exception ex)
{
status.Text = ex.StackTrace;
}
}
}
}
One error message showed that it was on line 22 , which is just "{
" after the try
. Another error said that it was line 33 which is "status.Text = ex.StackTrace;
". Another error that it comes up with says that it is line 28 that says "client.Send(message);
".
For that last one, the stacktrace said
"at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at my_site.contact.send_Click(Object sender, EventArgs e)
in C:\Users\Administrator\Desktop\my_site\my_site\contact.aspx.cs:line 28"
Is there something that I'm doing wrong? Or is there a better way to do it?
If it helps, I do have a bit of experience in Classic ASP.
Upvotes: 0
Views: 384
Reputation: 10013
I think you have the to and from in the wrong places. It is:
new MailMessage(from, to, subject, body);
But really it should be:
new MailMessage("jameshollan79@gmail.com",
"jameshollan79@gmail.com",
"Contact Page: " + txtSubject.Text,
txtFrom.Text + "<br />" + txtBody.Text);
Shouldn't give them the ability to edit "to". And don't have your email address on the page or you will start getting a lot of spam.
Upvotes: 1
Reputation: 1027
You have to do following two changes to make it work out:
Enable the SSL You have to make the EnableSSL property to true as you are working with gmail
client.EnableSsl = true;
Google Account Setting
Go to your Google Account setting of your email id "jameshollan79@gmail.com"
Go to "Sign-in & security
" option and under that find "Allow less secure apps
" and turn it "ON". This is because gmail don't want to send any mail from non authentic sources.
Also as @JBrooks pointed out please do correction in from and to part of MailMessage it is MailMessage(from, to, subject, body);
Hope it helps
Upvotes: 2