I am trying to test out sending an email in my asp.net mvc application but I keep getting the exception below thrown:
"An attempt was made to access a socket in a way forbidden by its access permissions ipaddress:587"
Any ideas what it could be? I have turned off windows firewall and I still get the exception. I have included the using System.Net.Mail; statement and my code is below:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.Credentials = new System.Net.NetworkCredential("info#mydomain.com", "mypassword");
mail.From = new MailAddress("info#mydomain.com");
mail.To.Add("myemail#gmail.com");
mail.Subject = "Testing application";
mail.Body = "Testing the application email";
SmtpServer.Send(mail);
This is a code that working. Have two extra setting compare it with yours, maybe this is the issue.
MailMessage msgMail = new MailMessage("a#gmail.com", "b#mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a#gmail.com", "a");
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
}
Related
I'm trying to send an e-mail using C#, but I get this recurring error :
.
Can you explain me what's wrong with my code ?
Here it is :
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myEmailAdress#gmail.com", "myPassword");
MailMessage msg = new MailMessage();
msg.To.Add("receiver#gmail.com");
msg.From = new MailAddress("sender#gmail.com");
msg.Subject = "My subject";
msg.Body = "My body";
client.Send(msg);
MessageBox.Show("Message sent !");
I have encountered same before.
You are getting this error because you haven't set ON on Less secure app access for sender#gmail.com as you are using Gmail SMTP port.
Reason:
Your email has no remotely access permission. You have to configure
it. Suppose you want to sent email from sender#gmail.com so you
have set that permission NO to this account.
How To Set:
You could try like below
Or can open that tab from this link directly Less secure app access
Update:
As per your comment this is for you which has working perfectly since the beginning of my career
public object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
// mail.Attachments.Add(new Attachment(#attacmmentLocationPath));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Hope that would help.
This code works for gmail, it is very similar to yours with slight differences, but if you try this, and it doesn't work for you, the issue is not the code - perhaps some other network related issue that you are going to need to fix first:
using (var msg = new MailMessage())
{
msg.From = new MailAddress("fromaddress#gmail.com");
msg.To.Add("toaddress#gmail.com");
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("xxx#gmail.com","password");
smtp.Send(msg);
}
}
An SMTP client may log in using an authentication mechanism chosen among those supported by the SMTP servers.
I am struggling with attempting to send email from a .NET application.
I have tried everything from making a new account both gmail and yahoo (changed the host for yahoo), as well as, changing the port using and not using mail.To, allowing less secure apps, i also tried enabling 2 step verification and giving an application password for gmail all of which have failed with the same caught error of: {"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required"}
This is my code:
SmtpClient smtpClient = new SmtpClient();
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myemail#gmail.com", "pw");
smtpClient.Credentials = credentials;
smtpClient.UseDefaultCredentials = false;
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("myemail#gmail.com");
mail.To.Add("myemail#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
try
{
smtpClient.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mail != null)
{
mail.Dispose();
}
}
Solution: as people may have the problem in the future the solution was to remove the line of code: smtpClient.UseDefaultCredentials = false;
smtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myemail#gmail.com", "pw");
smtpClient.Credentials = credentials;
Use those lines in such order.
I have a winforms app that sends email with code below:
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.Priority = MailPriority.High;
mail.From = new MailAddress("autoemailer#no-reply", "Auto Email");
mail.Subject = "Test Email";
mail.Body = "Kindly disregard if received";
SmtpClient smtp = new SmtpClient();
smtp.Host = "172.16.224.7";
smtp.Port = 25; //587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("username", "password"); // Sender's user name and password. Note: The username and password here is an example. Real username and password are the same as outlook's
try
{
smtp.Send(mail);
MessageBox.Show("Sent!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I'm getting the following exception:
Insufficient system storage. The server response was: 4.3.1
Insufficient system resources
The exception message was clear that the server is low on storage/resources. But when I tried sending email thru Outlook, it sent successfully. Is there something wrong with the code? TIA for any help.
I'm sending simple email messages in my application using smtp client and I was using this code before and it just works fine. Now, when I tried to run my project again from my local host computer and try to send email messages. I got a runtime error that says
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
I don't know what just happened since it was working fine before. Now I can't send email and all I've got is this error. I could hardly troubleshoot what went wrong. How do I resolve this? Here's my code below: Thanks...
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(#"myemailaddress#gmail.com",#"myemailpassword");
// create message
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox4.Text);
message.To.Add(new MailAddress(TextBox1.Text));
message.Subject = TextBox2.Text;
message.Body = TextBox3.Text; //body of the message to be sent
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
// message.Subject = "subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
try
{
client.Send(message);
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Mail has been successfully sent!')", true);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
message.Dispose();
}
Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.
Also please go to this link and click on Continue Allow access to your Google account
also I edit it little bit :
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
I am trying to send email using c# following is my code.
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("kmrizwan.shahid#gmail.com");
msg.To.Add("kmrizwan.shahid#gmail.com");//Text Box for To Address
msg.Subject = "Testinng subject"; //Text Box for subject
msg.IsBodyHtml = true;
msg.Body = "testing comment is here..";//Text Box for body
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("kmrizwan.shahid#gmail.com", "");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = msg;
client.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
Giving following exception
It's common for internet providers to block the port used by SMTP, except to their own outgoing mailserver. The reason is to prevent spamming.
If that is the case for you, you need to use the mail server of your internet provider instead of the GMail server.