Gmail domain not receiving email's MVC .net - c#

I have hosted the application in GoDaddy server and am using smtp to send a maill. While sent a maill for Gmail domain. The maill was not received and i have checked to spam folder aloso not get it the maill.
` public static bool SendEmailTemplate1(string emailId, string ccEmailid, string Msg, string sub, string fromId, string fromPass)
{
try
{
string mailServer = ConfigurationManager.AppSettings["MailServer"].ToString();
MailMessage msg = new MailMessage();
msg.To.Add(emailId);
if (ccEmailid != null)
{
msg.CC.Add(ccEmailid);
}
msg.From = new MailAddress(fromId, "Dinesh");
msg.Subject = sub;
msg.IsBodyHtml = true;
msg.Body = Msg;
System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(fromId, fromPass);
System.Net.Mail.SmtpClient mailClient = new
System.Net.Mail.SmtpClient(mailServer, Convert.ToInt32(MailPort));
mailClient.EnableSsl = Convert.ToBoolean(MailSSL);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
try
{
mailClient.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
Common.AjaxErrorLog("EMAIL", "SENDEMAIL", ex);
throw ex;
}
return true;
}
catch (Exception ex)
{
Common.AjaxErrorLog("EMAIL", "SENDEMAIL", ex);
throw new Exception("EMAILERROR: " + ex.Message.ToString());
}
}`
There's is no error show while send a maill for gmail domain.I need to know the reson.

Related

SMTP Email and ERR_CONNECTION_RESET Error in Angular Project

Hi when i use the below code in angular api project my requests are not getting saved and i am gettign this following error. And when i comment the code to send mail everything is working fine
ERROR in console
polyfills POST net::ERR_CONNECTION_RESET
And code to send mail
public async Task SendEmail(string email, string subject, string firstName, string contact)
{
try
{
string message = BuildMessageBody(firstName, email, contact);
using (var client = new SmtpClient())
{
var networkCredential = new NetworkCredential
{
UserName = _configuration["Email:Email"],
Password = _configuration["Email:Password"]
};
client.UseDefaultCredentials = false;
client.Credentials = networkCredential;
client.Host = _configuration["Email:Host"];
client.Port = int.Parse(_configuration["Email:Port"]);
client.EnableSsl = true;
using (var emailMessage = new MailMessage())
{
emailMessage.To.Add(new MailAddress(email));
emailMessage.CC.Add(new MailAddress("my cc adddress"));
emailMessage.From = new MailAddress(_configuration["Email:Email"]);
emailMessage.Subject = subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = true;
emailMessage.BodyEncoding = System.Text.Encoding.UTF8;
emailMessage.SubjectEncoding = System.Text.Encoding.Default;
emailMessage.ReplyToList.Add(new MailAddress(_configuration["Email:Email"]));
client.SendCompleted += (s, e) => {
client.Dispose();
emailMessage.Dispose();
};
await client.SendMailAsync(emailMessage);
}
}
await Task.CompletedTask;
}
catch (Exception ex)
{
_appLogger.CreateLog("SendEmail - " + ex.Message);
}
}
Any idea why this behaviour???

RemoteCertificateNameMismatch when sending SMTP email

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...

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.

Cannot send email when deploying

When working locally it works fine, but when publishing the site, the mail function doesn't work. I suspect that the issue might lie with SmtpClient, but I am not sure. Does anyone know if there is a difference in how the network acts when running over the network vs locally?
My code:
public ActionResult SendMail(EmailModel model)
{
string host = ConfigurationManager.AppSettings.Get("SMTPHOST");
int port = Convert.ToInt16(ConfigurationManager.AppSettings.Get("SMTPPORT"));
string receiver = ConfigurationManager.AppSettings.Get("TOADDRESS");
string password = ConfigurationManager.AppSettings.Get("PASSWORD");
SmtpClient smtp = new SmtpClient(host, port);
smtp.Credentials = new NetworkCredential(receiver, password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
MailMessage mailMsg = null;
var status = new HttpStatusCode();
try
{
if (ModelState.IsValid)
{
mailMsg = new MailMessage(model.from, receiver, model.subject, model.body);
smtp.Send(mailMsg);
return PartialView("_About");
}
else
{
status = HttpStatusCode.BadRequest;
}
}
catch (Exception ex)
{
status = HttpStatusCode.BadRequest;
SendInfoMail("address", "address", "Exception", ex.ToString());
}
return new HttpStatusCodeResult(status);
}
I think the code throws an exception, but since this only happens when the site is published, I can't debug it.

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