C#.net发送邮件错误

本文关键字:错误 net | 更新日期: 2023-09-27 18:26:08

很抱歉,我知道有很多问题已经解决,但我已经尝试了很多使用这些解决方案,但我做不到,我不断收到以下错误,我正在使用C#.net 制作这个应用程序

错误:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1身份验证必需。

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Net.Mail;
  namespace WindowsFormsApplication2
 {
  public partial class Form1 : Form
 {
    bool selecteditems;
    public Form1()
    {
        InitializeComponent();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        MailAddress from = new MailAddress("samnake12345@gmail.com");
        MailAddress to = new MailAddress(textBox1.Text);

        MailMessage message= new MailMessage(from,to);
        message.Subject = textBox4.Text;
        message.Body = richTextBox1.Text;
        if (textBox2.Text != "")
        {
            MailAddress cc = new MailAddress(textBox2.Text);
            message.CC.Add(cc);
        }
        if (textBox3.Text != "")
        {
            MailAddress bcc = new MailAddress(textBox3.Text);
            message.Bcc.Add(bcc);
        }
        message.Attachments.Clear();
        for (int index = 0; index < listBox1.Items.Count; index++)
        {
            Attachment attdata = new Attachment(listBox1.Items[index].ToString());
            message.Attachments.Add(attdata);
        }
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("samnake12345@gmail.com", "password");
        SmtpServer.EnableSsl = true;
        SmtpServer.UseDefaultCredentials = false;
        SmtpServer.Send(message);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string[] files = new String[openFileDialog1.FileNames.Length];
            for (int index = 0; index < openFileDialog1.FileNames.Length; index++)
            {
                listBox1.Items.Add(openFileDialog1.FileNames[index].ToString());
            }
        }
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        selecteditems = true;
    }
         private void Form1_Load(object sender, EventArgs e)
    {
      }
   }
 }

C#.net发送邮件错误

MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
mail.To.Add(textBox1.Text);
mail.From = new MailAddress("samnake12345@gmail.com");
mail.Subject = textBox4.Text;
mail.IsBodyHtml = false;
mail.Body = richTextBox1.Text;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.Credentials = new NetworkCredential("samnake12345@gmail.com", "samisagoodboy");
smtp.EnableSsl = true;
smtp.Send(mail);

试试这个!