C# - SMTP - GoDaddy - Send Email - c#

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!

Related

Cannot get IIS pickup directory on Windows Server 2016 / IIS 10

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.

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

Send email with out using network credentials

I want to send email using the SMTP client in C#. Currently I am using this code:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("receiver");
message.Subject = "";
message.From = new System.Net.Mail.MailAddress("sender");
message.Body = "This is a test mail";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(message);
But I want to send mail with out using username and password by just providing the sender address. Is this possible in C#?
Is this possible with any SMTP server(Not google). I mean login to server anonymously and just provide your mail address.
No. Gmail does not allow anonymous senders. Thankfully.
If it was possible anyone could pretend to send emails from everyone else, rendering gmail as one big spam engine.
It depends on your server. In your code you are using Gmail and Gmail requires this.
You can set up your own SMTP server.
The SMTP server in IIS allows this per default if your application is on the same server as IIS.
In the end you'll have to send credentials if the server demands them.
You could send the credentials of the current security context if that's what you're looking for. See: http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultnetworkcredentials.aspx.

C# - Send e-mail without having to login to server

I have an application that needs to send e-mails. Currently, this is what I am using:
System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage();
MyMailMessage.From = new System.Net.Mail.MailAddress(fromemail.Text);
MyMailMessage.To.Add(toemail.Text);
MyMailMessage.Subject = subject.Text;
MyMailMessage.Body = body.Text;
System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
SMTPServer.Port = 587;
SMTPServer.Credentials = new System.Net.NetworkCredential("email", "password");
SMTPServer.EnableSsl = true;
SMTPServer.Send(MyMailMessage);
Is there a simple way to send an e-mail without having to login to a server? Thank you.
GMail's SMTP server always requires authentication. You may need to setup your own server to send email without authentication.
Configure an SMTP server into your local network (behind a firewall to avoid being a spam source) and use it directly. You can create one in IIS.
There are 2 ways to achieve this:
1) Use your local smtp server (e.g. one with IIS on Win2003/2008 server) and write messages to the local pickup queue). This is possible with minimal changes.
2) You need to resolve the target smtp server. For example when you want to send an email to somebody at msn.com, you'll need to get the MX record for msn.com, e.g. something like mx1.msn.com. You can then directly connect to this SMTP server and send your email to the (local) recipient. Note that there are no built-in ways to resolve the MX-host in .NET (in the sense there are no methods on the Dns class to accomplish this) - you need to do it "manually". Also most SMTP hosts will reject connections from home/residential IP addresses.
You need an SMTP server that does not require authentication, however to stop it being a SPAM server, it needs some other kind of protection like a firewall.

Categories