发送电子邮件给文本框中指定的收件人 [英] Send email to recipient specified in Textbox

查看:99
本文介绍了发送电子邮件给文本框中指定的收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此进行了编码,因此我可以发送电子邮件。

  Imports  System.Net.Mail 
公开 Form1
功能 SendEmail( ByVal 收件人作为列表( Of 字符串),_
ByVal FromAddress 作为 字符串,_
ByVal 主题作为 字符串,_
ByVal 正文作为 字符串,_
ByVal UserNa me 作为 字符串,_
ByVal 密码作为 字符串,_
可选 ByVal 服务器作为 字符串 = smtp.gmail.com,_
可选 ByVal 端口作为 整数 = 587 ,_
可选 ByVal 附件作为列表( String < /跨度> )= Nothing As String
Dim 电子邮件作为 MailMessage( )
尝试
Dim SMTPServer 正如 SmtpClient
对于 每个附件作为 字符串 附件
Email.Attachments.Add(附件(附件))
下一步
Email.From = MailAddress(FromAddress)
对于 每个收件人作为 字符串 收件人
电子邮件。 .Add(收件人)
下一步
Email.Subject = Subject
Email.Body = Body
SMTPServer.Host = Server
SMTPServer.Port =端口
SMTPServer.Credentials = System.Net.NetworkCredential(用户名,密码)
SMTPServer.EnableSsl = True
SMTPServer.Send(电子邮件)
Email.Dispose()
返回 电邮至&收件人( 0 )& 来自& FromAddress& 已发送。
Catch ex As SmtpException
Email.Dispose()
返回 发送电子邮件失败.Smtp错误。
Catch ex As ArgumentOutOfRangeException
Email.Dispose()
返回 发送电子邮件失败。检查端口号。
Catch Ex As InvalidOperationException
Email.Dispose()
返回 发送电子邮件失败。检查Po rt数。
结束 尝试
结束 功能
私有 Sub Button1_Click(发件人作为 对象,e As EventArgs)句柄 Button1.Click
Dim 收件人作为 列表( 字符串
Recipients.Add(MailTextBox.Text)
Dim FromEmailAddress As String =收件人( 0
昏暗主题作为 字符串 = < span class =code-string>从VB测试。

Dim Body As 字符串 = 电子邮件正文,如果你正在从您的Gmail帐户阅读此程序,该程序正常运行。
Dim UserName As 字符串 = GMAIL USERNAME WITHOUT(@ GMAIL> COM)
Dim 密码作为 字符串 = 密码
Dim 端口 As 整数 = 587
Dim 服务器作为 字符串 = smtp.gmail.com
Dim 附件作为 列表( String
MsgBox(SendEmail(收件人,FromEmailAddress,主题,正文,用户名,密码,服务器,端口,附件))
结束 Sub
结束



我的问题是它不会将电子邮件发送到mailtextbox中指定的电子邮件地址!事实上,它根本不发送它!你能帮助我吗?你可以帮我修改代码,这样我就可以把邮件发送给多个收件人吗?

谢谢!!!

解决方案

我认为这是防火墙/防病毒问题...检查您的防病毒/防火墙是否阻止应用程序。

我已经尝试过代码并且它可以100%工作但是系统机制专业人员要求我允许连接到RemotePort 587,因为它不是常见的。


尝试添加此

电子邮件。[] .Add( New  MailAddress(Recipient))

而不是

电子邮件。收件人。添加(收件人)





希望此帮助:)


I coded this so I am able to send emails.

Imports System.Net.Mail
Public Class Form1
    Function SendEmail(ByVal Recipients As List(Of String), _
                      ByVal FromAddress As String, _
                      ByVal Subject As String, _
                      ByVal Body As String, _
                      ByVal UserName As String, _
                      ByVal Password As String, _
                      Optional ByVal Server As String = "smtp.gmail.com", _
                      Optional ByVal Port As Integer = 587, _
                      Optional ByVal Attachments As List(Of String) = Nothing) As String
        Dim Email As New MailMessage()
        Try
            Dim SMTPServer As New SmtpClient
            For Each Attachment As String In Attachments
                Email.Attachments.Add(New Attachment(Attachment))
            Next
            Email.From = New MailAddress(FromAddress)
            For Each Recipient As String In Recipients
                Email.To.Add(Recipient)
            Next
            Email.Subject = Subject
            Email.Body = Body
            SMTPServer.Host = Server
            SMTPServer.Port = Port
            SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)
            SMTPServer.EnableSsl = True
            SMTPServer.Send(Email)
            Email.Dispose()
            Return "Email to " & Recipients(0) & " from " & FromAddress & " was sent."
        Catch ex As SmtpException
            Email.Dispose()
            Return "Sending Email Failed. Smtp Error."
        Catch ex As ArgumentOutOfRangeException
            Email.Dispose()
            Return "Sending Email Failed. Check Port Number."
        Catch Ex As InvalidOperationException
            Email.Dispose()
            Return "Sending Email Failed. Check Port Number."
        End Try
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Recipients As New List(Of String)
        Recipients.Add(MailTextBox.Text)
        Dim FromEmailAddress As String = Recipients(0)
        Dim Subject As String = "Test From VB."
        Dim Body As String = "email body text, if you are reading this from your gmail account, the program worked."
        Dim UserName As String = "GMAIL USERNAME WITHOUT  (@GMAIL>COM)"
        Dim Password As String = "Password"
        Dim Port As Integer = 587
        Dim Server As String = "smtp.gmail.com"
        Dim Attachments As New List(Of String)
        MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))
    End Sub
End Class


My problem is that it doesn't send the email to the email adress specified in the mailtextbox! In fact, it doesn't send it at all!Can you help me? Can you help me to modify the code so I can send the email to multiple recipients?
Thanks!!!

解决方案

I think it's a firewall / antivirus problem... check if your antivirus / firewall blocks the application.
I have tried the code and it works 100% but system mechanics professional asked me to allow the connection to RemotePort 587 because it is not a 'common one'.


try Adding this

Email.[To].Add(New MailAddress(Recipient))

instead of

Email.To.Add(Recipient)



hope this help :)


这篇关于发送电子邮件给文本框中指定的收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆