一个C#gmail MailSender

时间:2015-09-24 03:47:43

标签: c# mailmessage

我创建了一个电子邮件发送功能,但它一直在超时。

try
        {
            send.Enabled = false;
            MailMessage message = new MailMessage();
            message.From = new MailAddress(senderemail.Text);
            message.Subject = subject.Text;
            message.Body = body.Text;
            foreach (string str in recipients.Text.Split(';'))
            {
                message.To.Add(str);
            }
            try
            {
                SmtpClient client = new SmtpClient();
                client.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.EnableSsl = true;
                client.Send(message);
                MessageBox.Show("Sent Successfully", "Details");
            }
            catch
            {
                SmtpClient client2 = new SmtpClient();
                client2.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
                client2.Host = "smtp.gmail.com";
                client2.Port = 465;
                client2.EnableSsl = true;
                client2.Send(message);
                MessageBox.Show("Sent Successfully", "Details");
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error");
        }
        finally
        {
            send.Enabled = true;
        }

使用telnet进行端口分析后,我无法解释为什么代码无法正常执行。

1 个答案:

答案 0 :(得分:0)

尝试

client.UseDefaultCredentials = false;