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.
Related
MailMessage message = new MailMessage();
message.Subject = "test";
message.Body = "test";
message.To.Add("test#gmail.com");
message.From = new MailAddress("bob#internalhost.com");
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "172.22.0.20";
smtp.Port = 25;
smtp.Send(message);
Any idea why I might be getting an error
The remote name could not be resolved.
Clearly no resolution is required as I have specified an IP address. I can ping the IP and even telnet on port 25 and successfully send an e-mail. But, I can't send an e-mail.
I ran a wireshark trace and it doesn't look like any traffic is being send to 172.22.0.20
I use this code and its working fine on my end
Make sure that you are using the correct IP Address and port number. I think your Port Number is not correct.
(open your email id and check mail configuration)
MailMessage mail = new MailMessage("bob#internalhost.com", "Toemai#email.com");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";
SmtpClient client = new SmtpClient("172.22.0.20");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("bob#internalhost.com", "password");
client.Port = 25;
client.Send(mail);
I tested your code with :
static void Main(string[] args)
{
try
{
MailMessage message = new MailMessage();
message.Subject = "test";
message.Body = "test";
message.To.Add("atmane.elbouachri#gmail.com");
message.From = new MailAddress("atmane.elbouachri#softeam.fr");
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "127.0.0.1";
smtp.Port = 25;
smtp.Send(message);
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
And it seems good. I used smtp4dev Tools and it truks this :
220 localhost smtp4dev ready
EHLO Aepro
250-Nice to meet you.
250-8BITMIME
250-STARTTLS
250-AUTH=CRAM-MD5 PLAIN LOGIN ANONYMOUS
250-AUTH CRAM-MD5 PLAIN LOGIN ANONYMOUS
250 SIZE
MAIL FROM:<atmane.elbouachri#softeam.fr>
250 Okey dokey
RCPT TO:<atmane.elbouachri#gmail.com>
250 Recipient accepted
RSET
250 Rset completed
RSET
250 Rset completed
MAIL FROM:<atmane.elbouachri#softeam.fr>
250 Okey dokey
RCPT TO:<atmane.elbouachri#gmail.com>
250 Recipient accepted
DATA
354 End message with period
MIME-Version: 1.0
But when i put a fake IP I have an exception (witch is normal).
I think your IP is bad or you must have a problem with a firewall
Below code is working fine at my end.....
MailMessage mailMessage = new MailMessage("abc#xyz.in", "receiver#xyz.in");
mailMessage.Priority = System.Net.Mail.MailPriority.High;
mailMessage.Body = "message";
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("host/IP", 25);
NetworkCredential credentials = new NetworkCredential("abc#xyz.in", "password");
smtpClient.Credentials = credentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = credentials;
smtpClient.Send(mailMessage);
If your code is running in a service application pool - you need to set the Identity under Advanced Settings in the Process Model as "Network Service" rather than the default "ApplicationPoolIdentity". If you do that, it'll work every time. If you don't, some emails work, some do not. It is a weird permission thing.
A similar issue popped up over here: System.Net.WebException: The remote name could not be resolved:
As you can see, a simple polling mechanism wrapped around a try-catch statement can gives you an opportunity try your block of method multiple times (which fails sporadically) before you report it to the user.
The problem I see is this line:
smtp.Host = 172.22.0.20;
smtp.Host expects string. So, it should be smtp.Host = "172.22.0.20"; Besides that, please add the code below so we can see error
try
{
smtp.Send(mail);
}
catch (Exception e)
{
Debug.WriteLine("Exception Message: " + e.Message);
}
Your IP address is not correct or you might be working under a firewall. Also check you DNS settings.
Maybe the reverse name is mandatory, have you tried to register a name for this IP in the host file ? (Or in your DNS of course)
Try to add:
172.22.0.20 my-smtp.example
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
I want to send mail using SmtpClient class, but it not work.
Code:
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(obMailSetting.UserName, obMailSetting.Password);
smtpClient.Host = obMailSetting.HostMail;
smtpClient.Port = obMailSetting.Port;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = obMailSetting.Connect_Security;//true
//smtpClient.UseDefaultCredentials = true;//It would work if i uncomment this line
smtpClient.Send(email);
It throws an exception:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
I'm sure that username and password is correct. Is there any problem in my code?
Thanks.
You can try this :
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre =
new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
// Handle exception here
}
You can try this out :
In the Exchange Management Console, under the Server Configuration node, select Hub Transport and the name of the server. In the Receive Connectors pane, open either of the Recive Connectors (my default installation created 2) or you can create a new one just for TFS (I also tried this and it worked). For any of these connectors, open Properties and on the Permission Groups tab ensure that Anonymous Users is selected (it's not by default).
Or
You can also try this by initializing SmtpClient as follows:
SmtpClient smtp = new SmtpClient("127.0.0.1");
The server responds with 5.7.1 Client was not authenticated but only if you do not set UseDefaultCredentials to true. This indicates that the NetworkCredential that you are using is in fact wrong.
Either the user name or password is wrong or perhaps you need to specify a domain? You can use another constructor to specify the domain:
new NetworkCredential("MyUserName", "MyPassword", "MyDomain");
Or perhaps the user that you specify does not have the necessary rights to send mail on the SMTP server but then I would expect another server response.
I'm just trying to get my hmailserver to send mail from my C# program. The part that's killing me is the SSL part.
I originally got this error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.
So I added: smtp.EnableSsl = true; and now I get Server does not support secure connections.
Here is my code, this is driving me nuts. Do I have to create my own SSL or is there a way to disable SSL on hmailserver side?
MailMessage mail = new MailMessage("jlnt#ademo.net", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
smtp.Port = 25;
NetworkCredential login = new NetworkCredential("ja#test.net", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Ahh okay what you have to do is in HMailServer go to advanced- ip ranges. Create a new IP range for example if you 192.168.1.2, you have to make the range 192.168.1.1-192.168.1.3, then at bottom uncheck all the required smtp authentication boxes.
Annoying...
To enable secure connection to send email throught your email provider, you have to change the port number.
MailMessage mail = new MailMessage("jlnt#ademo.net", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
//smtp.Port =25;
smtp.Port =587;
NetworkCredential login = new NetworkCredential("ja#test.net", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
i was having this issue, what i did was used localhost ip and EnableSsl to false
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "127.0.0.1";
smtpClient.Credentials = new NetworkCredential("test#123test.com", "pass123");
smtpClient.EnableSsl = false;
// then your other statements like: from, to, body, to send mail
this guide will help you setup custom NetworkCredentials in HMailServer as used above, hope helps someone.
I have stumbled on this question when trying to configure hMailServer to work to e-mail sending from C#. I have tried the following:
C# SmtpClient - does not work with implicit SSL - see this question and answers
AegisImplicitMail from here - could not make it work with UTF-8 strings (I have diacritics in my strings)
MailKit from here - very powerful and mature, no problems using it
I aimed for the following:
decent security
being able to send e-mails to mainstream e-mail providers (e.g. Google, Yahoo) and reach Inbox
being able to receive e-mails from mainstream e-mail providers
C# code
public void MailKitSend(string senderEmail, string senderName, string subject, string bodyText, string receivers, string receiversCc)
{
// no receivers, no e-mail is sent
if (string.IsNullOrEmpty(receivers))
return;
var msg = new MimeMessage();
msg.From.Add(new MailboxAddress(Encoding.UTF8, senderName, senderEmail));
msg.Subject = subject;
var bb = new BodyBuilder {HtmlBody = bodyText};
msg.Body = bb.ToMessageBody();
IList<string> receiversEmails = receivers.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string receiver in receiversEmails)
msg.To.Add(new MailboxAddress(Encoding.UTF8, "", receiver));
if (!string.IsNullOrEmpty(receiversCc))
{
IList<string> receiversEmailsCc = receiversCc.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string receiverCc in receiversEmailsCc)
msg.Cc.Add(new MailboxAddress(Encoding.UTF8, "", receiverCc));
}
try
{
var sc = new MailKit.Net.Smtp.SmtpClient();
if (!string.IsNullOrWhiteSpace(SmtpUser) && !string.IsNullOrWhiteSpace(SmtpPassword))
{
sc.Connect(SmtpServer, 465);
sc.Authenticate(SmtpUser, SmtpPassword);
}
sc.Send(msg);
sc.Disconnect(true);
}
catch (Exception exc)
{
string err = $"Error sending e-mail from {senderEmail} ({senderName}) to {receivers}: {exc}";
throw new ApplicationException(err);
}
}
hMailServer configuration
1) Opened ports - 25, 143, 465, 995 are opened to ensure that you can send and receive e-mail
2) TCP/IP ports configuration
SMTP / 0.0.0.0 / port 25 / no security (allow receiving start process)
SMTP / 0.0.0.0 / port 465 / SSL/TLS security (must define a SSL certificate)
POP3 / 0.0.0.0 / port 995 / SSL/TLS security (use the same SSL certificate)
3) pre C# testing
Run Diagnostics from hMailServer Administrator
Use an e-mail client that allows manual configuration of various settings such as ports for each protocol, security. I have used Thunderbird. Include sending of e-mails to external providers and receiving e-mails from them (I have tried with Gmail).
I made no changes in IP ranges and left the implicit ones (My computer and the Internet).
Although it's 7 years passed since the accepted answer was posted - I also upvoted it in the beginning - I want to emphasize that the suggested solution disables the whole authentication process which is unnecessary. The problem is the line with :
smtp.UseDefaultCredentials = false;
Just remove that line and it should work.
I post here the working solution for me (note that I'm not using SSL):
MailMessage mail = new MailMessage("a1#test.com", "foooo#gmail.com");
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("a1#test.com", "test");
client.Port = 25;
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "...IPv4 Address from ipconfig...";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
I'm attempting to send email using CDO. I'm wanting to change the settings to always send from a specific smtp server with a specific user, pass, and from. However, when I attempt to change the config, I get an error that the data is readonly. How do you go about changing the config of the message?
Message msg = new Message();
IConfiguration config = msg.Configuration;
config.Fields["smtpserver"] = "SERVER";
msg.Subject = "TEST";
msg.From = "FROM#FROM.com";
msg.To = "TO#TO.com";
msg.TextBody = "TESTING";
msg.Send();
I've attempted using System.Net.Mail, but that seems to be firewall blocked. I get the exception message Unable to connect to the remote server : No connection could be made because the target machine actively refused it {IP}:67
MailMessage msg = new MailMessage();
msg.Subject = "TESTING";
msg.From = new MailAddress("MYMAIL#MYMAIL.org");
msg.To.Add(new System.Net.Mail.MailAddress("TOMAIL#TOMAIL.org"));
msg.Body = "dubbly doo";
SmtpClient client = new SmtpClient();
client.Host = "HOST";
client.Port = 67;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(msg);
}
catch(SmtpException e)
{
Console.Write(e.InnerException.Message+":"+e.InnerException.InnerException.Message);
Console.ReadLine();
}
Is using CDO a requirement? You are already using C#, so I recommend porting your CDO code to System.Net.Mail.
http://msdn.microsoft.com/en-us/library/dk1fb84h.aspx
Edit:
Since it sounds in the comments like you are having configuration issues with System.Net.Mail, I would use some of the SysInternals tools (specifically TcpView) to monitor your connections as you step through the CDO code. That way you can see what IP and ports your code is using to connect.
Then armed with that information, you should be able to configure your System.Net.Mail code with the correct settings.