Not able to connect to smtp from Azure Cloud Service - c#

we are having 2 cloud services hosted on Azure.
Both the services depend on our smtp server for sending mails.
Problem is azure cloud service not able to connect to our smtp server.
we are able to use same code on internal machines without any issue. also we had checked that 25 port is open and IP address are also not on blacklist.
Below is the error while connecting from cloud service :
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 111.93.111.42:25
Email sending logic
MailMessage message = new MailMessage(senderID, reminder.UserName, template.Subject, body);
message.From = new MailAddress(data.SenderEmail, data.SenderName);
message.IsBodyHtml = true;
try
{
SmtpClient smtp = new SmtpClient
{
Host = data.SMTPServer, // smtp server address here...
Port = data.PortNo,
EnableSsl = data.SSL,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
smtp.Send(message);
//Thread th = new Thread(() => { smtp.Send(message); });
//th.Start();
}
catch (Exception ex)
{
ErrorLogging.ErrorLog(ex, "Error Reminders send Mail - Employee Reminders Mail Error Message : " + ex.Message, "Employee Reminders Mail", "0", "EmployeeRemindersMail", schemaName, companyId);
}

Microsoft recommends that Azure customers employ authenticated SMTP relay services (typically connected via TCP port 587 or 443, but often support other ports too) to send e-mail from Azure VMs or from Azure App Services. These services specialize in sender reputation to minimize the possibility 3rd party e-mail providers will reject the message. Such SMTP relay services include but are not limited to SendGrid. It is also possible you have a secure SMTP relay service running on premises that can be used.
Use of these e-mail delivery services is in no way restricted in Azure regardless of subscription type.
Reference: https://blogs.msdn.microsoft.com/mast/2017/11/15/enhanced-azure-security-for-sending-emails-november-2017-update/
You may also want to refer this thread which addresses similar issue and see if that helps.

Please note - from the article linked to above - Enterprise Azure clients CAN send SMTP messages direct from Azure:
"For Enterprise Agreement Azure users, there's no change in the technical ability to send email without using an authenticated relay. Both new and existing Enterprise Agreement users can try outbound email delivery from Azure VMs directly to external email providers without any restrictions from the Azure platform. Although it's not guaranteed that email providers will accept incoming email from any given user, delivery attempts won't be blocked by the Azure platform for VMs within Enterprise Agreement subscriptions. You'll have to work directly with email providers to fix any message delivery or SPAM filtering issues that involve specific providers."

Related

How to send an email from office365 account using domain windows authentication

I'm developing a console app which will be executed from a windows service that needs to send emails using the domain account associated to the domain account running the windows service.
In my development machine I'm logged with a domain account that belongs to the same domain that will run the windows service but I'm not able to get it working properly.
For this development I'm using .NET 4.6.1 and the nuget package FluentEmail.Smtp
My code looks like this:
Email.DefaultSender = new SmtpSender(new SmtpClient
{
UseDefaultCredentials = true,
EnableSsl = true,
Host = "smtp.office365.com",
TargetName = "STARTTLS/smtp.office365.com",
Port = 587,
DeliveryMethod = SmtpDeliveryMethod.Network
});
await Email.From("myname#mycompanydomain.com", "Some tittle")
.To(emailListObject)
.Subject("Some subject")
.Body("Some body", true)
.SendAsync();
With this code I'm getting the following exception:
Unable to connect to the remote server
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 52.96.9.178:587
at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)
at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
Note: From my machine I'm able to ping the IP mentioned in the exception
I'll appreciate your assistance
For starters you might want to take a look at the official document - direct send. You will note that there are quite a few issues (such as TLS security) alongside proper configuration of your Exchange server.
Without more information in your question it is rather limited as to what can be answered, however as an alternate solution, maybe take a look at direct send. (which is much less effort).
Settings for direct send
Enter the following settings on the device or in the application directly.
Server/smart host. Use your MX endpoint, for example, contoso-com.mail.protection.outlook.com
Port. Use port 25
TLS/StartTLS. Enable this.
Email address. Use any email address for one of your Office 365 accepted domains. This email address does not need to have a mailbox.
Microsoft recommends adding an SPF record to avoid having messages flagged as spam. If you are sending from a static IP address, add it to your SPF record in your domain registrar's DNS settings as follows:
DNS entry Value
SPF v=spf1 ip4:<Static IP Address> include:spf.protection.outlook.com ~all

creating smtp server and read the emails c#

I am given a task to create a new smtp mail server which can receive mail using C#.
While going through the articles i read we can send emails via SMTP but we have to receive or read using POP.
I was directed to links by some stackoverflow already existing questions:
Rnwood and sourceforge
Rnwood I am sorry but i did not understand how to use it.
source forge the msi asked to download if we run it, it asks to download framework 1.1.4322 which will not install in my system and throw error.
Usually there are codes for sending messages so I tried msdn example
I used localhost as the server and 587 as the port.
which gives me error (for any port 587,25)
I also found an article here which actually monitors the localhost and specified port when I try to run the msdn code.
But still I am unable to send email to test in any way.
So is there any way I can code to set up smtp in my own server and receive email and test.
Setting up and configuring a mail server is a completely different ball game than just sending or reading emails from an existing IMAP / POP3 server.
A mail server consists of a number of components such as:
A Mail Transfer Agent (MTA) that handles SMTP traffic and which is responsible for sending email from your users to an external MTA and to receive email from an external MTA.
Mail Delivery Agent which retrieves mail from the MTA and places it in the recipient's mailbox.
A domain name with appropriate DNS records and an SSL certificate.
A server that provides IMAP / POP3 functionality.
In short... stick to publicly available mail servers...
In your post you referenced the SmtpClient from the .NET framework. That library is used to connect to an existing mail server. You can use it like this.
MailMessage message = new MailMessage();
message.From = new MailAddress("your.email.address#example.com", "Your name");
MailAddress recipientsMailAddress = new MailAddress("the.recipients.email#example.com");
message.To.Add(recipientsMailAddress);
message.Subject = "The subject of your email";
message.Body = "The body / content of your email";
message.IsBodyHtml = false; // You can set this to true if the body of your email contains HTML
SmtpClient smtpClient = new SmtpClient
{
Credentials = new NetworkCredential("Your username/email", "Your password"),
EnableSsl = true, // Will be required by most mail servers
Host = "The host name of the mail server", //
Port = 465 // The port number of the mail server
};
smtpClient.Send(message);
If you have a Gmail account, you can use their SMTP server in your C# application, simply use these settings and it should all work.
Hostname: smtp.gmail.com
Port: 587
Username: your_email#gmail.com
Password: ********
RequireSSL: true
Have a look at SmtpListener, I think it does what you want.
It isn't a standard email server which will receive new emails throught SMTP, store them on disk and allow you to retrieve them using POP.
SmtpListener will create a SMTP server that will receive email and allow you to react to any new email through code.
However, please note that you will have to configure it in your production environment like a real SMTP server, including MX DNS entries.

How to send email from a secure network with closed SMTP port

I tried to Send email from a Desktop app using SMTP sever but my network is secure and port is closed.
So, is there another way to send email like using Gmail api ?!
I use this code but doesn't work with me
public void Send_Mail(string HTMLBody, string MailTo)
{
MailMessage Mail = new MailMessage();
SmtpClient SmtpClient = new SmtpClient();
string MailSubject = "Subject;
string MailFrom = "from#xxxx.com";
Mail.Subject = MailSubject;
Mail.Body = HTMLBody;
Mail.To.Add(MailTo);
MailAddress From = new MailAddress(MailFrom);
Mail.From = From;
Mail.IsBodyHtml = true;
SmtpClient.Host = "host";
SmtpClient.Port = port;
SmtpClient.EnableSsl = true;
SmtpClient.Send(Mail);
}
If your network doesn't allow outbound connections to whatever port gmail uses (or restricts a particular protocol, or IP, etc), then there's nothing you can do. You would have to talk to the "network guys" to either remove this restriction for you or better yet, ask them to provide the local smtp server for you to use.
I've worked in a place where we had a similar problem. Desktop machines were not allowed to send emails, but servers could be permissioned to talk to an SMTP server.
What we ended up doing was writing a windows service that listened for messages placed on a queue (Tibco EMS in our case, but MSMQ would also do). The service took the messages from the queue and passed them onto the SMTP server is was permissioned to use.
It added an extra step, and process, to the system, but was enough to satisfy the compliance department.
Normally a "secure network" means that there is a firewall in place that restricts the traffic and only allows communication on certain ports like port 80 and maybe 8080.
Such networks (workplaces, shared office spaces, schools, eg.) usually have an outgoing SMTP server you could use. Alternatively you will need to use a server that can be contacted through the port(s) that are actually open or relay/tunneling the request through a third party.

Unable to send mail from ASP.NET web page

I have a Contact Us page in my website from which I am trying to send mail.Here is my code
MailMessage feedBack = new MailMessage();
feedBack.To.Add("some#some.com");
feedBack.From = new MailAddress("MyClient#some.com");
feedBack.Subject = "Mail from My Client's Website.";
feedBack.Body = "Sender Name: " + Name.Text + "<br/><br/>Sender Last Name:"+LastName.Text+"<br/><br/>Sender Company:"+Company.Text+"<br/><br/>Sender Designation:"+Designation.Text+"<br/><br/>Sender Email:"+Email.Text+"<br/><br/>Sender Phone No:"+ PhoneNo.Text+"<br/><br/>Sender Enquiry:"+Enquiry.Text;
feedBack.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.MyClient.com"; //Or Your SMTP Server Address
//smtp.Port = 25;
//smtp.EnableSsl =false;
smtp.Credentials = new System.Net.NetworkCredential("MyClient#some.com","XXXX");
//Or your Smtp Email ID and Password
smtp.Send(feedBack);
All the time I keep getting this error
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 64.37.118.141:25
I have verified with my client and all the details are correct(smtp server name,credentials).
I also tried setting port to 587 and 465 but it did not work.
Can someone help me out with this?
What can be the cause?
I am not able to find it.
Any suggestions are welcome.
I tested the ip above with port 25 through telnet and got the following response:
Command:
telnet 64.37.118.141 25
Response:
Could not open connection to the host, on port 25: Connect failed
Most likely port 25 is being blocked. You will need to find what port is used with your pop/smtp servers.
MEDIUM TRUST
If you are on a shared hosting service, chances are that ASP.Net is set to run in medium-trust (as it should). This restricts SMTP to port 25 only, you cannot use any other port.
SMTP problems when ASP.NET is not running in full-trust
SO similar post
Have you checked that the server that the web application resides on is set up to be allowed to send mail through the SMTP server? Trust connections sometimes have to be setup to allow relaying off one server throught another.

keeping mail from going into the sent folder when using Exhange Web Service to send

We are using Exchange Web Services to send mail in out WCF application, here is a little code snippet.
//using ExchangeWebServices;
var email = new MessageType();
email.IsFromMe = false;
email.From = new SingleRecipientType();
email.From.Item = new EmailAddressType();
email.From.Item.EmailAddress = message.From;
email.ToRecipients = message.To.Select(to => new EmailAddressType { EmailAddress = to }).ToArray();
It works fine but it's filling up the sent mail folder in for the "appserver" user who sends the mail. Is this something we can configure in the app to "not copy it to the sent folder" or does this need to be done by an administrator for the exchange serer?
The reason I ask is cause the admin is a third party consultant so if it could be done without bothering them that would be great.
Thanks! Happy Holidays!
Not sure if this is an option or not, but if you can use SmtpClient rather than Exchange Web Services, you can send email without a copy going to a 'sent mail' folder. Obviously you have access to an Exchange server, so you'd just need to have Exchange's SMTP server configured such that your application server can relay through it. Otherwise you could setup a new SMTP server using the SMTP functionality included in IIS:
SmtpClient
Configuring SMTP in IIS 7

Categories