电子邮件发送系统.mail.smtpExpetion

本文关键字:mail smtpExpetion 系统 电子邮件 | 更新日期: 2023-09-27 18:25:25

我想发送一封带有附件的电子邮件。这是我的代码:

try{
    OpenFileDialog dlgs = new OpenFileDialog();
    if (dlgs.ShowDialog() == DialogResult.OK)
    {
        string path = dlgs.FileName.ToString();
        label10.Text = path;
    }
    MailMessage mail = new MailMessage("rajithaprasad3@gmail.com","rajithaprasad3@gmail.com","no","no");
    mail.Attachments.Add(new Attachment(label10.Text));
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.Port=587;
    client.EnableSsl = true;
    client.Send(mail);   
    MessageBox.Show("Succcessfully Send Your Backup.Please check Mail Inbox", "done", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
catch (Exception ex){
    MessageBox.Show(ex.ToString());
    MessageBox.Show("This mail Cant Be Sent Make sure You are connected to the internet'n or check whether the particular file stil exite in the system.make sure provided informations are correct", "Eror", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

但当我运行应用程序时,它会抛出一条异常消息,如

我该怎么修?

电子邮件发送系统.mail.smtpExpetion

我看不出您在哪里为gmail帐户提供了登录凭据。您需要将UseDefaultCredentials设置为false,然后提供登录凭据。以下示例:

client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("email@gmail.com", "password");

此外,使用gmail设置MailMessage-from参数没有意义,因为gmail会忽略该设置。

此外,请确保您已将gmail帐户配置为允许不太安全的应用程序。