Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

email

asp.net

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

like image 498
jmasterx Avatar asked Dec 25 '13 21:12

jmasterx


People also ask

Can we send email using C++?

Send Outlook Emails using C++Create an object of SmtpClient. Set host, username, password, and port number. Set security options. Send email using SmtpClient->Send() method.


2 Answers

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);
}
like image 88
Nitin Agarwal Avatar answered Sep 29 '22 01:09

Nitin Agarwal


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.

like image 27
shamp00 Avatar answered Sep 29 '22 03:09

shamp00