我已经使用下面的代码成功发送了电子邮件:
Friend Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As SmtpClient
Dim e_mail As MailMessage
Smtp_Server = New SmtpClient()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("myuserid@gmail.com", "mypassword")
Smtp_Server.EnableSsl = True
Smtp_Server.Port = 587
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtFrom.Text)
e_mail.To.Add(txtTo.Text)
e_mail.Subject = "Email Sending Subject"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text ''Take from textbox
attachment = New System.Net.Mail.Attachment("D:\mypdfdoc.pdf")
e_mail.Attachments.Add(attachment) 'attachment
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
但是当我附加文件时:
attachment = New System.Net.Mail.Attachment("D:\mypdfdoc.pdf")
e_mail.Attachments.Add(attachment) 'attachment
它抛出一个错误: System.Net.Mail.SmtpException:操作已超时。在 System.Net.Mail.SmtpClient.Send(MailMessage消息)
(不带附件,可以通过启用来自Google帐户的安全性较低的应用来正常工作) 哪种解决方案可用。