Unable to connect to local server in C# mail program - c#

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
}
}

Related

Send email with an attachment

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

error to send mail (gmail to gmail)

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!

How to fix the smtp authentication runtime error when sending email using smtp client?

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();
}
}

Need to catch the proper error for SMTP send email code

For some reason my program does not send email using SMTP server, it stops somewhere in this code, and I can't debug it, because it happens with the compiled .exe
public static void sendLog(MailMessage mail) {
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Constants.EMAIL_SENDER);
mail.To.Add(Constants.EMAIL_RECEIVER);
mail.Subject = Environment.UserName;
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential(Constants.EMAIL_SENDER, Constants.EMAIL_SENDER_PASSWORD);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
what I need is to know what try/catch to use, and to handle the proper Exception to write in a .txt file?
You should use proper SMTP Exception
catch (SmtpException ex)
{
// write to log file
string msg = "Failure sending email"+ex.Message+" "+ex.StackTrace;
}

Exception thrown while trying to send mail through gmail SMTP

The below exception is thrown while trying to send mail by below code:
SMPT server requires a secure connection or the client was not authenticated. The server response was 5.5.1. Authentication required
C#
string E_mail_ID = "hidden";
try
{
SmtpClient gmail_client = new SmtpClient("smtp.gmail.com");
gmail_client.Port = 587;
gmail_client.EnableSsl = true;
gmail_client.Timeout = 100000;
gmail_client.DeliveryMethod = SmtpDeliveryMethod.Network;
gmail_client.UseDefaultCredentials = false;
gmail_client.Credentials = new NetworkCredential("hidden", "hidden");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("hidden");
msg.To.Add(E_mail_ID.Trim());
msg.Body = "Request for quotation from Jeet fly ash products, a unit of Vidya shakti niyas";
msg.Attachments.Add(new Att![enter image description here][1]achment(pdfFile));
gmail_client.Send(msg);
MessageBox.Show("RFQ sent to vendor successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Try the port: 465 - This is ssl for gmail.
Take a look: Configure Gmail-Account Outlook

Categories