C# sending email code suddenly stopped working - c#

i have the following code that has worked fine for months but suddently stopped working on this line:
smtpClient.Send(msg);
this is an internal application sending emails to internal people in my company
with the following error:
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for ABC#comp.com
can anyone think of a reason why this code would work fine for ages and suddently just stop work ?
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmailAddress_);
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(email.Trim()));
msg.Subject = subject_;
msg.Body = body_;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
var smtpClient = new SmtpClient(_mailServer);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(_user, _pwd);
try
{
smtpClient.Send(msg);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}

The mail server you're connecting to has been tightened up to not allow relaying either for your credentials or from your network/IP. This is not a code issue (short of changing the SMTP server) but an issue or question for whoever manages the SMTP server you're using.

Apart from checking to ensure the smtp service is running you can try the following:
Try setting the SmtpDeliveryMethod enum to PickupDirectoryFromIis.
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx

Related

Send email connection refused

I have an application that worked perfectly until this Saturday.
Every time I try send an email I receive the error "ExtendedSocketException: Connection refused [::ffff:xxx.xxx.xxx.xxxx]:25".
I tried with 465 port but no solve the problem and it's show the error IOException: Unable to read data from the transport connection: The connection was closed.
The account email that I use in this app I use in my smartphone too and yesterday the I lost the connection with server. I contacted my provider and after remove the account and configured its started working again.
In the app I have this code
SmtpClient smtp = new SmtpClient("mail.yyyy.com", 465);
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("noreply#yyyy.com", "xxxxx");
MailAddress from = new MailAddress("noreply#yyyy.com", "yyyy - NoReply");
MailAddress to = new MailAddress(mail);
MailMessage mailMessage = new MailMessage(from, to);
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
reader = new StreamReader("wwwroot/templates/emails/comment-company.htm");
body = reader.ReadToEnd();
body = body.Replace("{url}", url);
body = body.Replace("{company}", name);
mailMessage.Body = body;
smtp.Send(mailMessage);
Anyone have an idea what I can do to solve this?
Try port 587 instead of 465. Port 465 is technically deprecated.

C# "Additional information: The SMTP server requires a secure connection or the client was not authenticated."

So im trying to send an email on C# and always getting the same error: "Additional information: The SMTP server requires a secure connection or the client was not authenticated." i have checked out pretty much every single link i could find about this i have diabled all gmail protection and my credentials are correct i don't know why this error continues
MailMessage mail = new MailMessage();
mail.From = new MailAddress("xx#gmail.com");
mail.Sender = new MailAddress("xx#gmail.com");
mail.To.Add("external#emailaddress");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Body content from";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("xx#gmail.com", "xx");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Timeout = 30000;
try
{
smtp.Send(mail);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message.ToString());
}
This is the exception message from C#, what is the actual response from the server? You may need to enable "less secure apps" to connect to your gmail account first (see https://www.google.com/settings/security/lesssecureapps).
I have tried your code getting the error "5.5.1 Authentication required". Please give this link a try Sending email through Gmail SMTP server with C#

SmtpClient is unable to connect to Gmail using SSL

I see many posts on SMTPClient but I'm still stuck.
I am creating a C# Console application targeting the .NET 4.0 framework and running on a Windows 2008 R2 Datacenter (server).
I want send an email using SSL from a gmail account I just created.
If I use port 587, I get an exception:
The SMTP server requires a secure connection or the client was not authenticated
If I use port 465, I get another one:
Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.
I realize this question has been asked several times in the past. All answers I see relate to the order in which UseDefaultCredentials is set. It must be set to false BEFORE the real credentials are set. I have followed this advice (as per the code below) and yet I still get a failure.
Other answers include switching ports from 465 to 587... which i have tried and just results in a different error (as stated above).
Here's my code:
NetworkCredential credentials = new NetworkCredential("something#gmail.com", "2-step-auth-Password");
MailAddress address = new MailAddress("something#gmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com", 465); // or 587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = credentials;
client.Timeout = 10000; // 10 seconds.
client.Host = "smtp.gmail.com";
//client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
MailMessage message = new MailMessage("example#gmail.com", recipients);
message.Subject = "Hello";
message.Body = "Goodbye";
message.BodyEncoding = Encoding.UTF8;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());
}
message.Dispose();
Update 2: I see another answer which may be my issue: gmail requires an application-specific password (which is apparently not available with my current gmail account. not sure why yet). but this may be my issue.
Update 3: I have followed all instructions. Generated 2-step authentication with Gmail, and still fails.
Below code works for me, you can try this:
SmtpClient smtp = new SmtpClient("smtp.office365.com");
//Your Port gets set to what you found in the "POP, IMAP, and SMTP access" section from Web Outlook
smtp.Port = 587;
//Set EnableSsl to true so you can connect via TLS
smtp.EnableSsl = true;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("Username", "Password");
smtp.Credentials = cred;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Sender Email Address", "Sender Name");
StringBuilder mailBody = new StringBuilder();
mail.To.Add("ToEmailAddress");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "Body Text";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}

SMTP relay - send email from console application

Is it possible to send email from a server that uses smtp-relay through a .net application.
I'm using app.config to get the actual values ex server IP, and the fromadress that the email should use.
According to the IT-technician the username and password to authorize should not be needed because it uses smtp-relay. The computer that are going to send the email is on the smtp-servers list of valid computers.
Can this actually work, don't I need to specify the username/pwd?
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(_smtpserver);
mail.From = new MailAddress(_fromAdress);
mail.To.Add(_toAdress);
mail.Subject = _subject;
mail.Body = _body;
mail.Priority = MailPriority.High;
SmtpServer.Port = Convert.ToInt32(_port);
SmtpServer.Credentials = new System.Net.NetworkCredential(_authUsername, _authPassword);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
In your snippet you are specifying credentials
SmtpServer.Credentials = new System.Net.NetworkCredential(_authUsername, _authPassword);
You can remove that line and it still will work if SMTP relay is configured as you said.
in your case,if you are seting your username and password in webconfig even then you need to call it like above.
but if you using your default mail credentials then it will pick automatically..
and if you are using diiferent mail client for sender then you have to pass credentials..
I had same situation. I got same error code. I noticed , I had used system.web.mail. But my application is a console application. I changed library, I used system.net.mail. And it works now.

Sending email using .NET

I have the following code but I am getting an exception that a smtp host is not defined. If I am running this and testing on my local machine from visual studio, what do I need to do to be able to send email from my machine. Do I have to turn on some Windows service?
private void SendMailToAdminToApprove(string email_, string name_)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("address#domain.com", "Person's Name");
msg.To.Add(new MailAddress("a#gmail.com", "Adam"));
msg.Subject = "Message Subject";
msg.Body = "Mail body content";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
try
{
SmtpClient c = new SmtpClient();
c.Send(msg);
}
catch (Exception ex)
{
Console.Write("T");
}
}
You need to set the SMTP host to point to an actual SMTP server. One option is to run the SMTP service on your own machine, but you could also point to your ISP's server.
edit
As pcampbell and Skeolan mentioned, the actual value should go into app.config. I'm not sure if localhost would be an exception: it would depend on whether you want the option of not running a local server.
You'll need to specify the SMTP host here:
string smtpHost = "localhost";
//or go to your config file
smtpHost = ConfigurationManager.AppSettings["MySmtpHost"].ToString();
SmtpClient c = new SmtpClient(smtpHost);
You need to define the SMTP relay:
SmtpClient c = new SmtpClient("relay.yourdomain.com");
or if you're running the relay locally:
SmtpClient c = new SmtpClient("localhost");
You should change this section:
SmtpClient c = new SmtpClient();
// Either specify a SMTP server above, or set c.Host
c.Send(msg);
You need to specify which SMTP server is going to be used for sending this message. If you install a SMTP server locally, this could be localhost - but otherwise, you'll need to have an outgoing mail server set appropriately.
Here is the code I use for sending email with C#. I also commented out code for sending it to a file locally if you need it.
SmtpClient smtp = new SmtpClient(smtpServer, portNumber);
// Disable SSL when saving to directory.
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(mailFrom, password);
// Set mail to be delivered to a folder
//smtp.PickupDirectoryLocation = #"C:\mail\Send";
//smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

Categories