Sending email with C# without SMTP Server?

41,960

Solution 1

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.

Solution 2

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 = "[email protected]";
MailMessage mailMessage = new MailMessage(fromEmail, "[email protected]", "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);
}
Share:
41,960
jmasterx
Author by

jmasterx

Hi

Updated on July 28, 2022

Comments

  • jmasterx
    jmasterx almost 2 years

    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 = "[email protected]";
        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

  • Brad Larson
    Brad Larson over 10 years
    You should probably disclose your affiliation to the service you link to in the first paragraph.