SmtpClient charset trouble - c#

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)

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

Mail is not being sent from unity 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

Cannot send Email :( - The remote certificate is invalid according to the validation procedure

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;

C# mails not being sent sometimes, but no error is thrown

This is my code :
public static void SendMail(string to, string subject, string body, string mailTitle)
{
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.From = new MailAddress("***", mailTitle);
mail.To.Add(to);
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient c = new SmtpClient("leavingstone.net", 25);
c.EnableSsl = false;
c.Credentials = new NetworkCredential("***", "***");
c.Send(mail);
}
Sometimes mails are sent, other times not - but no error is thrown.
Is it possible to fix the problem from code, or is it host fail?
Try my SMTP code, works perfect for me !
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.domain.com";
smtp.EnableSsl = Convert.ToBoolean("false");
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "no-reply#domain.com";
NetworkCred.Password = "**************";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = Convert.ToInt16(25);
smtp.Send(mm);

sending mail to multiple users through asp.net

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;
}
}

Categories