I am trying to send a default passwords using smtp for users who want to retrieve their new passwords!
My Code :
public void SendUserPass()
{
string sql = "Select Username, Password from Registration
where Email='" + Email + "'";
DataTable table = new DataTable();
table = db.RunQuery(sql);
MailMessage message = new MailMessage("Esco#gmail.com", Email);
message.Subject = "ESCO -Forgot Password";
message.Body = "Username " + table.Rows[0][0].ToString() + "<br/> Password" +
table.Rows[0][1].ToString();
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("Esco#gmail.com", "Mypassword");
smtp.EnableSsl = true;
smtp.Send(message);
}
I get this error :
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at....
Line 310: smtp.Send(message);
Any help please ?
This worked for me
Login in to gmail ( in your case it is Esco#gmail.com)
visit
https://www.google.com/settings/security/lesssecureapps
Enable access to less secure apps
Related
I tried to send mails from Microsoft Exchange from our organization, but I get the error message: The customer is not allowed to submit mail to this server. The response from the server was: 4.7.1 : Relay access denied
My Code is :
private void button2_Click(object sender, EventArgs e)
{
string Username = "MyUsername";
string Password = "MyPassword ";
string SmtpServer = "OurSmtpServer";
string From = Username + "SmtpServer ";
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
NetworkCredential basicCredential = new NetworkCredential(Username, Password, SmtpServer);
MailMessage message = new MailMessage();
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(From);
// setup up the host, increase the timeout to 5 minutes
smtpClient.Host = SmtpServer;
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//smtpClient.Credentials = basicCredential;
smtpClient.UseDefaultCredentials = true;
smtpClient.Timeout = (60 * 5 * 1000);
message.From = fromAddress;
message.Subject = " - " + DateTime.Now.Date.ToString().Split(' ')[0];
message.IsBodyHtml = true;
message.Body = " -88888888888888888888888888888 ";
message.To.Add("recipient#gmail.com");
smtpClient.Send(message);
}
We use windows authentication normally in our organization. Is this the cause of the problem? if so how to use windows authentication in my code?
And thank you in advance for your help !!
This is my code to send email after registration is successful. It works fine on my localhost IIS server. But after deploying web-site on server email is not sent to user. There is no exception or error message shown.
MailMessage mm = new MailMessage("xyz#gmail.com", TextBoxEmail.Text.Trim());
mm.Subject = "Password Recovery";
mm.Body = string.Format("Hi ,<br /><br />Your password is .<br /><br />Thank You.");
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "xyz#gmail.com";
NetworkCred.Password = "xyz";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
message = "Registration successful. Activation email has been sent.";
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
same code work on password recovery page. but here it is not working.
I did some changes in code and also create a new page for registration now it is showing a following error
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
first i thought that it is error of mail sending code but it work fine on iis local server and also on other page. so i think may be it is a problem of button click event. just able to reach here. please some help me as i know i get my answer here.
my new code:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("xyz#gmail.com");
mail.Subject = "mailSubject";
mail.Body = "mailBody";
mail.IsBodyHtml = true;
mail.To.Add("xyz#gmail.com");
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))//2nd parameter is PORT No.
{
smtp.Credentials = new System.Net.NetworkCredential("xyz#gmail.com", "xyz");
smtp.EnableSsl = true;//set this as your Host Name properties, for gmail,its true
smtp.Send(mail);//actual sending operation here
}
}
Be sure to use System.Net.Mail, not the deprecated System.Web.Mail.
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
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);
}
Also, You have to create a specific Google password for that app via:
Google> Account> Security> Apps> Manage apps> Add an app
Select a name My Awesome App and Google will generate a custom password.
Another advice is Allow Less Secure Applications in your google account: link
And finally, take in mind that Google don't allow you to send more than 250 messages per hour, no more than 1000 per day (not sure of the last number).
Good day, I'm a beginner from using ASP.net and SMTP Mailer
Heres my Question, I always encounter this Error when i send email from my local
and searched and tried the solutions around the net but not so lucky,
I hope someone point out what codes do i need and where i encounter this errror
Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
Heres my Code:
protected void btnSendEmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Value, txtName.Value);
smtpClient.Credentials = new System.Net.NetworkCredential("myUser#gmail", "password");
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
//Default port will be 25
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("myEmail#gmail.com");
message.Subject = txtSubject.Value;
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("myEmail#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("myEmail#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtaMessage.Value;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.HeadersEncoding = System.Text.Encoding.UTF8;
// Send SMTP mail
smtpClient.Send(message);
lblSuccess.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblSuccess.Text = "Send Email Failed.";
}
}
i tried to make a simple codes for sending email try this
MailMessage mm = new MailMessage();
mm.From = new MailAddress("fromEmail");
mm.To.Add("toEmail");
mm.CC.Add("ccEmail");
mm.Subject = "StringSubject";
mm.Body = "BodySubject";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "UsernameString";
NetworkCred.Password = "PasswordString";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
try
{
smtp.Send(mm);
}
catch (Exception)
{
}
Try this reference ASP Simple SMTP for C# and VB it helps me a lot for may smtp problem
Please have a look on google support team, what they are saying regarding sending mail from application.
https://support.google.com/mail/answer/78775?hl=en
Also following link can help you.
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Goto Account of Gmail , then select
Connected apps & sites
Allow less secure apps: ON(if this is off you cannot send mails through apps,or your websites )
Hello all can anyone please explain what would the UserName and Password be? I believe that those are asking for my gmail's username and password right? (I'm the one who will always receive emails) I am trying to implement the email service for my contact form. Sorry I'm pretty new to asp.net mvc.
public class MailService : IMailService
{
public string SendMail(string from, string to, string subject, string body)
{
var errorMessage = "";
try
{
var msg = new MailMessage(from, to, subject, body);
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "myemail#yahoo.com",
Password = "password"
};
smtp.Credentials = credential;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(msg);
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
return errorMessage;
}
}
Yes. The Username and Password are the ones required by your host. In this case the credentials you need to login at smtp.gmail.com
Yes, you need to authenticate using the login information just as if you were setting up an email client to send emails.
SmtpClient.Credentials Property
Gets or sets the credentials used to authenticate the sender.
....
Some SMTP servers require that the client be authenticated before the server will send e-mail on its behalf.
Yes, it's the gmail username and password.
Remember to create a application specific password and use that instead of the actual gmail password
Found the following code in one of my projects:
static Task sendMail(IdentityMessage message)
{
string text = string.Format("Please confirm your account by pasting this link on your browser\'s address bar:\n\n{0}", message.Body);
string html = "Please confirm your account by clicking this link<br/>";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("id#gmail.com");
msg.To.Add(new MailAddress(message.Destination));
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("id#gmail.com", "gmailpassword");
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
return smtpClient.SendMailAsync(msg);
}
I want to send email with my web application. It is published on rackspace dedicated server but I'm using GoDaddy's SMTP server to send email.
The fault I'm getting is:
System.Net.Mail.SmtpFailedRecipientException: Mailbox name not allowed. The server response was: sorry, relaying denied from your location [xx.xx.xxx.xx] (#5.7.1)
This is my code
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net", 25);
string to ="rpanchal#itaxsmart.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential("EmailId#domain.com","**");
MailAddress fromAddress = new MailAddress("myEmailId#domain.com", "CompanyName");
MailMessage message = new MailMessage();
message.From = fromAddress;
message.To.Add(to);
message.Body = "This is Test message";
message.Subject = "hi";
client.Send(message); message.Dispose(); return "Email Send";
Should I do any configuration on dedicated server?
Are you testing locally? If yes, then your SMTP server may not allow relaying. Do not worry when you will deploy the application there won't be any problem.
If you are hosting with RackSpace you should use the SMTP RackSpace recommends for sending from their servers. Unfortunately, you can only use relay-hosting.secureserver.net if you are sending from go Daddy Shared or 4GH hosting.
Start trying to change your port 465 instead 25.
Or remember Relay-hosting is very limited to 250 emails per day and not accept remote connections so easy. Check if you can use SSL connection.
That is so simple:
You must focus on smtp host, port, ssl...
Change smtp host to: relay-hosting.secureserver.net
And DELETE port and ssl, thats all...
Do not use smtp port and smtp ssl true or false
var fromAddress = "mailfrom#yourdomain";
// any address where the email will be sending
var toAddress = "mailto#yourdomain";
//Password of your mail address
const string fromPassword = "******";
// Passing the values and make a email formate to display
string subject = TextBox1.Text.ToString();
string body = "From: " + TextBox2.Text + "\n";
body += "Email: " + TextBox3.Text + "\n";
body += "Subject: " + TextBox4.Text + "\n";
body += "Message: \n" + TextBox5.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "relay-hosting.secureserver.net";
**//Warning Delete =>//smtp.Port = 80;**
**//Warning Delete =>//smtp.EnableSsl = false;**
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);