Smtp mail delivered unsuccessfully - c#

I am using Smtp to send mail.A message was sent successfully but it was not delivered. What is the reason behind this.Is this a problem in mailing server?The message sending process is working fine for the last couple of years.The issue came first time.
public bool SendMail(string p_strFrom, string p_strDisplayName, string p_strTo, string p_strSubject, string p_strMessage , string strFileName)
{
try
{
p_strDisplayName = _DisplayName;
string smtpserver = _SmtpServer;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(_From,_DisplayName);
smtpClient.Host = _SmtpServer;
smtpClient.Port = Convert.ToInt32(_Port);
string strAuth_UserName = _UserName;
string strAuth_Password = _Password;
if (strAuth_UserName != null)
{
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(strAuth_UserName, strAuth_Password);
smtpClient.UseDefaultCredentials = false;
if (_SSL)
{
smtpClient.EnableSsl = true;
}
smtpClient.Credentials = SMTPUserInfo;
}
message.From = fromAddress;
message.Subject = p_strSubject;
message.IsBodyHtml = true;
message.Body = p_strMessage;
message.To.Add(p_strTo);
try
{
smtpClient.Send(message);
Log.WriteSpecialLog("smtpClient mail sending first try success", "");
}
catch (Exception ee)
{
Log.WriteSpecialLog("smtpClient mail sending first try Failed : " + ee.ToString(), "");
return false;
}
return true;
}
catch (Exception ex)
{
Log.WriteLog("smtpClient mail sending overall failed : " + ex.ToString());
return false;
}
}

A message was sent successfully but it was not delivered
If it was sent successfully from your mailing server then the possible reason of non delivery can be that the mail filter on client blocked it or is delivered in the junk.

Related

Gmail mail cannot be sent through C#

I have created a Windows application which is used to send emails. i have given credentials. i turned on google/settings/lesssecure apps. Eventhough its not sending. Its showing the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required here is my code.
MailMessage message = new MailMessage();
message.From = new MailAddress("Selvacsebe23#gmail.com");
string[] mailaddress = new string[count];
int i;
if (textSubject.Text != string.Empty)
{
message.Subject = textSubject.Text;
if (textBody.Text != string.Empty)
{
message.To="Selvakesavan#gmail.com"
message.IsBodyHtml = true;
string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds";
message.Body = tmpBody;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("selvacsebe23#gmail.com", "mypassword");
message.Priority = MailPriority.High;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Mail has sent successfully !!!", "Success !");
}
else
{
MessageBox.Show("Please Enter Body of the Message !");
}
}
else
{
MessageBox.Show("Please Enter Subject !");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failure !");
log.Fatal(ex.Message);
}
}
If you turn on 2-step-verification, then you need to login using app-specific-password. You can create it here:
https://support.google.com/accounts/answer/185833?hl=en. If you use your normal password, then you will get exception : 5.5.1 Authentication Required. You don't need lots of code, this code is enough to send email without attachment:
const string from = "user1#gmail.com";
const string to = "user2#yahoo.com";
const string subject = "This is subject";
const string body = "This is body";
const string appSpecificPassword = "akdfkajsdhklakdfh";
var mailMessage = new MailMessage(from, to, subject, body);
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword);
smtpClient.Send(mailMessage);
}

How to create an SMTP mail?

I tried to send mail from my asp.net web application.
I want to send my application password to the user mail id, for that I took the password from datatabase using getdetailss function.
btn4getPwd is the button calling the mailing function.
txtusername.Text is the text box containing sending mail address. All values are receiving correctly, no error occur but it's not working ..!
protected void btn4getPwd_Click(object sender, EventArgs e)
{
if (txtusername.Text.Trim() != "")
{
em.username = txtusername.Text.Trim();
DataTable forget = em.getdetailss(10);
string passwd = (forget.Rows[0]["PassCode"].ToString());
try
{
string Subject = "Your NLS Password";
string Body = passwd;
string ToEmail = txtusername.Text.Trim();
string SMTPUser = "mymail#gmail.com", SMTPPassword = "pswd";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(SMTPUser, "AspnetO");
mail.To.Add(ToEmail);
mail.Body = Body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);
smtp.EnableSsl = true;
smtp.Send(mail);
Response.Write("<script language=javascript>alert('Mail send...!! ')</script>");
}
catch (SmtpException ex)
{
lbl4get.Text = "SmtpException ";
}
catch (Exception ex)
{
lbl4get.Text = "Exception";
}
}
else { Response.Write("<script language=javascript>alert('Invalid USERNAME...!! ')</script>"); }
}
smtp.Port = 25; is default port but for as you are sending over SSL use port 587 or 465 (nonstandard, but sometimes used for legacy reasons). Assume NetworkCredential are correct.

Exception sending email using Asp.net

I've been tring to make a webpage use Gmail as a smtp Server to send emails with Asp.net. Here is the link for the settings that google has set for such occasion.
https://support.google.com/a/answer/176600?hl=en
Every time I try to send an email I get the "Exception caught in RetryIfBusy()" message..
Any ideas?...Here is my code below:
[System.Web.Services.WebMethod]
public static void SendMessage(string toEmail)
{
MailMessage msg = new MailMessage("myEmail#gmail.com", toEmail);
msg.Subject = "Email Subject";
msg.Body = "Here goes email body";
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
System.Net.NetworkCredential netCred = new System.Net.NetworkCredential();
netCred.UserName = "myEmail#gmail.com";
netCred.Password = "myPassword";
smtp.Credentials = netCred;
smtp.Port = 465;
smtp.EnableSsl = true;
try
{
smtp.Send(msg);
Console.WriteLine("Email Successfully sent!!");
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
Console.WriteLine("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
smtp.Send(msg);
}
else
{
Console.WriteLine("Failed to deliver message to " +
ex.InnerExceptions[i].FailedRecipient);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in RetryIfBusy(): " +
ex.ToString());
}
}
You are using a wrong port on Google's SMTP server. Should be:
smtp.Port = 587;

Loop for resending smtp email on failure to send

I have a service that sends an email after a user registers. Every once in a while, a user contacts support with the complaint that they aren't receiving the email, so I've made a list of possible issues, one of which is a smtp failure to send email, which I noticed occasionally when I would step through the code. I want to write a simple loop that tries to resend the email a couple of times on failure to send, but I'm not sure how to go about doing that. I'd appreciate any advice on the subject.
public void MusicDownloadEmail(string email)
{
try
{
var smtp = new SmtpClient();
var mail = new MailMessage();
const string mailBody = "Body text";
mail.To.Add(email);
mail.Subject = "Mail subject";
mail.Body = mailBody;
mail.IsBodyHtml = true;
smtp.Send(mail);
}
catch (Exception ex)
{
var exception = ex.Message.ToString();
//Other code for saving exception message to a log.
}
}
Something like this should do the trick:
public void MusicDownloadEmail(string email)
{
int tryAgain = 10;
bool failed = false;
do
{
try
{
failed = false;
var smtp = new SmtpClient();
var mail = new MailMessage();
const string mailBody = "Body text";
mail.To.Add(email);
mail.Subject = "Mail subject";
mail.Body = mailBody;
mail.IsBodyHtml = true;
smtp.Send(mail);
}
catch (Exception ex) // I would avoid catching all exceptions equally, but ymmv
{
failed = true;
tryAgain--;
var exception = ex.Message.ToString();
//Other code for saving exception message to a log.
}
}while(failed && tryAgain !=0)
}
You could do it recusively
Firstly define the maximum amount of retries
public const int MAX_RETRY_COUNT = 3;
Then call the method using the retry count
MusicDownloadEmail("code#mail.com", MAX_RETRY_COUNT);
And modify the method as follows
public static void MusicDownloadEmail(string email, int retryCountsLeft) {
if (retryCountsLeft > 1) {
try {
var smtp = new SmtpClient();
var mail = new MailMessage();
const string mailBody = "Body text";
mail.To.Add(email);
mail.Subject = "Mail subject";
mail.Body = mailBody;
mail.IsBodyHtml = true;
smtp.Send(mail);
} catch (Exception ex) {
var exception = ex.Message.ToString();
//Other code for saving exception message to a log.
MusicDownloadEmail(email, --retryCountsLeft);
}
}
}

SmtpException While sending mails in a bulk of 3 or more

I am using SmtpClient to send emails.I am using the same code for last 2years,but from last day when i send 3 or more emails together one of them will fail.When i sent the failed one again it will sent out.Please help me i am using aibn.com mail server.
public bool SendMail(string p_strFrom, string p_strDisplayName, string p_strTo, string p_strSubject, string p_strMessage , string strFileName)
{
try
{
p_strDisplayName = _DisplayName;
string smtpserver = _SmtpServer;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(_From,_DisplayName);
smtpClient.Host = _SmtpServer;
smtpClient.Port = Convert.ToInt32(_Port);
string strAuth_UserName = _UserName;
string strAuth_Password = _Password;
if (strAuth_UserName != null)
{
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(strAuth_UserName, strAuth_Password);
smtpClient.UseDefaultCredentials = false;
if (_SSL)
{
smtpClient.EnableSsl = true;
}
smtpClient.Credentials = SMTPUserInfo;
}
message.From = fromAddress;
message.Subject = p_strSubject;
message.IsBodyHtml = true;
message.Body = p_strMessage;
message.To.Add(p_strTo);
try
{
smtpClient.Send(message);
Log.WriteSpecialLog("smtpClient mail sending first try success", "");
}
catch (Exception ee)
{
Log.WriteSpecialLog("smtpClient mail sending first try Failed : " + ee.ToString(), "");
return false;
}
return true;
}
catch (Exception ex)
{
Log.WriteLog("smtpClient mail sending overall failed : " + ex.ToString());
return false;
}
}
Got the following error message
smtpClient mail sending Failed :
System.Net.Mail.SmtpException:
Failure sending mail.
System.NullReferenceException:
Object reference not set to an instance of an object.
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of Inner Exception Stack Trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
I have changed my mail server and now it is working fine.May be it depends by some mail service restriction to prevent flooding and/or spam.

Categories

Resources