Unable to send email via localhost - c#

I am getting the following error:
Failure sending mail
and inner exception is
Unable to connect to the remote server
in C#
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com","Newsletter", "This is a test mail");
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
This is my local smtp email setting in IIS 7.
Also installed the smtp server tools

Try setting the "Email Address" field in the IIS SMTP settings to the FROM address you are using.
SOURCE

Related

Azure Function: Client was not authenticated to send anonymous mail during MAIL FROM [DB6P189CA0021.EURP189.PROD.OUTLOOK.COM]

I want to send email in Azure Function. I write down below code. It works properly in console app & I am able to send email using the credentials. But when I tested the same code in Azure Function it throws me below error.
Exception while executing function: Functions.HttpTriggerCSharp. Microsoft.Azure.WebJobs.Script: One or more errors occurred. f-HttpTriggerCSharp__-1774598883: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [DB6P189CA0021.EURP189.PROD.OUTLOOK.COM]
The code I used -
SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
string _sender = "--email--";
string _password = "-password---";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(_sender, _password);
client.EnableSsl = true;
client.Credentials = credentials;
string recipient = "--test#outlook.com--";
string subject="Temperature of device exceeds";
string message="Temperature of device exceeds";
try
{
var mail = new MailMessage(_sender.Trim(), recipient.Trim());
mail.Subject = subject;
mail.Body = message;
client.Send(mail);
}
catch (Exception ex)
{
}
I use a queuetrigger and follow your code in my azure function(v1) and it works well.
The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
This message indicates that the SMTP server configured in your Outgoing Mail Account is connecting to an SMTP client submission endpoint which cannot be used for direct send.
Configure your Exchange SMTP to direct send.
Configure the email notification In the DS-Client > setup > configuration > notification Selected SMTP, add the SMTP server settings and selected add the SMTP server settings
Server require authentication.
Add the office365 authenticated account information in the SMTP server authentication window.
Refer to the following Microsoft articles for more information:
Fix issues with printers, scanners, and LOB applications that send email using Office 365
How to set up a multifunction device or application to send email using Office 365

Unable to connect remote sever (SMTP , Hosting Server)

I am trying to send mail through SMTP but i am getting the error unable to connect remote server, I have tried alot but still getting same error
I am hosted my site on Go Daddy,
please any body suggest me where i am wrong , or is there any settings on GoDaddy server to send mail from "relay-hosting.secureserver.net"
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient("
relay-hosting.secureserver.net", 25);
Client.Credentials = CredentialCache.DefaultNetworkCredentials;
Client.DeliveryMethod = SmtpDeliveryMethod.Network;
Client.Send(message);
You are trying to send mail using remote SMTP server, but using credentials from your local environment.
Try to use another credentials class type to specify it more concrete:
Client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
Referring to your code:
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient("
relay-hosting.secureserver.net", 25);
You're using port 25, which is actually the problem. System Administrators who utilize GoDaddy as their hosting provider may experience trouble sending mail over port 25. This is because GoDaddy requires their servers to send through their own SMTP Relay instead of any third party. This is really a frustration situation, but this is what Godaddy policy is. If you use Godaddy relay, then it will work.

SMTP works in Visual Studio but not when uploading to production IIS Server

I have narrowed this down to an issue with IIS. I am using system.net.mail to send emails. They work on my local box, but when I publish them to my IIS8 Server 2012 Server, the emails fail to send.
Try
Dim mailMessage As New MailMessage("test#test.com", Me.ddlApprovers.SelectedValue)
mailMessage.Subject = "TEST"
mailMessage.Body = "TEST"
Dim smtpClient As New SmtpClient(My.Settings.SMTP, 25)
smtpClient.Send(mailMessage)
Catch ex As Exception
Me.lblAlertText.Text = "An error send email has occured. Contact your system administrator."
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "LaunchAlertPopup", "$(function() { AlertMessage(); });", True)
End Try
I have downloaded an SMTP test tool and send emails just fine through it, so it has to be an issue with this website in IIS.
Many ISPs block port 25. Try sending through 465 or 587. It would also be worth checking for any firewall rules that could be blocking any of these ports.
We can only assume that you're using a valid host name for your SMTP client. This needs to exist or your messages will never send.

Error on sending mail programmatically

I am trying to send an e-mail from a simple console application using my company's mail server:
MailMessage mm = new MailMessage("myemail#mycompany.com", "myemail#mycompany.com", "test", "testbody");
SmtpClient client = new SmtpClient("mail.mycompany.com", 465);
client.Credentials = new NetworkCredential("myemail#mycompany.com", "myPassword");
client.EnableSsl = true;
client.Send(mm);
Console.WriteLine("email sent");
Console.ReadKey();
I am getting the following error: "Failure sending mail.", innerException is "Unable to read data from the transport connection: net_io_connectionclosed."
I have tried connecting to the mail server on that specific port via telnet, which was working fine. My login credentials were also tested on the webmail client and I was authenticated correctly. I have checked my firewall settings, and let Visual Studio through the firewall.
Any ideas what could cause the problem?
Regards,
Adam
Try setting the ssl to false and setting the port number to 25.
Also double check wether the smtp server is open, because then you won't need the login authentication parameters.

System.Net.Sockets.SocketException on 2008R2 Server

I have a program that I want to run on a server which is able to monitor various servers on a network and send out email notifications when certain situations involving those servers occur.
Currently, I am able to run this program on my local machine, and have it fire off emails (using gmail SMTP). However, when I try to run it on the server via the command prompt, it throws the following exception:
Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond
xxx.xxx.xxx.xxx:587
The code related to the sending of the email is fairly simple:
static void sendEmail(MailAddressCollection mailCollection, string emailSender,
string emailDisplayName, string emailPassword,
string subject, string body) {
MailAddress fromAddress = new MailAddress(emailSender, emailDisplayName);
SmtpClient smtpClient = new SmtpClient {
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, emailPassword)
};
MailMessage message = new MailMessage {
From = fromAddress,
Subject = subject,
Body = body
};
foreach (MailAddress mailAddress in mailCollection) {
message.To.Add(mailAddress);
}
smtpClient.Send(message);
}
The server in question is a 2008R2 box, and it does not have Windows Firewall enabled. Also, port 25 is definitely open, as this server can send other types of emails (specifically, those generated by a Dynamics CRM email router).
What could be causing this error?
The issue was related to Gmail blocking the IP address of the server attempting to send email.
When attempting to send emails using a Gmail account, first attempt logging into Gmail manually on that machine specific at that specific IP address, using a browser. I ultimately had to do this and go through the process of authenticating that the email send attempts from my server (being generated programmatically) were legitimate. If you do not go through this process like I did, Gmail can assume that your server is not a legitimate email sender and can continue to assume all traffic from your IP address is invalid--until you formally validate that IP address as described above.

Categories

Resources