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.
Related
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
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.
I use code below to send email, but I get this error every time
Failure sending mail.
My code:
MailMessage message = new MailMessage("donot-reply#mydomain.ir", "reception#yahoo.com", "test", "msg");
message.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient("mail.mydomain.ir",110);
emailClient.Credentials = new System.Net.NetworkCredential("donot-reply#mydomain.ir", "donot-replyA!1");
emailClient.EnableSsl = true;
emailClient.Send(message);
I can send email by this email address in thunder birds, but I do not know why I can't send the email in .NET
At a quick glance, your using an invalid Simple Mail Transfer Protocol port, try the following:
Port: 25
Port: 587
Without a Stack Trace or more information on your error we won't be much use.
I want to send an email using c# SmtpClient via an smtp host that requires authentication or it will fail with relay denied. I have read many posts, but none quite solve my issue which is that while I specify NetworkCredentials, in the communication with the mailhost, no credentials are passed along. This confuses me.
This is the code that sets the credential and sends the mail:
using (SmtpClient smtp = new SmtpClient
{
Host = smtpserver,
Port = port,
EnableSsl = ssl
})
{
if (username.Length > 0)
{
Console.WriteLine("Setting credentials to\nusername: {0}\npassword: {1}", username, password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(username, password);
}
smtp.Send(message);
}
The correct credentials are printed to the console.
This is the TCP stream as captured by Wireshark:
220 mail.myhost.com Microsoft ESMTP MAIL Service ready at Tue, 20 Jan 2015 12:44:19 +0100
EHLO DK-XYZ-800SFF1
250-mail.myhost.com Hello [172.16.123.132]
250-SIZE 52428800
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-AUTH
250-8BITMIME
250-BINARYMIME
250 CHUNKING
MAIL FROM:<myemail#myhost.dk>
250 2.1.0 Sender OK
RCPT TO:<someemail#gmail.com>
550 5.7.1 Unable to relay
Clearly no authentication attempts are being made, despite the credentials having been set on SmtpClient.
I solved the issue and learned a bit in the process. While my code was correct, one of the parameters that I did not provide was the port information. From the SMTP log above, we can see the AUTH line is empty, i.e. has no protocols available for authentication. That is why SmtpClient didn't send any credentials.
The reason why AUTH line didn't have any methods for authentication was because I was using port 25 for communication. As soon as I switched to port 587, smtp reported that authentication was possible and credentials were sent and mails were sent as intended.
public static void CreateTestMessage2(string server)
{
string to = "jane#contoso.com";
string from = "ben#contoso.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString() );
}
}`
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.