I want to send mail (gmail to gmail) this is the code with c#:
I now it's easy and exist more tuto.
I test all tuto to send mail but always the same problem
using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace VerificationBlockage
{
class EnvoyerMail
{
public void sendEmail()
{
// Mail message construction
MailMessage mm = new MailMessage("halloula.briki#gmail.com", "halloula.briki#mail.com");
// content
mm.Subject = "testing message";
mm.Body = "hello... from .net c# mailmessage";
mm.CC.Add("mejdi68#mail.com");
// mm.CC.Add("copycc2#mail.com");
// mm.Bcc.Add("copybcc#mail.com");
// some attachments
//mm.Attachments.Add(new Attachment("c:\\filename.txt"));
// Sending message
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
// ...
// our account credentials
sc.Credentials = new NetworkCredential("halloula.briki#gmail.com", "&******&");
sc.EnableSsl = true;
// Catching result
try
{
sc.Send(mm);
MessageBox.Show("Message sent");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
I don't what's the problem. I change the port 25 , 587, 465.
the error is le serveur ne prend pas en charge les connexions sécurisées
English Translation:
the server does not support secure connections
This suggests the server you are using does not support SSL connections.
Remove the line
sc.EnableSsl = true;
Or Change it to:
sc.EnableSsl = false;
However I am fairly sure Gmail does. Try this:
public string SendGmailMessage(string toAddress, string fromAddress, string ccAddress, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress emailFrom = new MailAddress(fromAddress);
message.From = emailFrom;
message.To.Add(toAddress);
if (!string.IsNullOrEmpty(ccAddress))
{
message.CC.Add(ccAddress);
}
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true; //Add this line
smtpClient.Credentials = new System.Net.NetworkCredential("GMAILUSERNAME", "GMAILPASSWORD");
smtpClient.Send(message);
msg = "Message Sent";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
Maybe that depends on the gmail service connected to that port (server side).
I've seen some examples using SSL connecting to port 465
I hope this helps.
It's most likely an issue with your firewall. Have you checked it? On your cmd prompt check the following:
Telnet smtp.gmail.com 587
If you do not get a valid response back something is blocking that port.
You canset Gmail to allow such activity (which is blocked for security reasons by default...) here: https://www.google.com/settings/security/lesssecureapps . Good luck!
Related
Good day guys,
I have a code that is used to send an email with attachment which I had referred from Finding the exact cause for the exception - System.Net.Sockets.SocketException
Here is the code:
namespace SendEmail
{
class Email
{
public static void Main(string[] args)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
try
{
mail.From = new MailAddress("harish.1138#gmail.com");
mail.To.Add("harish_1138#yahoo.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("C:\\EmailTest.xlsx");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("harish.1138#gmail.com", "SamplePWD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
When I execute this, I get an exception as this:
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"
How do I fix the authentication required exception. Does this means something is blocking me from sending out the email?
Thanks a lot
I had make it to work...
Referred here: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?
as suggested by #ymonad.
namespace SendEmail
{
class Email
{
public static void Main(string[] args)
{
try
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("harish.1138#gmail.com");
mail.To.Add("harish_1138#yahoo.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("C:\\EmailTest.xlsx");
mail.Attachments.Add(attachment);
using (SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587))
{
SmtpServer.UseDefaultCredentials = false; //Need to overwrite this
SmtpServer.Credentials = new System.Net.NetworkCredential("harish.1138#gmail.com", "SamplePWD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
}
MessageBox.Show("Mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
And have to Turn on less secure apps as here:
https://stackoverflow.com/a/38024407/5266708
And it works perfectly...
This exception is thrown, when you do not have access to the SMTP server.
Usually you log in to email with your password and username, when it comes to SMTP server requests, you send your IP as login credential.
If your IP is not authorized, you will not be allowed to use it and will be blocked.
You need to provide credentials, to override the IP authentication:
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer .Host = "mail.youroutgoingsmtpserver.com";
SmtpServer Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
If it does not allow you, then google is blocking your access.
Use this for reference:
Google mail api
I am unable to connect to the frontier mail server with the following code. I get the message "Unable to connect to remote server". I am running the program using C# on my local computer.
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.frontier.com");
mail.From = new MailAddress(emailaddress);
mail.To.Add("xxxx#frontier.com");
mail.Subject = thistitle;
mail.Body = thisdescription;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(thisimage);
mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Email Error Message");
}
Can anyone tell if I have the correct parameters for Frontier mail? I know they use Yahoo but I tried that also with no success. Isn't it possible to run a mail server from my local machine? Any help is appreciated.
just try remove this code SmtpServer.EnableSsl = true;
Does your ISP block SMTP traffic? (this is often the case for non-commercial accounts).
If not... rewrite you code closer to this:
try
{
using (var attachment = new Attachment(thisimage))
using (var mail = new MailMessage())
{
mail.From = new MailAddress(emailaddress);
mail.To.Add("xxxx#frontier.com");
mail.Subject = thistitle;
mail.Body = thisdescription;
mail.Attachments.Add(attachment);
using (var client = new SmtpClient("smtp.frontier.com"))
{
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
client.EnableSsl = true;
client.Send(mail);
}
}
MessageBox.Show("Mail sent");
}
catch (SmtpException ex)
{
// go read through https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx
// go read through https://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception(v=vs.110).aspx
}
#if DEBUG
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Email Error Message");
}
#endif
}
...and run it in a debugger and look at what the SmtpException is reporting. There are myriad reasons why you may fail a connection.
Im unable to comment so I will type my comment as an answer. Are you able to use ImapClient instead of SmtpClient ? With Imap, you can do some authenticating processes. Could be the issue, it only looks like you are signing in. For Imap I do this:
using (var clientTest = new ImapClient())
{
clientTest.Connect("xxxx#frontier.com");
clientTest.AuthenticationMechanisms.Remove("XOAUTH");
clientTest.Authenticate(eMail, pw);
bIsConnected = clientTest.IsConnected;
if (bIsConnected == true)
{
/// Insert Code here
}
}
When I send mail to my gmail account, it shows below error.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required...
code I am using is below
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m.From = new MailAddress("me#gmail.com");
m.To.Add("me#gmail.com");
m.Subject = "This is a Test Mail";
m.IsBodyHtml = true;
m.Body = "test gmail";
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("me#gmail.com", "passward");
sc.UseDefaultCredentials = true;
sc.EnableSsl = true;
sc.Send(m);
Response.Write("Email Send successfully");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Just tried your code, had to fiddle with a couple things but was sent this. Funny because I have done this previously using Gmail smtp (couple years back). But it looks like they are now verifying apps that use their platform.
Either use another smtp server that you are signed up to, or use your own. (there must be a test one that is available online??). Pretty sure sendgrid do a free trial.
using System.Net;
using System.Net.Mail;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "email#yahoo.com";
string password = "abcdefg";
string emailTo = "someone#domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Please try this this should work for you
Thank you
I setup a private email with my namecheap domain but I am having trouble sending e-mails. Below is my code. Am I missing anything? I get a timeout message each time.
//Send email to end user
MailMessage mm = new MailMessage();
foreach(string to in toList)
{
mm.To.Add(to);
}
mm.From = new System.Net.Mail.MailAddress("fromaddress");
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
var smtp = new SmtpClient
{
Host = "mail.privateemail.com",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("username", "pw"),
Timeout = 20000
};
smtp.Send(mm);
Port number may be wrong. I tried with 587 and it worked on gmail.
And consider to remove timeout temporarily to get an exception and find out the detailed reason of the timeout.
The solution is:
Host: smtp.privateemail.com
Port: 587
EnableSsl: true;
SecurityProtocol: (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12);
This code is what I've used with success.
string to = "yo#yo.com";
string from = "help#help.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("mail.privateemail.com");
client.Credentials = new System.Net.NetworkCredential("username", "pass");
client.Port = 587;
client.EnableSsl = true;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}",
ex.ToString());
}
Namecheap now only supports SMTP on port 465 and implicit SSL after their most recent udpate. System.Net.Mail unfortunately does not support this (see here). A work around could be to use the System.Web.Mail namespace (just a heads up it is obsolete) however it does work as seen in this SO post.
Hi I need to send an email using template in c# windows application . I had created a template but i am unable to pass the parameters through the html template. Here is the template which i am using.
For this HTML Template i am calling this in my windows application and sending through gmail smtp. I am able to send the mail but unable to pass the parameters into it. Please Help me out.Here is the code where i am calling in my windows application
try
{
using (StreamReader reader = File.OpenText("H:\\Visitor Management_Project\\Visitor Management_Project\\Visitor Management_Project\\EmailTemplate.htm"))
{
SmtpClient SmtpServer = new SmtpClient("173.194.67.108", 587);
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu#gmail.com", "*******");
//SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
message = new MailMessage();
message.Subject = "Visitor Arrived";
//message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.Body = "EmailTemplate.htm";
//message.BodyEncoding = System.Text.Encoding.UTF8;
message.From = new MailAddress("ambarishkesavarapu#gmail.com");
message.To.Add(lblCPEmail.Text);
message.Priority = MailPriority.High;
message.Body = reader.ReadToEnd();
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpServer.Send(message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
How to add Parameters into HTML Template where i am getting all the parameters of that in the same page in Textboxes. Please help me out
I think you already discovered the answer yourself, but I will keep posting the answer instead.
In case you are using Windows Forms and not Windows Presentation Forms (The different only the design part and many new features that does not have on Windows Forms), what I have done is like this (to send email):
public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
{
try
{
SmtpClient _smtp = new SmtpClient();
MailMessage _message = new MailMessage();
_message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name
_message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited
_message.Subject = _subject; // The subject of the email
_message.Body = _body; // The body of the email
_smtp.Port = 587; // Google mail port
_smtp.Host = "smtp.gmail.com"; // Google mail address
_smtp.EnableSsl = true;
_smtp.UseDefaultCredentials = false;
_smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtp.Send(_message);
ShowMessageBox("Your message has been successfully sent.", "Success", 2);
}
catch (Exception ex)
{
ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
}
}
And I am using it like this:
SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");
Here is the image:
May this answer would help you.
Cheers!