Cannot get IIS pickup directory on Windows Server 2016 / IIS 10 - c#

I am trying to send an email using C# on a Windows 2016 server, running IIS 10.
I installed IIS SMTP and it can send out messages using both Telnet and the windows pickup service.
Now I am using a C# webservice to send an email out.
At first I tried to use SmtpDeliveryMethod.Network like this:
MailMessage mail = new MailMessage(mailFrom, mailTo);
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "localhost";
however that resulted in this error:
Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25
So as the pickup service of the IIS SMTP (on C:\inetpub\mailroot\Pickup) is working I thought it would be best to just start using the pickup service.
Using this code:
MailMessage mail = new MailMessage(mailFrom, mailTo);
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
However this results in a Cannot get IIS pickup directory error.
So in IIS Services manager under SMTP I check the bullet at
"Store e-mail in pickup directory:"
And added C:\inetpub\mailroot\Pickup in the text field.
This didn't work however.
So I also changed the web.config and added these lines:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\inetpub\mailroot\Pickup" />
</smtp>
</mailSettings>
</system.net>
This however didn't do the trick either.
This will result in the following error:
Failure sending mail. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\mailroot\Pickup\bb3fc5af-e213-43d3-af47-cb2836de78c3.eml'.
I still get the same error and don't know how to fix this.
Could you please help out?
Best regards

I couldn't get it to work.
Eventually I just brought everything back to it origin and started over.
It works now, but what it is exactly I don't know for sure.

Related

ASP.NET MVC web application unable to send email via SMTP on IIS server hosted by Arvixe

I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?
I am developing a web-based sales-tracking application in Visual Studio 2015 (ASP.NET MVC). The site will be hosted on an Arvixe BusinessClass for Windows shared server. The domain of the server is mydomain.com, however this domain is actually hosted by one.com, which also provides mydomain.com email.
One of the functions of the site is to inform the line manager when a user reports a sale. The line manager is to be informed via email. The email account I am trying to send from is provided by one.com.
Using System.Net.Mail.MailMessage and sending via SmtpClient, trhe following code works in my development environment (Windows 10 pro) but not deployed (deployed on Arvixe BusinessClass for Windows shared server environment):
MailMessage message = new MailMessage();
message.To.Add("recipient#mydomain.com");
message.From = new MailAddress("sender#mydomain.com", "sender#mydomain.com");
message.IsBodyHtml = true;
message.Subject = "Subject";
message.Body = "Body<br />";
SmtpClient client = new SmtpClient();
client.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "send.one.com";
client.Port = 587;
client.UseSSL = true;
client.Credentials = new NetworkCredential("sender#mydomain.com", "***");
client.Send(message);
On my Arvixe server I get the error:
"Unable to read data from the transport connection: net_io_connectionclosed".
Using this code I have also tried ports 465 and 25, with SSL set to true and false, and also host "mailout.one.com", without success.
I gave up and tried using MailKit with the following code:
MimeMessage message = new MimeMessage();
message.To.Add(new MailboxAddress("recipient#mydomain.com"));
message.From.Add(new MailboxAddress("sender#mydomain.com", "sender#mydomain.com"));
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "htmlbody<br />";
bodyBuilder.TextBody = "textbody";
message.Body = bodyBuilder.ToMessageBody();
message.Subject = "Subject";
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("send.one.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
client.Authenticate("sender#mydomain.com", "***");
client.Send(message);
client.Disconnect(true);
}
Again it works in my development environment, but not in production. Here I get the error:
"The operation is not allowed on non-connected sockets".
Of note, both solutions work in deployed environment if I change the host to 127.0.0.1 and add an example#mydomain.com email account and connected user account in Arvixe, but only for external email addresses. Addresses ending in mydomain.com obviously get rerouted back to the server itself, as the server's domain is mydomain.com, but as the email provider is one.com and not the Arvixe server, the email doesn't get through.
Other things I have tried without success:
-Setting the SPF record for mydomain.com to include my server address, and my server IP address.
-Using an Office365 account on a different domain to send from (host "smtp.office365.com")
I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?
Arvixe/One.com suggestions (from support tickets and live chat):
SMTP Authentication must be used.
The local mail server, or localhost must be used to send email via web script. (try xxx.win.arvixe.com, localhost, or 127.0.0.1 - port 25 or 26, or xxxsecuremail.win.arvixe.com - port 465 for secure mail)
The FROM email address, and SMTP Authentication email address, must be a local user on the local server.
If you have your own remote email server and intend to send emails via web script to email addresses that are local to your domain, then that mail domain needs to be removed from the local server (we have to do this) and you will have to use an account from another domain local to the server to send mail from. Otherwise any email going to the local domain will be delivered locally and not to your remote server.

Sending mail using external SMTP server uses IP not defined in Azure Web Apps' Outgoing IPs

I have an Azure Web App. When I'm attempting to send an email using my external SMTP server, the source IP from Azure is not one of the ones listed in the Web App's "Outgoing IPs". I need to reliably know what the source IPs may be so I can whitelist it on my SMTP server. Does anyone know why this may be?
If it helps, here's some test code.
private void SendTestEmail()
{
SmtpClient client = new SmtpClient();
MailMessage mail = new MailMessage();
mail.To.Add(RECIPIENT_EMAIL);
mail.Subject = "TEST subject";
mail.Body = "This is a test<BR><BR><BR>";
mail.IsBodyHtml = true;
mail.From = new MailAddress("myemail#mydomain.com", "Testing");
mail.Body += "<div class=eventBody>This is a test</div><BR><BR>";
client.Send(mail);
}
The SMTP server address is defined in the web.config and works from known IPs:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<mailSettings>
<smtp>
<network host="mysmtpservernamegoeshere" />
</smtp>
</mailSettings>
</system.net>
</configuration>
When you set up a SMTP client, I am assuming you know the smtp domain, all you have to do is a reverse lookup on that. For example if my smtp host is smtp.office365.com and I do a reverse ip lookup it will give me the ip address of 132.245.229.178, but you should be able to just forward the port for the smtp in your firewall.
Maybe it wasn't there back when this question was asked, but it might help someone now:
In the Azure portal go to you App Service and open the Resource Explorer.
You will see some JSON data about your App service. One property is the one you want to take a look at: possibleOutboundIpAddresses
This is a comma separated list of IP addresses that are possible values for the outgoing IP of your app service. If you add that list to your SMTP server, you should be save.

Sending email with C# without SMTP Server? [duplicate]

This question already has answers here:
Sending mail without installing an SMTP server
(3 answers)
Closed 9 years ago.
I am making a simple website. It is hosted on my VPS to which I run IIS 7 and have full access to. DNS is setup and configured but no mail servers or anything are configured.
I want users to be able to send feedback through a very simple form.
I however do not have an SMTP server (that I am aware of).
string from = "";
string to = "someemails#hotmail.com";
string subject = "Hi!";
string body = "How are you?";
SmtpMail.SmtpServer = "mail.example.com";
SmtpMail.Send(from, to, subject, body);
I want to send the messages to a free email account but I'm not sure how since I do not have an SMTP server.
Is there some other way I can do it? Or some alternative (like using a free smpt or something)
Thanks
Sending email directly from your code to the receiving mail server isn't recommended and is like running your own mail server as far as the receiving mail server is concerned. A lot goes into running a mail server properly to ensure reliably delivered email. As an example, one of those things (very important) is having correct reverse dns records (disclosure: documentation link at company I work for).
Instead, you should relay your email through a real email server. You can use the SMTP server of any email address you already have, including gmail.
Use SMTPClient with SMTP Authentication and SSL (if supported).
Code Example:
using System.Net;
using System.Net.Mail;
string fromEmail = "FromYou#gmail.com";
MailMessage mailMessage = new MailMessage(fromEmail, "ToAnyone#example.com", "Subject", "Body");
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, "password");
try {
smtpClient.Send(mailMessage);
}
catch (Exception ex) {
//Error
//Console.WriteLine(ex.Message);
Response.Write(ex.Message);
}
As an alternative, in your config file, you could put
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
</smtp>
</mailSettings>
</system.net>
</configuration>
This will cause all sent mail to be sent to disk in the specifiedPickupDirectory instead of having to configure the SMTP settings.
It is not possible tosend without a smtp server, but you could either use your emailprovider or a free smtp like turbosmtp http://www.serversmtp.com/en/free-smtp-server

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.

C# - SMTP - GoDaddy - Send Email

What am I doing wrong? Im trying to send a email using c# with GoDaddy webhost.
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net", 465);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("emailGODADDY", "password");
MailMessage message = new MailMessage("emailGODADDY", "otherEmail");
message.Subject = txtSubject.Text;
message.Body = txtContent.Value;
client.Send(message);
With a shared hosting account with Go Daddy you need to send emails on port 25 not port 465. Furthermore, relay-hosting.secureserver.net does not need you to authenticate with a username and password when you are sending from your hosting account.
Each smtp server has own credentials which is not same with other.
According to microsoft client.UseDefaultCredentials should not be used
when possible.
You can try by omitting this line of code.....
client.UseDefaultCredentials = false;
If this will not work then try with
client.EnableSsl = false;
Because some server do not use secure connection.
You can check with this code also
client.DeliveryMethod = SmtpDeliveryMethod.Network;
Just comment out below line than it will work fine
//client.EnableSsl = false;
also use Port 25.
OKAY! I have figured this out.
Wow, I've spent so much time trying to get this up and running. I have Economy Windows Hosting with Plesk (Shared), with a single Office365 email account. I learned the hard way that you can't create a SMTP client that connects to smtp.office365.com, as port 587 is blocked for this shared hosting package. TXT records, SPF records don't help either. Lots of wasted time.
But alas, here's exactly what worked for me. I added the following to my web.config, though I think you can build the same info into your SMTP client object. Still, this works fine. Whatever.
<system.net>
<mailSettings>
<smtp from="noreply#MyDomain.com">
<network host="relay-hosting.secureserver.net" port="25" />
</smtp>
</mailSettings>
</system.net>
In my code behind, I made sure that the FROM address used in the MailMessage exactly matched the FROM value within the web.config. Does this really matter? Not sure, but they match, and it works. Whatever.
The FROM address (noreply#) does NOT exist as an email, nor is it an alias, etc. It's just something from the same domain where the website is hosted.
My TO address is retrieved from within the web.config (AppSettings["SendTo"]). This is my real email address that lives on this domain (my Office365 email address). I'm not sure if you can send an email to something outside the domain, as I haven't tested.
... obviously the MailMessage (msg) is not complete ...
msg.To.Add(new MailAddress(ConfigurationManager.AppSettings["SendTo"].ToString()));
msg.From = new MailAddress("noreply#MyDomain.com");
var smtp = new SmtpClient
{
// nothing is needed here
};
smtp.Send(msg);
Create your client and send the message!
YAY! I had to list the noreply# email as not spam, but now it's arriving as expected. Remember, you only have a limited number of relay emails per day, so use them wisely! Hope this helps!

Categories