C# smtp.google.com could not be resolved - c#

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.

Related

Send email from C# does not work

I am trying to send an email with C# code, copied from examples on MSDN (e.g. https://msdn.microsoft.com/en-us/library/14k9fb7t%28v=vs.110%29.aspx)
// from and password contain my credentials
// to contains a valid email address
public static void CodeExample()
{
try
{
using (MailMessage mail = new MailMessage(from, to))
{
using (SmtpClient server = new SmtpClient("smtp.googlemail.com"))
{
mail.From = new MailAddress(from);
mail.To.Add(new MailAddress(to));
mail.Subject = "Test subject";
mail.Body = "Test message";
mail.IsBodyHtml = false;
server.Port = 465;
server.Credentials = new System.Net.NetworkCredential(from, password);
server.UseDefaultCredentials = true;
server.EnableSsl = true;
server.ServicePoint.MaxIdleTime = 1;
server.Timeout = 60000;
Console.WriteLine("Sending to {0} by using SMTP host {1} port {2}.", to.ToString(), server.Host, server.Port);
server.Send(mail);
Console.WriteLine("mail Sent");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine("Inner Exception:");
Console.WriteLine(ex.InnerException?.ToString());
}
}
But I always get an exception:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException:
Unable to read data from the transport connection: net_io_connectionclosed.
The ‘from’ address details have been checked and seem OK. Sending from a Yahoo! account fails in the same way. I have tried lots of different combinations of SmtpClient properties. There are no messages in my firewall log.
Using Thunderbird, I can send from both the Googlemail and Yahoo! accounts without problems.
I would be grateful for any hints on how to get this to work.
Edit
I have seen this post SmtpException: Unable to read data from the transport connection: net_io_connectionclosed
Google mail fails on port 587 (both using and commenting-out UseDefaultCredentials = true and EnableSsl = true), reporting that I have an insecure app. I will try Yahoo! on port 587 later.
Thanks for the help. Using port 587 was important, as shown at SmtpException: Unable to read data from the transport connection: net_io_connectionclosed
I still cannot get smtp.googlemail.com or smtp.gmail.com to work, but that is covered at SmtpClient with Gmail.
My program is now working with smtp.mail.yahoo.com.

`The operation has timed out.` exception when sending mail using ZOHO SMTP configuration

I am using ZOHO mail server for sending mails through my application. But its unable to connect to server and throws exception The operation has timed out.. Following is my code:
public int sendMail(string from, string to, string subject, string messageBody) {
try {
SmtpClient client = new SmtpClient();
client.Port = 465;
client.Host = "smtp.zoho.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(Username, Password);
MailMessage mm = new MailMessage(from, to, subject, messageBody);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.IsBodyHtml = true;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
return 0;
} catch (Exception) {
throw;
}
}
I also tried using port 587 as suggested here Send email using smtp but operation timed out using ZOHO. But still problem persists.
Zoho SMTP Configuration help link: https://www.zoho.com/mail/help/zoho-smtp.html
Time out problems are usually related to network, ports problems, I haven't experience sending emails using SSL or TLS methods but I'd check this too, of course I suppouse you changed the port number when you say you tried TLS.
After trying all kinds of firewall/anti-virus/router port forwarding, port scanners, website port checkers I simply found out that with code almost identical to yours I was able to send mail successfully!
All you need to do is change smtp to:
smtp.zoho.eu
and port to:
587

Send mail works locally but not on server?

This code works locally, but when I upload it to my server on Godaddy, it does not send the e-mail. Any idea why it doesn't work on their server? What do I need to change?
try {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("Myemail#gmail.com");
mail.To.Add("Myemail#gmail.com");
mail.Subject = "New sign up";
mail.Body = "New member";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("Myemail#gmail.com", "**Mypass**");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
} catch(Exception ex) {
throw ex;
}
They may be blocking outgoing SMTP connections in order to prevent spammers from using their service to send spam. You should check what error messages you're getting and check your server host's policy.
There are a couple of things you need to do when sending from inside a site hosted from Godaddy. Use their relay server to send the message (this won't work from your dev machine, you'll have to test it live after you upload it). Here is the relay server info. Also make sure the "from" address is an email within the same domain. I usually use the same as the toAddress. See here for info on why this is necessary.
This is the code I'm using to send from a site inside Godaddy:
btnSend.Disabled = true;
const string serverHost = "relay-hosting.secureserver.net";
var msg = new MailMessage(toAddress, toAddress);
msg.ReplyTo = new MailAddress(emailFrom);
msg.Subject = subject;
msg.Body = emailBody;
msg.IsBodyHtml = false;
try
{
var smtp = new SmtpClient();
smtp.Host = serverHost;
smtp.Credentials = new System.Net.NetworkCredential("account", "password");
smtp.Send(msg);
}
catch (Exception e)
{
//Log the errors so that we can see them somewhere
}
You need to send your email via the godaddy smtp servers. I experienced the same issue with them before I think. I believe they give instructions of how to login via their FAQ.
If you have ssh access to the server, try to telnet smtp.google.com via 25 and 465 ports also. If you get a timeout, then you're likely firewalled from connecting to these ports outside a certain IP range.
Port 587 is for TLS. As you're using SSL, try port 465.

Mail Sending problem

I have used this code to send mails but I am not getting any error but I'm able to receive the mail. The default smtp server is also set to "127.0.0.1" as my local host in relay mail in the "inetmgr" but I'm still not able to receive the mail. I don't know where the problem is.
In emailsender.cs class this is the code:
public void SendEmail(string To, String Subject, String Body, String uname)
{
string body = "Hi " + uname + ",\n\n \t" + Body + "\n" + " \n Regards, \n LMS Team" + "\n\n\tSent at: " + DateTime.Now + " \n\n\t\t---- This is an auto generated mail. Please do not reply.";
try
{
try
{
MailMessage Message = new MailMessage();
Message.From = new MailAddress("karhik.varadarajan#asteor.com");
if (!string.IsNullOrEmpty(To))
Message.To.Add(new MailAddress(To));
Message.Subject = Subject;
Message.Body = body;
try
{
SmtpClient smtpClient = new SmtpClient("localhost");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(Message);
}
catch (System.Web.HttpException ehttp)
{
throw new Exception("Email Sending Failed", ehttp);
}
}
catch (IndexOutOfRangeException ex)
{
throw new IndexOutOfRangeException("Email Sending Failed", ex);
}
}
catch (System.Exception ex)
{
throw new Exception("Email Sending Failed", ex);
}
}
In the .aspx file:
protected void Page_Load(object sender, EventArgs e)
{
EmailSender email = new EmailSender();
email.SendEmail("karhik.varadarajan#asteor.com", "testingmail", "this is a test mail", "From");
}
If you use PickupDirectoryFromIis option, Check you C:\Inetpub\mailroot\Pickup or Queue or Badmail directory whether the EML file created or not. If it is in PickUp or Queue folder, IIS may process the file. If it is in BadMail, IIS unable to process the file.
I experienced the same issue,sometimes the organization wont allow access to send email.so i tried email relaying server. try elastic email.
If there are no error there are most likely an smpt server setup problem. Firstly, you are using localhost, not 127.0.0.1. I would recommend as a best practice to use 127.0.0.1 when calling localhost.
Even if it is a "shouldn't need too" there are no reason at all, using localhost. At least put "127.0.0.1 localhost" in windows etc\hosts file. You may also try a external SMTP host that you know ou have access to (like your isp). I know misconfigured smtp hosts CAN appear as the was sended succesfully.
However, as other already stated above, there can be a lot of other problems like access to send mail. Though, i think most errors like those will throw an error back to you.

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