电子邮件发送system.mail.smtpExpetion

时间:2015-12-03 11:45:34

标签: c# smtp

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

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

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

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我不知道您在哪里提供了Gmail帐户的登录凭据。您需要将UseDefaultCredentials设置为false,然后提供登录凭据。示例如下:

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

此外,使用gmail无法从参数设置MailMessage,因为gmail会忽略该设置。

此外,请确保您已配置gmail帐户以允许安全性较低的应用程序。

相关问题