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
Related
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.
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.
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!
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.
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.