Sending email with C# without SMTP Server?

 Eosven_119 发布于 2023-02-08 10:37

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

1 个回答
  • 不建议直接从您的代码向接收邮件服务器发送电子邮件,就接收邮件服务器而言,这就像运行您自己的邮件服务器一样。正确运行邮件服务器要确保可靠地发送电子邮件,需要付出很多努力。例如,其中一件事情(非常重要)是拥有正确的反向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);
    }
    

    2023-02-08 10:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有