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

时间:2016-03-29 12:31:56

标签: c# smtp smtpclient

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

我的代码。

    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

自从我得到以后,我必须遗漏一些东西:

  

发送邮件失败。

我在这里做错了什么?

编辑: 内在的执行。

  

InnerException = {"无法解析远程名称:   ' smtp.google.com'"}

4 个答案:

答案 0 :(得分:2)

您可以尝试使用

smtp.gmail.com

而不是

smtp.google.com

还要尝试确保您提供正确的凭据以及正确的端口。服务器参数信息:

enter image description here

<子> 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);   
}

答案 1 :(得分:1)

@ 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);

答案 2 :(得分:1)

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

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

发送(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);

答案 3 :(得分:0)

 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);