Sending email with C# without SMTP Server?

jma*_*erx 3 c# asp.net email

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);
Run Code Online (Sandbox Code Playgroud)

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

Nit*_*wal 5

不建议直接从您的代码向接收邮件服务器发送电子邮件,就接收邮件服务器而言,这就像运行您自己的邮件服务器一样。正确运行邮件服务器要确保可靠地发送电子邮件,需要付出很多努力。例如,其中一件事情(非常重要)是拥有正确的反向DNS记录(披露:我工作的公司的文档链接)。

相反,您应该通过真实的电子邮件服务器中继电子邮件。您可以使用任何已有的电子邮件地址(包括gmail)的SMTP服务器。

使用SMTPClientSMTP AuthenticationSSL(如果支持的话)。

代码示例:

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);
}
Run Code Online (Sandbox Code Playgroud)


sha*_*p00 2

作为替代方案,在您的配置文件中,您可以放置

<configuration>
  <system.net>
     <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
      </smtp>
     </mailSettings>
  </system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这将导致所有已发送的邮件发送到指定的 PickupDirectory 中的磁盘,而不必配置 SMTP 设置。