0

我尝试使用此代码发送电子邮件..但smtp.Send(mail)发生错误;消息“发送邮件失败”

  MailMessage mail = new MailMessage();
  // set the addresses
  mail.From = new MailAddress("from@gmail.com");

  mail.To.Add(new MailAddress("to@yahoo.com"));

  // set the content
  mail.Subject = "test sample";
  mail.Body = @"thank you";
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");

  smtp.Credentials = new NetworkCredential("from@gmail.com", "password"); 
  smtp.Send(mail);
4

6 回答 6

1

在您的代码中指定端口号:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)

另请查看此线程使用 C# 通过 Gmail SMTP 服务器发送电子邮件

于 2012-05-04T06:58:54.983 回答
0

You should be using the using statement when creating a new MailMessage, plus a few things you missed out like port number and enableSSL

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress("from@gmail.com");
    mail.To.Add(new MailAddress("to@yahoo.com"));
    mail.Subject = "test sample";
    mail.Body = @"thank you";

    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
    smtpServer.Port = 587;
    smtpServer.Credentials = new NetworkCredential("from@gmail.com", "password"); 
    smtpServer.EnableSsl = true;
    smtpServer.Send(mail);
}
于 2012-05-04T07:03:22.980 回答
0

这是我不久前写的一个基本的 GMAIL smtp 电子邮件实现:

public static bool SendGmail(string subject, string content, string[] recipients, string from)
{
    bool success = recipients != null && recipients.Length > 0;

    if (success)
    {
        SmtpClient gmailClient = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = new System.Net.NetworkCredential("******", "*****") //you need to add some valid gmail account credentials to authenticate with gmails SMTP server.
        };


        using (MailMessage gMessage = new MailMessage(from, recipients[0], subject, content))
        {
            for (int i = 1; i < recipients.Length; i++)
                gMessage.To.Add(recipients[i]);

            try
            {
                gmailClient.Send(gMessage);
                success = true;
            }
            catch (Exception) { success = false; }
        }
    }
    return success;
}

它应该适合你,但你需要在我在代码中标记的地方添加一个有效的 gmail 帐户。

于 2012-05-04T07:06:45.570 回答
0

这是我检查发送邮件的功能......它工作正常。

`

        private static bool testsendemail(MailMessage message)
        {

            try

            {

            MailMessage message1 = new MailMessage();

            SmtpClient smtpClient = new SmtpClient();

            string msg = string.Empty;

            MailAddress fromAddress = new MailAddress("FromMail@Test.com");
            message1.From = fromAddress;
            message1.To.Add("ToMail@Test1.com");
            message1.Subject = "This is Test mail";
            message1.IsBodyHtml = true;
            message1.Body ="You can write your body here"+message;
            smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
            smtpClient.Port = 587;
            smtpClient.EnableSsl = false;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new  System.Net.NetworkCredential("SenderMail@yahoo.com", "YourPassword");

            smtpClient.Send(message1);
        }
        catch
        {
            return false;
        }
        return true;

    }`           

谢谢你。

于 2012-05-04T08:53:42.007 回答
0

您需要smtp.EnableSsl = true为 gmail 设置。

看看这个类,它应该对你有用:

public class Email
{
    NetworkCredential credentials;
    MailAddress sender;

    public Email(NetworkCredential credentials, MailAddress sender)
    {
        this.credentials = credentials;
        this.sender = sender;
    }

    public bool EnableSsl
    {
        get { return _EnableSsl; }
        set { _EnableSsl = value; }
    }
    bool _EnableSsl = true;

    public string Host
    {
        get { return _Host; }
        set { _Host = value; }
    }
    string _Host = "smtp.gmail.com";

    public int Port
    {
        get { return _Port; }
        set { _Port = value; }
    }
    int _Port = 587;

    public void Send(MailAddress recipient, string subject, string body, Action<MailMessage> action, params FileInfo[] attachments)
    {
        SmtpClient smtpClient = new SmtpClient();

        // setup up the host, increase the timeout to 5 minutes
        smtpClient.Host = Host;
        smtpClient.Port = Port;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = credentials;
        smtpClient.Timeout = (60 * 5 * 1000);
        smtpClient.EnableSsl = EnableSsl;

        using (var message = new MailMessage(sender, recipient)
        {
            Subject = subject,
            Body = body
        })
        {
            foreach (var file in attachments)
                if (file.Exists)
                    message.Attachments.Add(new Attachment(file.FullName));
            if(null != action)
                action(message);
            smtpClient.Send(message);
        }
    }
}
于 2012-05-04T06:59:30.373 回答
0

填写mail.Host和mail.Port

具有适当值的属性

于 2012-05-04T07:00:51.433 回答