Can't send mail using SmtpClient - c#

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.

Related

How do I send an email with Gmail and SmtpClient when the sending account uses two factor authentication?

SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("sender#gmail.com", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("sender#gmail.com");
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>Hello, this is a demo ... ..</h1>";
message.To.Add("receiver#gmail.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
ex.ToString();
}
// The thing is that this code works fine for gmails without phone number protection. Exception occurs when using this code with gmails that are protected(verified) via the client phone number.
One of the solution is to use a remote server to access clients mails.
Now my question is there another method to solve this issue ? other than third parties.
If I understand you correctly, you're saying the Google account is using two-factor authentication.
If that's the case, you need to create an Application Password for this. Go to https://security.google.com/settings/security/apppasswords once logged in as the account you want to two-factor auth with.
In the list, under Select App choose "Other" and give it some name. Click Generate, and write this password DOWN cause you will only ever see it ONCE. You will use this in your authentication. It will be 16-characters long and the spaces don't matter, you can include them or omit them. I included them here just because.
NetworkCredential basicCredential =
new NetworkCredential("sender#gmail.com", "cadf afal rqcf cafo");
MailMessage message = new MailMessage();

error in sending email by yahoo smtp

i config my outlook 2010 by thie article to send and receive email from yahoo.com it works good without any problem.
i develop a small application to send my emails by my application but it gave me errors:
"unable to read data from the transport connection:an exist connection was
forcibly closed by the remote host."
my codes:
try
{
SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 465);
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("myid", "mypass");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress("myid#yahoo.com", "blabla");
mailMessage.To.Add(new System.Net.Mail.MailAddress("xxx#live.com", "xxx#live.com"));
mailMessage.Subject = "test";
mailMessage.Body = "test";
mailMessage.IsBodyHtml = false;
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.Priority = MailPriority.High;
smtp.Send(mailMessage);
Console.WriteLine("hooooooooooraaaaaaaaaaaaaaa");
Console.ReadKey();
}
catch (Exception err)
{
Console.WriteLine(err.InnerException.Message);
Console.ReadKey();
return;
}
From MSDN
Some SMTP servers require that the client be authenticated before the
server sends e-mail on its behalf. Set this property to true when this
SmtpClient object should, if requested by the server, authenticate
using the default credentials of the currently logged on user. For
client applications, this is the desired behavior in most scenarios.
The UseDefaultCredentials = true sends to the SMTP server the credentials of the current logged in user (i.e. the Windows User) not the credentials you have defined.
Try with UseDefaultCredentials = false

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.

Send mail from C# code using lotuslive credentials?

I am trying to send mail from C# code using lotuslive smtp. But I have no success in sending the mail. everytime it says {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}.
My code is working fine for other email hosts like gmail and yahoo.
below is the code that I have used.
MailMessage message = new MailMessage();
message.From = new MailAddress("fromaddress");
message.To.Add(new MailAddress("toaddress"));
message.Subject = "Test";
message.Body = "test";
SmtpClient client = new SmtpClient("companyname-com-smtp.mail.lotuslive.com", 465);
client.UseDefaultCredentials = false;
NetworkCredential credential = new NetworkCredential("companycredentials", "password");
client.Credentials = credential;
client.EnableSsl = true;
try
{
client.Send(message);
}
catch(Exception ex)
{
}
Outgoing SSL SMTP Server: -smtp.mail.lotuslive.com (port: 465) Please
Note: Outgoing SMTP access for third party email clients is not
available for Trial accounts.
If it is trail account then may cause some problems.
MailClient = new SmtpClient();
MailClient.Host = "smtp.mail.lotuslive.com/your host address";
MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailClient.Credentials = new System.Net.NetworkCredential(username, password);
MailClient.EnableSsl = true;
MailClient.Port = 465;
If you do not have demo account then Check this link - How to configure client in Outlook 2003.
Check these outlook configure settings match to your code settings.
If all this stuff is not the issue then it may be problem at your mail server. Check these links for information:
An existing connection was forcibly closed by the remote host in SMTP client
System.Net.Mail with SSL to authenticate against port 465

How can I make SMTP authenticated in C#

I create new ASP.NET web application that use SMTP to send message. The problem is the smtp was not authenticated from who send the message.
How can I make SMTP authenticated in my program? does C# have a class that have attribute for enter username and password?
using System.Net;
using System.Net.Mail;
using(SmtpClient smtpClient = new SmtpClient())
{
var basicCredential = new NetworkCredential("username", "password");
using(MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress("from#yourdomain.com");
smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
// Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("to#anydomain.com");
try
{
smtpClient.Send(message);
}
catch(Exception ex)
{
//Error, could not send the message
Response.Write(ex.Message);
}
}
}
You may use the above code.
Ensure you set SmtpClient.Credentials after calling SmtpClient.UseDefaultCredentials = false.
The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.
Set the Credentials property before sending the message.
To send a message through TLS/SSL, you need to set Ssl of the SmtpClient class to true.
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;
client.EnableSsl = true;
client.Send(message);
How do you send the message?
The classes in the System.Net.Mail namespace (which is probably what you should use) has full support for authentication, either specified in Web.config, or using the SmtpClient.Credentials property.
In my case even after following all of the above. I had to upgrade my project from .net 3.5 to .net 4 to authorize against our internal exchange 2010 mail server.

Categories

Resources