如何在vb.net中创建自动发送电子邮件?

时间:2017-04-04 00:41:00

标签: .net

Dim mailmsg As New System.Net.Mail.MailMessage()
    mailmsg.From = New MailAddress("yourname@domain.com")
    mailmsg.To.Add("to.domain.com")
    mailmsg.Subject = "Mail From .Net Application"
    mailmsg.IsBodyHtml = True
    mailmsg.Body = "Hi this is a mail from .net appl i have used same code......"
    mailmsg.Priority = MailPriority.Normal
    Dim client As New System.Net.Mail.SmtpClient()

    client.UseDefaultCredentials = False
    client.Credentials = New System.Net.NetworkCredential("yourname@domain.com", "password")
    client.Port = "587"
    client.Host = "smtp.gmail.com"
    client.EnableSsl = True
    Dim userstate As Object = mailmsg

    client.Send(mailmsg)
如果我的smtp出现问题,你们可以查看我的代码吗?然后我       想要为附件文件添加一些代码,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

mailmessage对象有一个附件属性,您可以这样使用:

mailmsg.Attachments.Add(new Attachment(PathToAttachment))

您确定使用Google的smtp正确端口吗?我以为是465号港口。

要测试邮件功能而不必担心smtp问题,您可以尝试使用smtp4dev之类的工具,它可以为您提供本地smtp服务器目标。请在此处查看:https://github.com/rnwood/smtp4dev