0

我曾多次尝试使用以下代码发送附件。我使用了端口 25、465、467 和 587,但都导致了相同的错误。我已经浏览了具有相同问题或接近相同问题的其他帖子,并尝试了许多涉及的修复程序,但没有一个对我有用。帮助!

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

2 回答 2

0

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

}
于 2012-06-23T08:39:27.537 回答
-1

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

缺少向电子邮件添加附件的代码。

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("path of the file to be attached");
mail.Attachments.Add(attachment);
于 2013-06-04T07:04:19.967 回答