C#SMTP邮箱无法正常工作?

时间:2014-01-01 21:35:16

标签: 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已取消联系

发送邮件失败

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您需要将端口设置为587并启用SSL:

Client.EnableSsl = true;

我通常也会指定用户名和密码。

以下是没有基础设施代码的完整列表:

MailMessage msg = new MailMessage(new MailAddress("test@gmail.com"), new MailAddress("test@gmail.com"));
msg.Subject = "The Subject";
msg.Body = "The Body";
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com"
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("userName", "pa55word");
client.Send(msg);
client.Dispose();