使用Visual Basic发送电子邮件?

时间:2013-04-09 21:52:20

标签: vb.net visual-studio

我想知道我是否可以在Visual Basic中发送电子邮件。我希望能够通过按下“Button1”按钮在“Textbox1.text”和“Textbox2.text”中发送什么内容。 这有可能吗?

额外注意: 我希望它可以在我已经制作的Windows程序中运行。

1 个答案:

答案 0 :(得分:0)

.NET确实支持通过System.Net.Mail发送电子邮件。 看看我认为可以帮助你:

您只需要获取文本的值并将其应用于示例。

Visual Basic

Class Mailer
        ''one static method for sending e-mails
        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)
            ''create a SmtpClient object to allow applications to send 
            ''e-mail by using the Simple Mail Transfer Protocol (SMTP).
            Dim MailClient As System.Net.Mail.SmtpClient = _
            New System.Net.Mail.SmtpClient(MailServer, MailPort)
            ''create a MailMessage object to represent an e-mail message
            ''that can be sent using the SmtpClient class
            Dim MailMessage = New System.Net.Mail.MailMessage( _
            [From], [To], Subject, Body)
            ''sets a value indicating whether the mail message body is in Html.
            MailMessage.IsBodyHtml = IsBodyHtml
            ''sets the credentials used to authenticate the sender
            If (AuthUsername IsNot Nothing) AndAlso (AuthPassword _
                                                     IsNot Nothing) Then
                MailClient.Credentials = New  _
                System.Net.NetworkCredential(AuthUsername, AuthPassword)
            End If
            ''add the files as the attachments for the mailmessage object
            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

<强> C#

MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");

message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.To.Add(new MailAddress("recipient3@foo.bar.com"));

message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = Textbox2.text;

SmtpClient client = new SmtpClient();
client.Send(message);