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

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?

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.

Cant send email using Gmail SMTP

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.

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 to Send Email via Yandex SMTP (C# ASP.NET)

Formerly, I used my server as mail host and was sending emails via my own host. Now, I use Yandex as my mail server. I'm trying to send emails via Yandex SMTP. However, I could not achieve it. I get "the operation has timed out" message every time. I'm able to send & receive email with the same settings when I use Thunderbird. Hence, there is no issue with the account. I appreciate your guidance. You can see my code below:
EmailCredentials credentials = new EmailCredentials();
credentials.Domain = "domain.com";
credentials.SMTPUser = "email#domain.com";
credentials.SMTPPassword = "password";
int SmtpPort = 465;
string SmtpServer = "smtp.yandex.com";
System.Net.Mail.MailAddress sender = new System.Net.Mail.MailAddress(senderMail, senderName, System.Text.Encoding.UTF8);
System.Net.Mail.MailAddress recipient = new System.Net.Mail.MailAddress(recipientEmail, recipientName, System.Text.Encoding.UTF8);
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(sender, recipient);
email.BodyEncoding = System.Text.Encoding.UTF8;
email.SubjectEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(mailBody, #"<(.|\n)*?>", string.Empty), null, MediaTypeNames.Text.Plain);
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailBody, null, MediaTypeNames.Text.Html);
email.AlternateViews.Clear();
email.AlternateViews.Add(plainView);
email.AlternateViews.Add(htmlView);
email.Subject = mailTitle;
System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient();
SMTP.Host = SmtpServer;
SMTP.Port = SmtpPort;
SMTP.EnableSsl = true;
SMTP.Credentials = new System.Net.NetworkCredential(credentials.SMTPUser, credentials.SMTPPassword);
SMTP.Send(email);
After so many trials & errors, I have found how to make it work. I have made the following changes on the code posted in the question:
Set SmtpPort = 587
Added the following 2 lines of code:
SMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
SMTP.UseDefaultCredentials = false;
Additional note:
I use Azure server. I realized later that I did not configure the smtp endpoint for port 465. That being said, I had to add the 2 lines of code above in order to make email delivery work, just changing the port was not enough. My point is it is worth to check the defined ports on Azure and firewall before doing anything further.
I was able to make my code work by getting help from #Uwe and also #Dima-Babich, #Rail who posted on the following page Yandex smtp settings with ssl
. Hence, I think credits to answer this question should go to them.
Try using port 25 instead of 465 specified in Yandex help. I found this info on https://habrahabr.ru/post/237899/. They mentioned that it might be due to the fact that explicit SSL mode was implemented in the SmtpClient. Then port 25 is used for establishing connection in unencrypted mode and after that, protected mode is switched on.
I had the same problem.
I solved it by going to the Yandex mail, and then change some settings.
Go to:
1- Settings.
2- Email clients.
3- Set selected POP3 setting that is all.

Categories