简体   繁体   English

使用 SMTP 发送电子邮件不适用于 asp.net

[英]Email sending using SMTP not working with asp.net

I'm trying to send email using SMTP in asp.net.我正在尝试在 asp.net 中使用 SMTP 发送电子邮件。 Following code is not working.以下代码不起作用。

public string send_email(string name, string email, string message)
{
    var fromAddress = email;
    var toAddress = "ashish_sharma307@hotmail.com";
    string subject = "Feedback";
    string body = "From: " + name+ "\n";
    body += "Email: " + email + "\n";
    body += "Subject: " + subject + "\n";
    body += "Question: \n" + message + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.live.com";
        smtp.Port = 587;
    }
    smtp.Send(fromAddress, toAddress, subject, body);


    return "Your query has been submitted.";
}

This is not working as per expected.这没有按预期工作。 I'm trying to send an email from feedback form.我正在尝试通过反馈表发送电子邮件。

Putting this in an answer so it's easier to read.将此放在答案中,以便更容易阅读。

In answer to your comments, here is the mail message i would send through a server with no credentials.为了回答您的评论,这是我将通过没有凭据的服务器发送的邮件消息。

You can send emails from any address to anyone.您可以从任何地址向任何人发送电子邮件。

But, remember that the emails getting to where it's going is not guaranteed.但是,请记住,无法保证电子邮件会到达目的地。 mail filters will often reject emails that come from an smtp server that has not been added to the SPF record for that domain.邮件过滤器通常会拒绝来自尚未添加到该域的 SPF 记录的 smtp 服务器的电子邮件。

MailMessage m = new MailMessage();

m.From = new MailAddress("Smith@MyDomain.com");
m.To.Add(new MailAddress("someone@TheirCompany.com));
m.Subject = "Message from Smith";
m.Body = "Hello, Test Message";
SendEmail(m);

var smtp = new SmtpClient
{
Host = "localhost",
Port = 25,
UseDefaultCredentials = false,                    
};

smtp.Send(m);

This is for sending through the local machine assuming you have setup an SMTP server.假设您已经设置了 SMTP 服务器,这是通过本地计算机发送的。

There are so many variables and options for sending emails, and each one has it's own limitations.发送电子邮件的变量和选项太多了,每个变量和选项都有自己的局限性。

Options选项

  1. Setup and run your own SMTP server设置并运行您自己的 SMTP 服务器
  2. Use a 3rd party to send emails such as Campain Monitor - There are other providers使用第 3 方发送电子邮件,例如Campain Monitor - 还有其他提供商
  3. If you are sending from your company domain only, and they have Exchange email, use that server如果您仅从您的公司域发送邮件,并且他们有 Exchange 电子邮件,请使用该服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM