locked
FAQs: 怎样用VB.NET设计一个发电子邮件的程序? RRS feed

  • 问题

  • 为了帮助大家更好地学习 Visual Basic技术,微软论坛技术支持团队编辑了一些列的 "Visual Basic 常见问题及解答" 精华帖。

    本帖的主题是:怎样用VB.NET设计一个发电子邮件的程序?

    如果您觉得这个帖子对您的学习、工作有所帮助,请再把这个帖子分享给你的同学、同事和朋友。

    如果您想阅读更多的 "Visual Basic 常见问题及解答",请打开索引页面:
    http://social.msdn.microsoft.com/Forums/zh-CN/vbasiczhchs/thread/c9ab4b8e-99dc-49a2-bcea-30f3ecc3c3b7


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月3日 2:44

答案

  • 你可以用System.Net.Mail命名空间完成发送邮件的功能。

     

    参考代码:

       Module Module1

        Sub Main()

            '' 发送测试邮件

            ''

            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _

                            "Test HTML mail with attachments", "test email <br /> yeah!", _

                            "smtp.magnet.ie", , , New String() {"c:\file1.txt", _

                                                                "c:\file2.txt", "c:\file3.txt"})

            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _

                            "Test text-only mail with attachments", "test email yeah!", _

                            "smtp.magnet.ie", False, , New String() {"c:\file1.txt", _

                                                                     "c:\file2.txt", "c:\file3.txt"})

            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _

                            "Test HTML mail with no attachments", _

                            "test email <br /> yeah!", "smtp.magnet.ie")

            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _

                            "Test text-only with no attachments and authentication", _

                            "test email yeah!", "smtp.magnet.ie", , , , "MyUsername", "P@$$w0rd")

            Mailer.SendMail(From:="jamie@plenderj.com", _

                            To:="jamie@plenderj.com", _

                            Subject:="Test text-only with no attachments and authentication #2", _

                            Body:="test email yeah!", _

                            Attachments:=Nothing, _

                            IsBodyHtml:=False, _

                            MailPort:=125, _

                            MailServer:="mail.magnet.ie")

        End Sub

        Class Mailer

            ''一个共享发送邮件方法

            Shared Sub SendMail(ByVal [From] As String, ByVal [To] As String, _

                                ByVal Subject As String, ByVal Body As String, ByVal MailServer _

                                As String, Optional ByVal IsBodyHtml As Boolean = True, _

                                Optional ByVal MailPort As Integer = 25, _

                                Optional ByVal Attachments() As String = Nothing, Optional _

                                ByVal AuthUsername As String = Nothing, Optional ByVal _

                                AuthPassword As String = Nothing)

                ''创建一个 SmtpClient 对象,是得应用程序有权发送邮件

                ''e-mail 是用 Simple Mail Transfer Protocol (SMTP)协议.

                Dim MailClient As System.Net.Mail.SmtpClient = _

                New System.Net.Mail.SmtpClient(MailServer, MailPort)

                 Dim MailMessage = New System.Net.Mail.MailMessage( _

                [From], [To], Subject, Body)

                ''设置一个值,表明邮件是不是Html形式的.

                MailMessage.IsBodyHtml = IsBodyHtml

                ''设置证书

                If (AuthUsername IsNot Nothing) AndAlso (AuthPassword _

                                                         IsNot Nothing) Then

                    MailClient.Credentials = New  _

                    System.Net.NetworkCredential(AuthUsername, AuthPassword)

                End If

                ''添加附件

                If (Attachments IsNot Nothing) Then

                    For Each FileName In Attachments

                        MailMessage.Attachments.Add( _

                        New System.Net.Mail.Attachment(FileName))

                    Next

                End If

                MailClient.Send(MailMessage)

            End Sub

        End Class

    End Module
    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月3日 2:45