System.Net.Mail.SMTPException: Failure Sending Mail

不打扰是莪最后的温柔 提交于 2019-12-25 05:13:10

问题


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

回答1:


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

}



回答2:


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


来源:https://stackoverflow.com/questions/10625391/system-net-mail-smtpexception-failure-sending-mail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!