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
Related
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
I'm trying to send an email using Outlook in our company with EnableSSL=True. However, I can't solve the issue I'm encountering at the moment with my sample code below:
public static bool RemoteServerCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return sslPolicyErrors == SslPolicyErrors.None;
}
public static void SendMail()
{
try
{
ServicePointManager.ServerCertificateValidationCallback = RemoteServerCertificateValidationCallback;
var mail = new MailMessage();
using (var smtp = new SmtpClient("mail.abccompany.com"))
{
mail.From = new MailAddress("john.smith#abccompany.com");
mail.To.Add("john.smith#abccompany.com");
mail.Subject = "Test";
mail.Body = "Test!";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
Console.WriteLine("Success");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
The value of SslPolicyErrors is "RemoteCertificateNameMismatch" and I don't have a clue how to fix it. I tried to export the certificate value from the "RemoteServerCertificateValidationCallback" method to a file and install it in my machine and still the issue is not fix. Any idea pls...
public static void SendEmail(StringBuilder sb) {
string mailbody = string.Empty;
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient("aaaaa.com");
smtpServer.Credentials = new System.Net.NetworkCredential("username", "pwd");
//smtpServer.Port = 587;
mail.IsBodyHtml = true;
smtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mail.BodyEncoding = System.Text.Encoding.UTF8;
smtpServer.UseDefaultCredentials = true;
smtpServer.EnableSsl = true;
mail.From = new MailAddress("bbbb.com");
mail.To.Add("ccccc.com");
mail.Subject = string.Format("test");
mailbody = sb.ToString();
mail.Body = mailbody;
smtpServer.Send(mail);
//everytime I get this error message and how to handle this, is this a security breach to bypass ssl in a company ? kindly advise the best way to handle this code and to send email.
}
Try the Unexpected “ The remote certificate is invalid according to the validation procedure.”
...tell StmpClient to ignore security with
client.EnableSsl = true;
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.
I want to modify this method to allow sending mail to multiple users instead of one by one .
I use the following method to send mail to bulk of users through putting the method in a loop according to the number of users but it takes several minutes making the user feeling that something wrong has been happened ..
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
if (to == "")
to = "----#gmail.com";
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = new MailAddress( "----#gmail");
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = new MailAddress("----#gmail");
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
client.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("----#gmail", "####");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}
Is there some way to refactor this method to allow sending the mail to multiple user .
public static string sendMail(string[] tolist, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
if (to == "")
to = "----#gmail.com";
MailAddressCollection m = new MailAddressCollection();
//Add this
foreach(string to in tolist)
{
m.Add(to);
}
//
mail.Subject = subject;
mail.From = new MailAddress( "----#gmail");
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = new MailAddress("----#gmail");
//And Add this
foreach(MailAddress ma in m)
{
mail.To.Add(ma);
}
//or maybe just mail.To=m;
smtp.Host = "smtp.gmail.com";
client.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("----#gmail", "####");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}