如何从 Smtp 客户端发送邮件?

How to send mail from Smtp client?

我有一个控制台应用程序,我正在尝试从中发送邮件。

我的代码。

    MailMessage message = new MailMessage(MailSender, "ToMe@me.com");

    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "smtp.google.com";

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        string t = ex.Message;
    }

here

得到这个

我一定是遗漏了什么,因为我得到了:

Failure sending mail.

我做错了什么?

编辑: 内部异常。

InnerException = {"The remote name could not be resolved: 'smtp.google.com'"}

你可以尝试使用

smtp.gmail.com

而不是

smtp.google.com

同时尝试确保您提供了正确的凭据和正确的端口。服务器参数信息:

Source

所以你可以尝试这样的事情

MailMessage message = new System.Net.Mail.MailMessage(); 
string fromEmail = "youremailaddress@xyz.com";
string password = "yourPassword";
string toEmail = "recipientemailaddress@abc.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Using the new SMTP client.";
message.Body = "Using this new feature, you can send an e-mail message from an application very easily.";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
    smtpClient.EnableSsl = true;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new NetworkCredential(fromEmail, password);

    smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);   
}

@Ra3IDeN ...嘿兄弟试试这个...

 SmtpClient smtpClient = new SmtpClient("mail.yourwebsitename.com", 25);

smtpClient.Credentials = new System.Net.NetworkCredential("demo@yourwebsitename.com.com", "yourIdPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();

//code for: From ,CC  & To
mail.From = new MailAddress("demo@yourwebsitename.com", "yourwebsite");
mail.To.Add(new MailAddress("demo@yourwebsitename.com"));
mail.CC.Add(new MailAddress("youremailid@gmail.com"));

 smtpClient.Send(mail);
 MailMessage msg = new MailMessage("YourEmail@gmail.com", "DestinationEmail@something.com");


        msg.Subject = message.Subject;

        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Message Content here as HTML", null, MediaTypeNames.Text.Html));

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32( 587));
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("YourEmail@gmail.com", "YourPassword");
        smtpClient.EnableSsl = true;
        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
                    System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        };


        smtpClient.Credentials = credentials;
smtpClient.Send(msg);

如果您尝试从控制台应用程序发送电子邮件(您的更高级别的问题),我建议使用 PostMark。为什么:

  • NuGet - 您可以获取 PostMark NuGet 程序包并使用漂亮的 API 发送电子邮件。方便又简单。
  • 未标记为垃圾邮件 - 您可以配置您的 "server" 进行验证(包括 spf​​ 和签名)。因此,您的电子邮件更有可能到达收件箱中的目的地,而不是垃圾邮件箱中。
  • 免费 - 到一个点。我认为这是 1000 封电子邮件免费,然后每 1000 封 1 美元。这很好。将其与任何其他出租的香草 SMTP 服务器进行比较。 PostMark 便宜
  • 一致 - 从工作站开发到服务器上线,PostMark API 始终可访问。我不能强调那有多好。服务器主机通常会提供 SMTP 服务器端点,但它只能在其网络内部工作,这意味着当您在工作站上进行 DEV 工作时必须配置另一个 SMTP 服务器(否则它根本不会工作)。
  • 异步接口 - 我不确定.Net 中的内置 smtp 客户端是否是异步的...
  • Tracking - 嘿,看看那个,他们有一个内置的跟踪功能。这很时髦。

发送示例代码 (source):

var message = new PostmarkMessage()
{
    To = "recipient@example.com",
    From = "sender@example.com",
    TrackOpens = true,
    Subject = "A complex email",
    TextBody = "Plain Text Body",
    HtmlBody = "<html><body><img src=\"cid:embed_name.jpg\"/></body></html>",
    Tag = "business-message",
    Headers = new HeaderCollection{
        {"X-CUSTOM-HEADER", "Header content"}
    }
};

var imageContent = File.ReadAllBytes("test.jpg");
message.AddAttachment(imageContent, "test.jpg", "image/jpg", "cid:embed_name.jpg");

var client = new PostmarkClient("server token");
var sendResult = await client.SendMessageAsync(message);