Mail is not being sent from unity C# - c#

I am trying to send mail through unity C#. I am using this code:
void SendEmail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(to);
mail.Subject = "Password Verification";
mail.Body ="Password Number";
SmtpClient smtpServer = new SmtpClient(smtp);
smtpServer.Port = 587;
smtpServer.EnableSsl = true;
smtpServer.Credentials = new System.Net.NetworkCredential(from, password) as ICredentialsByHost;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object obj, X509Certificate cert, X509Chain chain, SslPolicyErrors sslerrors)
{ return true; };
smtpServer.Send(mail);
#if UNITY_EDITOR
Debug.Log("email sent");
#endif
SetEmailing(false);
}
But mail is not begin sent to the Recipient. I have also enabled the "Allow secure less apps" in my Gmail account.
But mail is not being sent....
Any help is appreciated

Related

Email Not Sent, Error 530-5.5.1 Authentication Required

Clear Screenshot of the code
Here is the code that sends the email, I really dont know why it doesnt send when I'm using it as a mobile app
receiverEmail = inputEmail.text;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(senderEmail);
mail.To.Add(receiverEmail);
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpServer = new SmtpClient(server);
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential(senderEmail, senderPassword) as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){
return true;
};
try
{
smtpServer.Send(mail);
emailStatus.GetComponentInChildren<Text>().text = "Email Sent !!";
}
catch (SmtpException error)
{
Debug.Log (error.StatusCode);
Debug.Log (error.Message);
emailStatus.GetComponentInChildren<Text>().text = "Email Not Sent \n" +error.Message+ "\n Error Number : "+ error.StatusCode;
}
Thanks to #bugfinder, it really was the .net, I changed mine to 2.0 from 2.0 subset and its working fine

Failure sending mail using asp.net. Connection host has failed to respond

I want to send a simple test mail from my c# application
I am using the following code
MailMessage mail = new MailMessage("from", "to");
mail.Subject = "TestEmailImportant";
mail.Body = "This mail is to test if this program is working";
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "xxxxxxxxx#gmail.com",
Password = "xxxxxx"
};
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
Label1.Text = "Email Sent";
This is being triggered on the click of a button.
Whenever I click the button, the browser keeps "waiting for the host" and after a minute or so I get this error
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond "some IP".
Pointing towards smtpClient.Send(mail); line.
sign up Gmail and go to https://www.google.com/settings/security/lesssecureapps where you can see settings.
Access for less secure apps
Turn off (default) ==> Turn On
after add this code,
MailMessage mail = new MailMessage("fromm#gmail.com", "toaddress#gmail.com");
mail.Subject = "TestEmailImportant";
mail.Body = "This mail is to test if this program is working";
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "XXXXXX#gmail.com",
Password = "YYYYYYYY"
};
smtpClient.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
smtpClient.Send(mail);

SmtpClient charset trouble

I have code that send message to purpose email. When i run program from Windows - charset work correctly. But when i run this app in Linux VDS (Centos) charset incorrect - example ����������� ���. Message on cyrillic. I try make charset utf-8, but it also not work
SmtpClient smtp = new SmtpClient(Smtp, Convert.ToInt32(port));
smtp.Credentials = new NetworkCredential(login, pass);
smtp.EnableSsl = true;
MailMessage message = new MailMessage();
message.From = new MailAddress(from);
message.To.Add(new MailAddress(to));
message.IsBodyHtml = true;
message.Subject = subject;
message.Body = string.Format(msg);
message.BodyEncoding = Encoding.UTF8;
Console.WriteLine(msg);
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtp.Send(message)

smtp.live Could not send the e-mail - error: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated

I saw all the other pages on stackoverflow that were about this problem and tried them but none of them worked.
im doing a website as a project for school, and I want the users to send an e-mail for them to report problems in, but it always gives me that error.
this is my code:
protected void Sendbtn_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage = new MailMessage("user#hotmail.com","my#hotmail.com");
mailMessage.Subject = "Problem from Gamer's Utopia";
mailMessage.Body = this.msgtxt.Text;
SmtpClient smtpClient = new SmtpClient(" smtp.live.com");
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
}
I tried using authentication with username and password but it didnt work - unless I did it incorrectly.
add authenticated NetworkCredential
System.Net.NetworkCredential smtpUser = new System.Net.NetworkCredential("admin#hotmail.com", "password");
smtpClient.Credentials = smtpUser;
You need to set SmtpClient Credentials, for example
smtpClient.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
check below answer for sample code:
https://stackoverflow.com/a/9851590/2558060
Change your code according to this!!! It will perfectly work!!!
using (MailMessage mail = new MailMessage())
{
SmtpClient client = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("from address");
mail.Subject = "";
string message = "";
mail.Body = message;
try
{
mail.To.Add(new MailAddress("To Address"));
}
catch
{
}
client.Credentials = new System.Net.NetworkCredential("smtp.live.com", "password");
client.EnableSsl = true;
try
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.Send(mail);
mail.To.Clear();
}
catch (Exception ex)
{
}
}
From your sender account, view the email inbox and say it is you to allow the third party to send emails.

How to add smtp hotmail account to send mail

I wrote some codes so as to send e mail but I can only send mail from gmail account to gmail account also, I want to use hotmail accounts how can i do it? thanks
It is
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("xxx#gmail.com");
mail.To.Add("kalaylevent#gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("kalaylevent#gmail.com", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
I changed a little of code and it tested successfully (from Hotmail to Gmail)
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("youremail#hotmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
I use different smtp client and make sure to set the socket options to StartTls :
using (SmtpClient client = new())
{
try
{
await client.ConnectAsync("smtp.office365.com", 587, SecureSocketOptions.StartTls);
client.AuthenticationMechanisms.Remove("XOAUTH2");
await client.AuthenticateAsync("Your_User_Name", "Your_Password");
await client.SendAsync(mailMessage);
}
catch
{
//log an error message or throw an exception, or both.
throw;
}
finally
{
await client.DisconnectAsync(true);
client.Dispose();
}
}

Categories