SmtpFailedRecipientException not thrown from SmtpClient.Send(MailMessage) - c#

I am trying to send an email to a fake email address and get a SmtpFailedRecipientException, however when I send the email to a fake address on my company domain, no exception is thrown. I am developing in VS2015 with .NET 4.5.2 and running the console application on a Windows 7 environment.
This code works correctly and sends emails when the email addresses are valid, and it will even throw a SmtpFailedRecipientException when I try sending to an invalid gmail or hotmail address. It seems that the only time an exception isn't thrown is when I try to send to a fake address within my company domain. Our company uses Office365 and I get an 'Undeliverable' message in my inbox when I try to send to the fake address from my address.
Does anyone have an idea why this is happening? I suspect it might be a setting on the mail server, but I dont have access to the mail server to check and see. I also might just be missing something obvious and be making a rookie mistake.
// Initialize SMTP client
SmtpClient client = null;
try
{
string from = "myEmailAddress#mycompany.com", to = "fakeEmailAddress#mycompany.com";
// Build client
client = new SmtpClient("mail.mycompany.com");
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Create message
using (MailMessage message = new MailMessage(from, to)
{
Subject = "Test email to fake address",
Body = "This email won't be sent"
})
{
// Send message
client.Send(message);
}
}
catch (SmtpFailedRecipientException ex)
{
Console.WriteLine($"SmtpFailedRecipientException caught.{Environment.NewLine}{ex.ToString()}");
}
catch (Exception ex)
{
Console.WriteLine($"Exception caught.{Environment.NewLine}{ex.ToString()}");
}

Related

How do I fix the error "The type initializer for '?' threw an exception." when sending e-mails through C# with EASendMail?

I have been working on this project to send an e-mail to a user and I have had an error come up all the time. (I'm using the EASendMail NuGet package) It is "The type initializer for '?' threw an exception." (this is in the console for the error system that the package has) I dont really know what that means, my code is this:
using EASendMail;
namespace Email
{
class Program
{
static void Main(string[] args)
{
try
{
SmtpMail oMail = new SmtpMail("TryIt");
// Set sender email address, please change it to yours
oMail.From = "Splat2ooner#gmail.com";
// Set recipient email address, please change it to yours
oMail.To = "Splat2ooner#gmail.com";
// Set email subject
oMail.Subject = "test email from c# project";
// Set email body
oMail.TextBody = "this is a test email sent from c# project, do not reply";
// SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// User and password for ESMTP authentication
oServer.User = "Splat2ooner#gmail.com";
oServer.Password = "password (not my passoword)";
// Most mordern SMTP servers require SSL/TLS connection now.
// ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
oServer.ConnectType = SmtpConnectType.ConnectTryTLS;
// If your SMTP server uses 587 port
//oServer.Port = 587;
// If your SMTP server requires SSL/TLS connection on 25/587/465 port
//oServer.Port = 25; // 25 or 587 or 465
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
Console.WriteLine("start to send email ...");
SmtpClient oSmtp = new SmtpClient();
oSmtp.SendMail(oServer, oMail);
Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
}
}
}
Thanks.
It looks like newer version of "EASendMail" (as of now the latest version is 7.5.0.3) package has this issue.
I don't have much time to do research on it, just installed older version (7.3.1.2) and it resolved this.
hope it helps.

C# smtp.google.com could not be resolved

Following code used to work but suddenly refuses to work.
private static void SendMail()
{
try
{
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.google.com", 587);
mail.From = new MailAddress("catthoor.jc#gmail.com", "Jasper.Kattoor");
mail.To.Add("YYYY");
mail.Subject = "sup";
mail.Body = "sup";
smtpServer.Credentials = new NetworkCredential("catthoor.jc#gmail.com", "XXXX");
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
I receive the following error:
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: The remote name could not be resolved:
'smtp.google.com'
I've also tried using hotmail instead of gmail, same error.
I can still send mails manually though.
Why would this error suddenly occur? Yesterday there were no problems with this.
That remote host name is wrong, it should be:
smtp.gmail.com
Read all about it: Send Email from Yahoo!, GMail, Hotmail (C#)
Updates: You can also ping the host name to check if it exists using command prompt
Yes, in my case I wasn't just connected to the internet.
After I connected the problem was gone.

How to send SMS to mobile using SMTP server in windows application?

I am developing a windows application using C#, in which i want to send SMS to some user based on some condition. i goes through the many forum post to "Send SMS using SMTP Server" but none of them use-full for me. In this i got some clue to send SMS through Gmail SMTP but not working as i think it is carrier specific (not sure).
My code sample :
try
{
MailMessage message = new MailMessage();
message.To.Add("1568235685#sms.sancharnet.in");
message.From = new MailAddress("sameone#gmail.com"); //See the note afterwards...
message.Body = "Hi, How r you ?";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("someonet#gmail.com", "password");
smtp.Send(message);
MessageBox.Show("Message sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
above code not giving any exception or error but also i am not getting any sms on my number as well.
So, what i want to ask that is there any way to send SMS using SMTP server to the mobile number of any carrier?
You have to send to the SMS gateway. It is provider specific.
Wikipedia has a List of SMS Gateways.
For example, to send to a Sprint PCS number you would send to number#messaging.sprintpcs.com, where number is the phone number (i.e. 5551234567, or whatever).
For those who have looked so much for a free way to send SMS from a web app, and are in France, and having FreeMobile as operator, I've just found a way in calling a free web service provided by FreeMobile. I've written this code in C# and it works fine.
private void SendSMSAlert(String message)
{
try
{
String url = "https://smsapi.free-mobile.fr/sendmsg?user="YourFreeMobileIdentifierHere"&pass="YOURPASSHERE"&msg=" + message;
var request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
}
catch(WebException e)
{
System.Diagnostics.Trace.WriteLine("SMS Not Sent! Exception "+e.ToString());
}
}
So if you have a freeMobile line in France, you can get your Pass from https://mobile.free.fr/moncompte/
Then, if you need to forward the SMS to other numbers, it can be done with many mobile apps on AppStore or GooglePlay.
I hope this helps!

"SmtpFailedRecipientException: Mailbox unavailable" when mailbox is available

I get this error when I try to send an e-mail to a specific address in my code:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: Unknown user
The code sends an e-mail to two email addresses, mine and my colleague's. The e-mail sends to me just fine, but I get that error when it tries to send the email to him.
I looked around, and basically the common explanation for this error is that the email address is invalid, or their mailbox is full and isn't allowed to receive mail, or there is some setting on the server that is restricting it from receiving an e-mail.
But the email address is able to receive email, I'm corresponding back and forth through e-mail with him right now.
Is there any other reason why this error might occur?
EDIT:
Here's the code, maybe someone can spot an issue. I checked the parameters being passed, all the data is correct:
private static void SendEmail(IEnumerable<MailAddress> to, MailAddress from,
string subject, string body, string bodyHtml)
{
var mail = new MailMessage { From = from, Subject = subject };
foreach (var address in to)
{
mail.To.Add(address);
}
mail.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(bodyHtml, null, "text/html"));
mail.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(body, null, "text/plain"));
try
{
var smtp = new SmtpClient("localhost", 25)
{
Credentials = new NetworkCredential("xxx", "xxx")
};
smtp.Send(mail);
}
catch (Exception err)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(err);
}
}
Assuming your SMTP settings are correct this is most probably a case of a server-side restriction...
For example to prevent spam the server only accepts smtp from static sender IP and/or is checking sender IP against MX records (DNS) etc.

How to send the mail from c#

I have code,
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(oMailMessage);
(all variables have values)
I have installed SMTP virtual services. why it is unable to send emails. why it is not working ??
EDIT
public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody)
{
try
{
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "SERVERNAME";
SmtpMail.Send(oMailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
I have this code. It is executing fine and is returning true, but I'm not getting any email in the inbox.
What else could be wrong?
Getting some mails in BadMail Dir at C:\Inetpub\mailroot\Badmail also in Queue Directory getting some mails here ... what does that means..??
I found that mail only can sent to gmail accounts... why it is?
As mentioned by others, your code is fine and is most likely something in your SMTP configuration or maybe your email client your sending your test emails to is marking them as spam. If it's spam, well that's easy enoughto figure out.
If it's something with the email, you can go to your mailroot folder and their will be some folders there with the email files along with a description. See if there's anything in the BadMail folder or the queue folder and open them up in notepad and view what error is given for why they weren't sent.
Determine what the error is:
try
{
SmtpMail.Send(oMailMessage);
}
catch (Exception ex)
{
//breakpoint here to determine what the error is:
Console.WriteLine(ex.Message);
}
From here, please edit your question with that exception details.
Its hard to tell, but one possibility is that you haven't enabled anonymous access on the SMTP virtual server. Go to the the virtual server properties dialog, select the Access tab, click the Access Control button, and make sure that Anonymous Access is enabled.
There doesn't appear to be anything functionally wrong with your program. It's likely a configuration issue between your program and the mail server. I would try the following to diagnose the problem.
Wrap the code in a try/catch block and see if the exception message contains useful data
Use 127.0.0.1 instead of localhost just to rule out anything crazy
Ensure your SMTP server is running on the standard port (25 I believe)
Hello you can follow the following code:
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your gmail id", "password");
MailMessage msg = new MailMessage();
msg.To.Add(textBoxTo.Text);
msg.From = new MailAddress("your gmail id");
msg.Subject = textBoxSubject.Text;
msg.Body = textBoxMsg.Text;
Attachment data = new Attachment(textBoxAttachment.Text);
msg.Attachments.Add(data);
client.Send(msg);
MessageBox.Show("Successfully Sent Message.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
have you tried 127.0.0.1 instead of Localhost?
Also have you tested that the SMTP service is working, check out this link for details.
In the virtual smtp server Add relay restrictions and connection control so that none of the outside connections are allowed

Categories

Resources