发送电子邮件 - 错误

时间:2011-04-20 13:11:12

标签: c#

我正在尝试使用Winforms应用中的以下代码发送电子邮件。 用户名和密码正确且真实,但在代码中已被屏蔽。

我不知道我的本地机器是否有能力发送电子邮件?

但请告诉我,我需要查看我的机器配置(在IIS中)?

错误:“由于目标机器主动拒绝它,因此无法建立连接209.85.143.109:587”

//All Variable Declarations
String senderAddress = string.Empty;
String receiveraddress = string.Empty;
String emailSubject = string.Empty;
String emailMessageText = string.Empty;
MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient("smtp.gmail.com",587);


private void btnEmail_Click(object sender, EventArgs e)
{

      try
      {
            //Get the Values of Controls

            senderAddress = txtSenderEmail.Text;
            receiveraddress = txtReceiverEmail.Text;
            emailSubject = txtSubject.Text;
            emailMessageText = txtMessageBody.Text;

            //Pass the Values
            mail.From = new MailAddress(senderAddress, "xxxxxxxxxx", System.Text.Encoding.UTF8);
            mail.To.Add(receiveraddress);
            mail.Subject = emailSubject;
            mail.Body = emailMessageText;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxx");
            client.EnableSsl = true;
            client.Send(mail);
            System.Windows.Forms.MessageBox.Show("Email Sent Successfully");



      }
      catch (Exception mailException)
      {
            System.Windows.Forms.MessageBox.Show("Message Not Sent ,Error:   " + mailException.Message);
            throw;
      }
      finally
      {

      }



}

我为两个端口都做过telnet,错误来自:无法打开与主机的连接。

请看下面的图片,它应该会让你更清晰,

enter image description here

4 个答案:

答案 0 :(得分:4)

GMail requires you to use SSL。您需要用于TLS / STARTTLS的端口是587.对于SSL,它是465.以下是可用于通过命令行发送电子邮件的代码。 CommandLine.Utility可以在CodeProject下载。

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.ComponentModel;
using CommandLine.Utility;

namespace SmtpClientProgram
{
    class Program
    {        

        static void Main(string[] args)
        {
            Arguments cmdLine = new Arguments(args);        

            SmtpClient mailer = new SmtpClient();

            // --ssl means we are using SSL/TLS

            mailer.EnableSsl = Convert.ToBoolean(cmdLine["ssl"]);

            // -host=smtp.gmail.com

            if (cmdLine["host"] != null)
                mailer.Host = cmdLine["host"];
            else
            {
                Console.WriteLine("Hostname must be specified");
                return;
            }

            // -port=25

            if (cmdLine["port"] != null)
                try
                {
                    mailer.Port = Convert.ToInt32(cmdLine["port"]);
                }
                catch (Exception)
                {
                    Console.WriteLine("Port must be a number between 1 and 65535");
                }
            else
                mailer.Port = 25;

            // -user= -password=
            if (cmdLine["user"] != null && cmdLine["password"] != null)
            {                
                mailer.Credentials = new NetworkCredential(cmdLine["user"], cmdLine["password"]);
            }

            try
            {
                MailMessage mail = new MailMessage();

                mail.From = new MailAddress(cmdLine["from"], cmdLine["name"]);

                if (cmdLine["to1"] != null)
                    mail.To.Add(cmdLine["to1"]);
                else
                    Console.WriteLine("Must specify first TO address");

                if (cmdLine["to2"] != null)
                    mail.To.Add(cmdLine["to2"]);

                if (cmdLine["to3"] != null)
                    mail.To.Add(cmdLine["to3"]);

                if (cmdLine["subject"] != null)
                    mail.Subject = cmdLine["subject"];

                if (cmdLine["body"] != null)
                    mail.Body = cmdLine["body"];

                mailer.Send(mail);

                //mailer.Send(cmdLine["from"], cmdLine["to"], cmdLine["subject"], cmdLine["body"]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            Console.WriteLine("Success");            
        }
    }
}

编辑:如果您无法连接到端口587(服务可能已关闭),请尝试465使用SSL或25使用SSL。请参阅here for Gmail help on sending email

答案 1 :(得分:3)

似乎问题不在于您的代码,而在于您要使用的SMTP服务器。检查机器209.85.143.109上的端口587是否已打开并接受SMTP命令。

答案 2 :(得分:2)

这通常意味着在指定的位置没有运行邮件服务器,或者您需要配置防火墙以通过连接并设置路由器以将587的连接转发到网络上的正确服务器IP。 / p>

答案 3 :(得分:-2)

看起来它会转到某些SMTP服务器,您的IIS正在转发到您的组织SMTP服务器。

如果它只是用于开发,您可以使用PickUP目录功能,这可以让您在没有任何SMTP设置的情况下进行测试。 Google上存在大量信息

修改

删除了Google链接。有些人不喜欢它。虽然我并不想冒犯任何人,但我仍然会删除,但我的意图仍然是帮助

相关问题