How to send mail using IIS 5.1 in WinXP? - c#

I have this code to send mail:
public bool SendMail(MailMessage message)
{
message.From = new MailAddress(AppProperties.FromMailAddress, AppProperties.FromDisplayName);
SmtpClient smtp = new SmtpClient { EnableSsl = AppProperties.EnableSsl };
try
{
smtp.Send(message);
return true;
}
catch (Exception)
{
return false;
}
}
and have configured web.config to send mail using IIS 5.1 in localhost with this (as suggested by the answers):
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="localhost"
userName=""
password=""
defaultCredentials="false"
port="25" />
</smtp>
</mailSettings>
</system.net>
What do I have to do to send mail with my IIS 5.1 in Windows XP? Is possible to do it? I guess yes, as you say, since I don't get any exception, but I don't receive it on destination. If I should put an user and a password, wich must be?

You should first install SMTP server (Windows Components > IIS > SMTP Service) and then configure it to enable relaying.
IIS > Default SMTP Server > Properties
Access > Authentication
Access Control > Anonymous Access - Checked
Relay Restrictions > Relay > Select - Only the list below > Add > 127.0.0.1

Sure it's possible, you will no longer need to use SSL however. In the config file, your port will probably be 25, you may or may not need username/password, and of course your hostname will change.
Also make sure you install the SMTP components along with IIS.

yes you can send it this way :D (but i think you need to use port 25) smtp class is part of .net

Related

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.

SMTP Email error with port 465

I am trying to send email from C#.net and getting following error:-
Unable to read data from the transport connection: net_io_connectionclosed.
Same code is working fine with port: 25, but throwing error when update to port: 465
I have tested the SMTP settings with port 465 in Outlook and it works fine. Only have issue while sending through C#.
I am using following email settings.
<mailSettings>
<smtp from="info#domain.com" deliveryMethod="Network" >
<network host="mail.domain.com" password="xxx" port="465" userName="info#domain.com" enableSsl="true" />
</smtp>
</mailSettings>
System.Net.Mail.SmtpClient does not support Implicit SSL. Using old component System.Web.Mail is working fine.
How can I send emails through SSL SMTP with the .NET Framework?
Use port 25 with SSL enable. It's worked for me.

Password recovery control fails to send mails

I'm having windows vista and I checked that it does not have default virtual smtp in its IIS.
I used the local host with port 25 to send emails for my password recovery control but it is not working.
How can I send the email?
I got it working now. Here is my code:
PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
e.Cancel = true;
PasswordRecovery1.MailDefinition.BodyFileName = "~/password.txt";
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(e.Message);
}
<mailSettings>
<smtp from="email#gmail.com">
<network host="smtp.gmail.com" port="587" userName="email#gmail.com" password="xxxxx"/> </smtp>
</mailSettings>
In password.txt file I wrote "You can return to the website by following login details. In password.txt file I wrote "You can return to the website by following login details.
<br>Username = <%Username%> </br>
<br>Password = <%password%>" </br>
With the scarce info provided ... I can only guess that your development mail server is not set up correctly. If you are sending using localhost then you will need to have smtp running and configured on the local IIS server. Why did you make sure this was off?

Sending Email Problem : Connection refused

Im trying to send email to the user when registering.
But its generating an error "No connection could be made because the target machine actively refused it 127.0.0.1:25".
Currently I have add this to the web.config
<system.net>
<mailSettings>
<smtp>
<network
host="localhost"
port="25"
defaultCredentials="true"
/>
</smtp>
</mailSettings>
</system.net>
As pointed out by Ben Robinson and Ernest Friedman-Hill you need to have SMTP server installed on your local machine. Otherwise you can use any other host.
<system.net>
<mailSettings>
<smtp from="yourMailId#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="yourMailId#gmail.com" password="yourMailPassword" />
</smtp>
</mailSettings>
</system.net>
I used gmail as a host here.
While using this Don't forget to enable the SSL
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
Your application needs to talk to a functioning mail (SMTP) server. Your configuration indicates there's one installed on your local machine, but apparently there is not. Either install and configure one, or change "localhost" to point to a host where one actually exists,.
The error is telling you there is no SMTP server listening on port 25 on the local machine.
This is because your app is looking for an SMTP server on your own machine. You'll need to either install an SMTP server on your machine or use your ISP's SMTP server.

Sending email through a Google Apps account is working locally, but not on my web server

Related:
Send Email via C# through Google Apps account
My question is the same so I implemented Koistya's solution as follows. The heartbreak is that it works beautifully on my dev laptop but when ported to web server it times out with no explanation.
My SMTP config is in my web.config file. I made mods based on Koistya's answer:
<mailSettings>
**Changed**
<smtp from="my#email.com">
<network host="smtp.gmail.com" password="[password]" port="587"
userName="my#email.com"/>
</smtp>
**Original**
<!--<smtp from="my#email.com">
<network host="mail.domain.com" password="[password]" port="25"
userName="my#email.com"/>
</smtp>-->
</mailSettings>
My .Net C# code (before & after removed):
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.EnableSsl = true;
mSmtpClient.Send(message);
As I said this works great on my dev environment but not on web. Can anyone help? Thanks.
Your settings are correct. We use gmail for sending mail all the time in our web apps. Your server is probably blocking outgoing traffic on port 587. I would contact your host and see if they can help otherwise you will need new mail or a new host.
Thanks to everyone's help on this site as well as the Google apps forum (although I like this one better) I finally put together all the pieces of the puzzle. For whatever reason port 465 and port 587 would not work. This is what worked for me:
Web.config:
<smtp from="pwretrieve#mydomain.com">
<network host="smtp.gmail.com" password="[password]" port="25"
userName="pwretrieve#mydomain.com"/>
</smtp>
from the aspx.cs file:
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.EnableSsl = true;
Thanks again!
I had the same problem for my form. My website is running on a Plesk control panel. All I did was to login to my panel and disable email hosting on my web server. After that my form started sending to a Google apps account. Try out what I did, I am sure you will get a difference.
Failing a response from your hosting company and if you have another server you could send test requests to, try requesting connections to other ports and see what happens.
Could be that the smtp client is not able to access the smtp server (might be disabled by the web host).
Ask the web host if they have a specific smtp server you should be using to send emails with.
If your webhost doesn't allow you to send outgoing SMTP mail from their servers, that would cause this problem. For example, GoDaddy only allows you to send outgoing mail through smtpout.secureserver.net from your server, so any attempt to send mail through another host (in this case, smtp.gmail.com) would fail. If your ISP does not block outgoing SMTP (like Qwest does not, for example), then that's why this would work locally.
Check the FAQ with your webhost to see what they have to say about it. Most hosting companies do allow outgoing SMTP, but limit it to a certain number of relays/day, to prevent accidental exploitation for spam forwarding.
You're correct that the MX record on your domain only affects incoming mail. When somebody tries to send mail to you#yourdomain.com, they hand it to an SMTP server (most likely, the one their ISP gives them), and then SMTP server looks up your MX record to see who handles your email. It will resolve to smtp.gmail.com, so that's who will get your mail, and you get it from them. When you send outgoing mail, it can go through anybody, since you only care about the MX record for the destination domain (where the mail will eventually end up).
Does that make sense? If you'd like some clarification, I can find some tutorials and other explanations to help make sense of it.

Categories