C#.net发送邮件错误

时间:2014-10-09 07:25:57

标签: c#

对不起,我知道有很多关于此的问题,但我已经尝试了很多使用这些解决方案,但我不能,我继续得到以下错误,我正在使用C#制作这个应用程序。净

  

错误: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)
    {

      }
   }
 }

1 个答案:

答案 0 :(得分:1)

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);

试试这个!