C# SMTP 邮件不工作?

标签 c# email smtp

   private void SendMail()
    {
        clGast.SelectById(clReservering.Gastid);
        System.Net.Mail.MailMessage mailtje = new System.Net.Mail.MailMessage("norepy@robocamp.nl", clGast.pemail);
        mailtje.Subject = "Bevestiging reservering Robocamp";
        mailtje.Body = "Geachte meneer/mevrouw " + clGast.ptussenvoegsel + " " + clGast.pachternaam + ",\n\n";
        mailtje.Body += "Hierbij de bevestiging van u reservering bij robocamp. Hieronder staan de gegevens nogmaals vermeld : \n\n";
        mailtje.Body += "Van datum: " + clReservering.Datumstart + " \n";
        mailtje.Body += "Tot datum: " + clReservering.Datumeind + " \n";
        mailtje.Body += "Plaats nummer: " + clReservering.Plaatsid + " \n\n";

        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "smtp.google.com";

        client.Send(mailtje); 
    }

为什么这不起作用?

无法发送邮件。 Smtp 失败?

clGast.pemail 是一个电子邮件地址。活跃和工作。

错误是:

SmtpException 未处理

发送邮件失败

有人吗?

最佳答案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

/// <summary>
/// Simple Emailer class for sending a simple email.
/// </summary>
public class Emailer
{
    /// <summary>
    /// Takes a users email and item name and generates an email
    /// </summary>
    /// <param name="recipient">Recipients e-mail address</param>
    public static void SendMail(string recipient)
    {
        try
        {
            // Setup mail message
            MailMessage msg = new MailMessage();
            msg.Subject = "Email Subject";
            msg.Body = "Email Body";
            msg.From = new MailAddress("FROM Email Address");
            msg.To.Add(recipient);
            msg.IsBodyHtml = false; // Can be true or false

            // Setup SMTP client and send message
            using (SmtpClient smtpClient = new SmtpClient())
            {
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.EnableSsl = true;
                smtpClient.Port = 587; // Gmail uses port 587
                smtpClient.UseDefaultCredentials = false; // Must be set BEFORE Credentials below...
                smtpClient.Credentials = new NetworkCredential("Gmail username", "Gmail Password");
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Send(msg);
            }
        }
        catch (SmtpFailedRecipientsException sfrEx)
        {
            // TODO: Handle exception
            // When email could not be delivered to all receipients.
            throw sfrEx;
        }
        catch (SmtpException sEx)
        {
            // TODO: Handle exception
            // When SMTP Client cannot complete Send operation.
            throw sEx;
        }
        catch (Exception ex)
        {
            // TODO: Handle exception
            // Any exception that may occur during the send process.
            throw ex;
        }
    }
}

关于C# SMTP 邮件不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873528/

相关文章:

html - iphone6 上的电子邮件应用程序测试

c# - 通过 C# 中的循环发送电子邮件失败(使用 .net smtp)

c# - 仅在多个空格上拆分 C#

c# - GraphDiff 中多对一和多对多关系中的空行插入

c# - 使用 Func<> 参数将委托(delegate)对象传递给方法

c# - 在电子邮件中编码为 UTF-8

html - 电子邮件后备 Outlook Gmail

java - Java 应用程序接收传入电子邮件的最简单方法是什么?

google-app-engine - 如何从 Google App Engine 向随机的非应用管理员发件人发送电子邮件?

c# - 在下拉列表上绑定(bind)数据