Cant send email using Gmail SMTP - c#

I have a web with contact form - I want feedback being sent to my gmail account. For this I created another, dummy gmail account for sending these messages. Code looks like this
MailMessage msg = new MailMessage();
msg.To.Add("to_mail#gmail.com");
msg.From = new MailAddress("from_mail#gmail.com");
msg.Subject = "Feedback";
msg.Body = txtName.Text + " " + txtEmail.Text + " " + txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("from_mail#gmail.com", "password");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);
This code works fine when I run it from localhost from my VS project, but once I compile it and upload to the web, its stops working with this error.
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Does anyone have an idea what caused it and how to fix it? I already enabled access for less secure applications in my gmail account. Tried also 465 port, but that doesnt work either.
Thank a lot, Jozef

Finally found an answer. Had to click this link with my "From" Gmail account logged in.
https://accounts.google.com/b/0/DisplayUnlockCaptcha

Possible Reason:
In gmail, If the current login region and the previous login region is
different, then gmail ask us some security questions before login. So
better try to login once via browser in the system which you are using
that C# app.

Related

C#. SMTP The server response was: 5.7.0 Authentication Required

Error: The SMTP server requires a secure connecton or the client was not authenticated. The server
response was: 5.7.0 Authentication Required. Learn more at.
private void SendMail(string email, string firstname)
{
MailAddress from = new MailAddress($"{email}", $"{firstname}");
MailAddress to = new MailAddress($"{email}");
MailMessage m = new MailMessage(from, to);
m.Subject = "TEST";
m.Body = "<h2>TEST</h2>";
m.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new NetworkCredential("jewralyapp#mail.ru", "password");
smtp.EnableSsl = true;
smtp.Send(m);
}
I want to send a letter to the mail, but the error that I indicated above comes out, here is the code, can you tell me what is wrong?
I see that your email is #mail.ru and the server is gmail, is that correct? I think you need to use your gmail account.
You also need enable weaker credentials on your gmail account. Forgot what they actually call it but it's on gmail security setting.
I had also faced the same problem.
Your SmtpClient is gmail, but you are using "jewralyapp#mail.ru"!!!
Make sure to set "Less secure app access" to ON in your Gmail security setting.
In my case, the problem was solved.

Unable to send mail with gmail credentials from godaddy server

Am using asp.net and have written a smtp client to send mail out using gmail. It works fine if I use VS debugger or if I host it on my PC (localhost) while it doesn't work if hosted on godaddy server. I called their technical support to know if there could be any specific setting which I missed but they declined to share any info. Here is C# code.
smtp = new SmtpClient();
from = new MailAddress("xxxxxxx#gmail.com");
to = new MailAddress(ToAddr);
message = new MailMessage(from, to);
message.CC.Add(CCList);
message.Subject = Subject;
message.Body = body;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("xxxxxxx#gmail.com", "password");
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Timeout = (60 * 5 * 1000);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
Tried sending gmail using port 25 and 485 as suggested but it dint work out. Look forward for your support.
I was getting socket closed error even though I was using the appropriate port numbers with/without SSL. It dint work. The only work around I have right now is am using my domain email id to send out emails and it works perfectly as suggested. The link i found for the same: https://www.godaddy.com/help/send-email-using-systemnetmail-19291
Thank you for your help guys. Cheers.

.NET MVC contact form with dynamic/fillable "from" address

I'm having issues coding my contact form in order to properly send e-mails with a "From" address that the user fills in. I get 5.7.1 responses informing me that the address doesn't belong to my account.
I know wordpress makes this possible however I'm not sure what the fault is on my C# project. I'm not sure if the sending host is supposed to be an e-mail account created on my domain for this purpose or should the credentials be from the machine I'm hosting on?
The project is depoyed to azure websites.
MailMessage mail = new MailMessage(cvm.FromMail, "myaddress#domain.com", cvm.Subject, sb.ToString());
SmtpClient smtp = new SmtpClient();
smtp.Host = "mydomain.host.com";
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("myaddress#domain.com", "*********");
smtp.EnableSsl = true;
smtp.Send(Mail);
I've tried many different combinations of port/ssl settings but still can't find success doing this.
Or should the host/credentials be something from my source azure website machine instead of my domain credentials?

How to send a Secure e-mail using SMTP

I am currently using Google Apps to send SMTP e-mails. If my project deploys some of the information that i am going to be sending will be confidential and i would like to make sure the transmission is secure. Can anyone please let me know what i need to do to ensure that i send a safe e-mail using smtp through the google apps smtp server? smtp.google.com.
Any help greatly appreciated.
From what I have been told i need to force Https and have a SSL cert in order to do this. I don't know if this is true?
You Can Use 'smtp.EnableSsl = true' For Enable SSL for security.
MailMessage mail = new MailMessage();
mail.To.Add("" + to + "");
mail.From = new MailAddress("" + from + "");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"";
mail.Body = Body;
mail.IsBodyHtml = true;
Attachment at = new Attachment(Server.MapPath("~/ExcelFile/TestCSV.csv"));
mail.Attachments.Add(at);
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential(""+ username +"", ""+ password +"");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Send(mail);
To enforce Network Security you have to use SSL. to enforce security of the data going from your webserver to mail server you need to send your mail over SSL. and to secure the HTTP request that triggers the mail action you need to enforce SSL over HTTP.
But the question is Security in what context ? If you need network security to ensure a 3rd party cannot eavesdrop or manipulate then SSL is your way to go.

How do I send email from C# without revealing my password?

I found a great way to send email in C#, except for one problem, I have to show my password. If anyone were to disassemble my program, or if I decided to make it open source (which I probably will) they would get the username and password for my gmail account. Is there any way to get around this?
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("Contact#pandorafreed.com");
mail.To.Add("myemail#gmail.com");
mail.Subject = "Pandora Free-D Message";
mail.Body = "Name: " + C.NameBox.Text + "\n" + "Email: " + C.emailBox.Text + "\n" + C.Message.Text;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
+++++EDIT+++++
hmmm... would localhost work? Anyone know?
Not if your mail server requires authentication. The program will need to know the password to send mail, and if this program is operating on a computer you don't trust then you can assume the password will be quickly compromised.
You should consider an alternate method, like submitting the mail via a web service or HTTP call to a server under your control, where you can validate the message and then send it to the mail server.
You could create web-service, which will send e-mail to mail-server.
Why would you have others using your account to send e-mail? Let the user use his/her own mailserver and authentication. Let the user enter those values when installing your program and store it in a configuration file.

Categories