发送电子邮件SMTP例外

时间:2015-06-23 13:48:10

标签: c# .net email

当我尝试从c#

发送电子邮件时出现此错误
MailMessage mail = new MailMessage("from@gmail.com", "to@hotmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("from@gmail.com", "pwd");
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
  

类型' System.Net.Mail.SmtpException'未处理的例外情况。发生在System.dll中   附加信息:SMTP服务器需要安全连接或客户端未经过身份验证。   服务器响应为:5.5.1需要身份验证。

2 个答案:

答案 0 :(得分:1)

你应该删除这两行

 public void startClient()
    {
        this.clientSocket.Connect(this.serverIp, this.serverPort);
        Thread sending = new Thread(new ThreadStart(firstsend));

        byte[] bytess = new byte[800 * 600 * 3];

        while (true)
        {
            if (!Threadwork.IsConnected(this.clientSocket))
                break;
            this.read(bytess);
            Thread.Sleep(100);
        }
        this.clientSocket.Close();
     //shutdown > close   
    }
    public void firstsend( )
    {
       // this.clientSocket.Send(GetBytes(Form1.t1.Text));
        CPacket Clientpacket= new CPacket(Form1.t1.Text);
        while (true)
        {
           if (!IsConnected(clientSocket))
             break;
            if (possibleMessage != "")
            {
                Clientpacket.MessageFromClient = possibleMessage;
                possibleMessage = "";
            }
            BinaryFormatter b = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            b.Serialize(ms, Clientpacket);
            this.clientSocket.Send(ms.ToArray());
            Thread.Sleep(100);   
        }
    }

答案 1 :(得分:0)

试试此代码

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("from@gmail.com", "name", System.Text.Encoding.UTF8);
        mail.To.Add("to@gmail.com");
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential("from@gmail.com", "password");
        client.EnableSsl = true;
        mail.Subject = "this is a test email.";
        mail.Body = "this is my test email body";
        client.Send(mail);