I am getting following error message while sending email using gmailD.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress("suraj.podval#in.vsolutions.com");
objMailMessage.To.Add(new MailAddress("itslaxman#gmail.com"));
objMailMessage.Subject = "Test";
objMailMessage.Body = "Test Test";
objMailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("user#gmail.com", "password");
smtpClient.Send(objMailMessage);
Try changing the port to 465
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Your gmail email address
oMail.From = "gmailid#gmail.com";
// Set recipient email address
oMail.To = "support#emailarchitect.net";
// Set email subject
oMail.Subject = "test email from gmail account";
// Set email body
oMail.TextBody = "this is a test email sent from c# project with gmail.";
// Gmail SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// If you want to use direct SSL 465 port,
// please add this line, otherwise TLS will be used.
// oServer.Port = 465;
// detect SSL/TLS automatically
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// Gmail user authentication
// For example: your email is "gmailid#gmail.com", then the user should be the same
oServer.User = "gmailid#gmail.com";
oServer.Password = "yourpassword";
check below link: http://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=2
Related
I am trying to send email using office365 e-mail account that has enabled the two-factor authentication. It gives an authentication failed error. For email accounts that have not enabled the two-factor authentication works fine. How to resolve this issue?
using (SmtpClient client = new SmtpClient())
{
client.Port = Convert.ToInt32(appSettings["Port"]);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(SenderMailAddress, SenderMailPassword);
email.Subject = String.Format("{0}", txtMailSubject.Text);
//
email.Body = String.Format("{0}", text);
email.IsBodyHtml = true;
client.Send(email);
}
Error message is
System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.7.1 Client was not authenticated at
System.Net.Mail.MailCommand.CheckResponse
You have to create an application password, then use the new application password in your code, check this link
MailAddress from = new MailAddress("fromid");
MailAddress to = new MailAddress("toID");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"The sensor get offline ...";
SmtpClient client = new SmtpClient(server);
client.Host = "hostID";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential
{
UserName = "**********",
Password = "*************,
};
Console.WriteLine("Sending an email message to {0} using the SMTPhost {1}.");
I created console application for sending Email
public static void sendEmail(string email, string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.From = new MailAddress("test#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("test#gmail.com", "admin#1234"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
}
}
I am using correct credential of my gmail account.
Is there any settings I need to do for GMail Account?
It's likely that you're actually getting this error, it's just suppressed in some way by you being in a console app.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Change this piece of code and add one extra line. It's important that it goes before the credentials.
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
// ** HERE! **
smtp.UseDefaultCredentials = false;
// ***********
smtp.Credentials = new System.Net.NetworkCredential("test#gmail.com", "admin#1234"); // ***use valid credentials***
smtp.Port = 587;
You'll also need to go here and enable less secure apps.
https://www.google.com/settings/security/lesssecureapps
Or if you have two step authentication on your account you'll need to set an app specific password, then use that password in your code instead of your main account password.
I've just tested and verified this works on my two step account. Hell, here's my entire method copied right out of LINQPad in case it helps (with removed details of course).
var fromAddress = new MailAddress("myaccount#gmail.com", "My Name");
var toAddress = new MailAddress("test.address#email.com", "Mr Test");
const string fromPassword = "tbhagpfpcxwhkczd";
const string subject = "test";
const string body = "HEY, LISTEN!";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Edit:
Using attachments:
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
Attachment attachment = new Attachment(filePath);
message.Attachments.Add(attachment);
smtp.Send(message);
}
In my experienced using C# SMTP Client, there is a certain steps to follow in the code (see code snippet below)
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("account#gmail.com", "secretPassword");
smtpClient.Send(mail);
I have tried sending mail from gmail and other share hosting mail server and its works as expected
But Now my requirement is sending mail from windows mail server.whenever i try to send mail it shows gives me error failure sending mail.
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "us2.webmail.mailhostbox.com";
string fromaddress = "info#csi.com";
client.Port = 25;
//client.Host = "mail.csisite.com";
// setup Smtp authentication
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("info#csisite.com", "password");
//client.UseDefaultCredentials = true;
client.Credentials = credentials;
client.EnableSsl = false;
try
{
//mail For Customer
MailMessage msgcustomer = new MailMessage();
msgcustomer.From = new MailAddress(fromaddress);
msgcustomer.To.Add(new MailAddress(EmailID));
msgcustomer.Subject = "Thank you for writing to us" ;
msgcustomer.IsBodyHtml = true;
msgcustomer.Body = "Test Mail";
client.Send(msgcustomer)
Send Mail:
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
using System.Net.Mail
MailMessage msgMail = new MailMessage();
MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress("sender's email","sender`s name and surname");
myMessage.To.Add("recipient's email");
myMessage.Subject = "Subject";
myMessage.IsBodyHtml = true;
myMessage.Body = "Message Body";
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
mySmtpClient.Host = "your smtp host address";
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.ServicePoint.MaxIdleTime = 1;
mySmtpClient.Send(myMessage);
myMessage.Dispose();
updated user is getting the following error
{"A socket operation was attempted to an unreachable network 208.91.199.230:25"} and Error Code is 10051
The Error 10051 is a socket error that appears when the target network or host server can not be reached. The error could have occurred due to an erroneous configuration of the router, Internet disconnection, or a blocking action by a firewall. This error has occurred mostly in attempts to generate Internet Control Message Protocol (ICMP) and Simple Mail Transfer Protocol (SMTP) connections with wrong configurations. This error also indicates that the Windows software employed is configured to communicate to a router, since the 10065 error appears instead if Windows is not set to link to a router.
you should make ensure that the network path is correct, that the computer is not running two software firewalls and that the targeted computer is not turned off.
Use This Code for sending mail this is tested code.
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
} // Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
mSmtpClient.Credentials = new System.Net.NetworkCredential
("YourEmail#gmail.com", "Password");
//Or your Smtp Email ID and Password
mSmtpClient.EnableSsl = true;
mSmtpClient.Send(mMailMessage);
}
Note: I have been trying to send email through my ISP mail server with no success - following all the suggestions that I could find on these forums. I just discovered that it would work fine if I did not attempt to alter the default credentials setting. If I included either: client.UseDefaultCredentials = true; or client.UseDefaultCredentials = false; in my code, then I would get a login failure message from the server. By not including either, it worked fine.
In C# I have a method that sends an email via a gmail account.
When I open the email in microsoft outlook the from address is shown as the gmail address and not the strFromAddress that I use in the headers.
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("***#gmail.com", "*****");
var strFromAddress = "no-repl#demuynck-printing.be";
var strToAddress = "sander#demuynck-media.be";
var strSubject = "Album: '" + lbltitel.Text + "' bestelling";
var strBody = "<html><head>";
// new instance of MailMessage
MailMessage mailMessage = new MailMessage();
// Sender Address
mailMessage.From = new MailAddress(strFromAddress);
// mailMessage.Headers("Selexion Clix Demuynck <no-reply#demuynck-printing.be>");
// mailMessage.Bcc.Add(new MailAddress("no-reply#demuynck-printing.be"));
// Recepient Address
mailMessage.To.Add(new MailAddress(strToAddress));
mailMessage.Headers.Add("Reply-To", "info#demuynck-printing.be");
// Subject
mailMessage.Subject = strSubject;
// Body
mailMessage.Body = strBody;
// format of mail message
mailMessage.IsBodyHtml = true;
// new instance of Smtpclient
smtp.Send(mailMessage);
Just set the displayname property on the MailAddress like so:
MailAddress fromAddress = new MailAddress("user#domaina.com","no-reply#domainb.com");
GMail will change the 'from' address to the account used to login to the SMTP server, unless the email-address used in 'from' field is verified to belong to the same owner. So, in your gmail preferences, just add and verify this specific from address.
This happens because Google is violating the SMTP protocols. There is a detailed article about it here: http://lee-phillips.org/gmailRewriting/
Steve's comment is incorrect; an authenticated sender should be able to set any From: header.
Possible Exact Duplicate: Sending Email in C#.NET Through Gmail
Hi,
I'm trying to send an email using gmail:
I tried various examples that I found on this site and other sites but I always get the same error:
Unable to connect to the remote server -- > System.net.Sockets.SocketException: No connection could be made because the target actively refused it 209.85.147.109:587
public static void Attempt1()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("MyEmailAddress#gmail.com", "MyPassWord"),
EnableSsl = true
};
client.Send("MyEmailAddress#gmail.com", "some.email#some.com", "test", "testbody");
}
Any ideas?
UPDATE
More details.
Maybe I should say what other attempts I made that gave me the same error:
(Note when i didn't specify a port it tryed port 25)
public static void Attempt2()
{
var fromAddress = new MailAddress("MyEmailAddy#gmail.com", "From Name");
var toAddress = new MailAddress("MyEmailAddy#dfdf.com", "To Name");
const string fromPassword = "pass";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
}
) { smtp.Send(message); }
}
public static void Attempt3()
{
MailMessage mail = new MailMessage();
mail.To.Add("MyEmailAddy#dfdf.com");
mail.From = new MailAddress("MyEmailAddy#gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("MyEmailAddy#gmail.com", "pass");
smtp.EnableSsl = true;
smtp.Send(mail);
}
I'm using the following code:
SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");//username doesn't include #gmail.com
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try {
sc.Send(mm);
} catch (Exception ex) {
EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
}
With the following code, it will work successfully.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#mydomain.com", "Enquiry");
mail.To.Add("abcdef#yahoo.com");
mail.IsBodyHtml = true;
mail.Subject = "Registration";
mail.Body = "Some Text";
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("xyz#gmail.com", "<my gmail pwd>");
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
But, there is a problem with using gmail. The email will be sent successfully, but the recipient inbox will have the gmail address in the 'from address' instead of the 'from address' mentioned in the code.
To solve this, please follow the steps mentioned at the following link.
http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
before following all the above steps, you need to authenticate your gmail account to allow access to your application and also the devices. Please check all the steps for account authentication at the following link:
http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html
Here is my connection resource to connect to Gmail from Java
<!-- Java Mail -->
<Resource name="mail/MailSession" auth="Container" type="javax.mail.Session"
mail.debug="true"
mail.transport.protocol="smtp"
mail.smtp.host="smtp.gmail.com"
mail.smtp.user="youremail#gmail.com"
mail.smtp.password="yourpassword"
mail.smtp.port="465"
mail.smtp.starttls.enable="true"
mail.smtp.auth="true"
mail.smtp.socketFactory.port="465"
mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
mail.smtp.socketFactory.fallback="false"
mail.store.protocol="pop3"
mail.pop3.host="pop.gmail.com"
mail.pop3.port="995" />
Connect your Gmail account on Secure ports (465 for SMTP and 995 for POP3) and use any .NET SSL Factory available to connect securely to Gmail.
Are you sure that your GMail account is set up to allow POP/SMTP connections? It is a configurable option that you can turn on and off as you choose.
You can see my blog post here at http://codersatwork.wordpress.com/2010/02/14/sending-email-using-gmail-smtp-server-and-spring-mail/ which explains how to use spring mail for sending email via gmail smtp server.
I used java but you can see the configuration and use that in your c# code.
Try using port number 465 for SSL connection