System.Net.Mail.SMTPException: Failure Sending Mai

2019-09-19 05:32发布

I have tried repeatedly to send an attachment using the following code. I have used Port 25, 465, 467 and 587 with all resulting in the same error. I have scoured through other posts with the same problem or close to the same problem and tried many of the fixes involved but none worked for me. HELP!

try
{
    var smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new System.Net.NetworkCredential("thisisjustatestokay1@gmail.com", "xxxxxxxxxxxx")
    };
    using (var message = new MailMessage("thisisjustatestokay1@gmail.com", "thisisjustatestokay1@gmail.com")
        {
            Subject = "Boom Baby!",
            Body = "The stuff!"
        })
    {
        smtp.Send(message);
    }
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}

2条回答
Juvenile、少年°
2楼-- · 2019-09-19 06:14

I have tried repeatedly to send an attachment using the following code

Your code is missing to add an attachment to the email.

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("path of the file to be attached");
mail.Attachments.Add(attachment);
查看更多
祖国的老花朵
3楼-- · 2019-09-19 06:16

The code looks good, but I modified it as shown below, and it worked flawlessly.

protected void Btn_SendMail_Click(object sender, EventArgs e){

  var smtp = new SmtpClient {
    Host = "smtp.gmail.com",
    Port = 587, 
    EnableSsl = true, 
    DeliveryMethod = SmtpDeliveryMethod.Network, 
    UseDefaultCredentials = false, 
    Credentials = new System.Net.NetworkCredential("someone@gmail.com","**********")
  };

  MailMessage mailObj = new MailMessage("someone@gmail.com", txtTo.Text, txtSubject.Text, txtBody.Text);

  smtp.Send(mailObj);

}
查看更多
登录 后发表回答