在VB.NET中发送SMTP电子邮件时出现问题 [英] Problem Sending SMTP E-Mail In VB.NET

查看:304
本文介绍了在VB.NET中发送SMTP电子邮件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们

我写了一个代码来发送简单的电子邮件服务.此代码在本地服务器到本地服务器上正常工作.但是,当我尝试将电子邮件本地服务器发送到gmail,hotmail,yahoo时.它抛出以下错误

不允许使用邮箱名称.服务器响应为:很抱歉,您所在的位置拒绝中继"

如果我使用Gmail,Hotmail或其他ID作为其他电子邮件服务器的发件人,则会引发错误
发送邮件失败.

此代码我正在代理服务器中执行".我已经获得Gmail,Hotmail和yahoomail的授权.

我在Google中搜索过,但没有找到解决方法.

这是我的代码

Hi Guys

I wrote a code to send simple E-Mail service. This code is working fine in local server to local server. But When I try to send e-mail local server to gmail, hotmail, yahoo. it is throwing below error

"Mailbox name not allowed. The server response was : Sorry, relaying denied from your location"

If i use Gmail, Hotmail or other ID as sender to other E-mail server it is throwing error
Failure sending mail.

"This code I am executing in proxy server". I have authorization for Gmail, Hotmail and yahoomail.

I searched in Google but I am not get solution.

This is my code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New Net.NetworkCredential(TxtUserName.Text, TxtPassword.Text)
        SmtpServer.Port = 25 ''25,26,578,468 etc...
        SmtpServer.Host = TxtServer.Text ''mail.gmail.com
        mail = New MailMessage()
        mail.From = New MailAddress(TxtUserName.Text)
        mail.To.Add(TxtSender.Text)
        mail.Subject = "Test Mail"
        mail.Body = "This is for testing SMTP mail from VB.NET"
        SmtpServer.Send(mail)
        MsgBox("mail send")
    Catch exc As Net.Mail.SmtpException
        MessageBox.Show(exc.Message.ToString, "Error?", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

推荐答案

您好,
您可以使用此代码从所有邮件服务器发送电子邮件到

最好的问候
Morteza Shoja

_______________________________________
导入System.Net
导入System.Net.Mail
导入System.Net.Mime
_______________________________________
私有子EmailSender(ByVal从作为字符串,ByVal显示名称作为字符串,ByVal发送至作为字符串,ByVal消息作为字符串,ByVal主题作为字符串,ByVal密码作为字符串,ByVal附加为字符串)
使用mailMessage作为新MailMessage(New MailAddress(SendTo),New MailAddress(SendTo))
mailMessage.Body =消息
mailMessage.Subject =主题
试试
将SmtpServer调暗为新的SmtpClient()
SmtpServer.Credentials =新的System.Net.NetworkCredential(发件人,密码)
SmtpServer.Port = 587

Dim MS()As String = From.ToString.Split("@")
昏暗的MailServer()作为字符串= MS(1).ToString.Split(.")
选择案例UCase(MailServer(0))
大小写为="GMAIL"
SmtpServer.Host ="smtp.gmail.com"
SmtpServer.EnableSsl = True
大小写为="YAHOO"
SmtpServer.Host ="smtp.mail.yahoo.com"
SmtpServer.EnableSsl = False
大小写为="AOL"
SmtpServer.Host ="smtp.aol.com"
SmtpServer.EnableSsl = False
大小写="LIVE"
SmtpServer.Credentials =新的System.Net.NetworkCredential(发件人,密码)
SmtpServer.Host ="smtp.live.com"
SmtpServer.EnableSsl = True
结束选择

mail =新MailMessage()
Dim addr()As String = SendTo.Split(,")
mail.From =新MailAddress(发件人,DisplayName,System.Text.Encoding.UTF8)
昏暗的我作为字节
对于i = 0要加长-1
mail.To.Add(addr(i))
接下来我
mail.Subject =主题
mail.Body =消息
mail.BodyEncoding = System.Text.Encoding.UTF7
如果为Attach.Length<> 0然后
mail.Attachments.Add(新附件(附件))
如果结束
mail.IsBodyHtml = True
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
mail.ReplyTo =新的MailAddress(SendTo)
SmtpServer.Send(邮件)
异常捕获
MessageBox.Show(例如,Message,"EMail",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
结束尝试
最终使用
结束Sub
Hello,
You can use this code to send email to from all mail servers

Best Regards
Morteza Shoja

_______________________________________
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
_______________________________________
Private Sub EmailSender(ByVal From As String, ByVal DisplayName As String, ByVal SendTo As String, ByVal Message As String, ByVal Subject As String, ByVal Password As String, ByVal Attach As String)
Using mailMessage As New MailMessage(New MailAddress(SendTo), New MailAddress(SendTo))
mailMessage.Body = Message
mailMessage.Subject = Subject
Try
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New System.Net.NetworkCredential(From, Password)
SmtpServer.Port = 587

Dim MS() As String = From.ToString.Split("@")
Dim MailServer() As String = MS(1).ToString.Split(".")
Select Case UCase(MailServer(0))
Case Is = "GMAIL"
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Case Is = "YAHOO"
SmtpServer.Host = "smtp.mail.yahoo.com"
SmtpServer.EnableSsl = False
Case Is = "AOL"
SmtpServer.Host = "smtp.aol.com"
SmtpServer.EnableSsl = False
Case Is = "LIVE"
SmtpServer.Credentials = New System.Net.NetworkCredential(From, Password)
SmtpServer.Host = "smtp.live.com"
SmtpServer.EnableSsl = True
End Select

mail = New MailMessage()
Dim addr() As String = SendTo.Split(",")
mail.From = New MailAddress(From, DisplayName, System.Text.Encoding.UTF8)
Dim i As Byte
For i = 0 To addr.Length - 1
mail.To.Add(addr(i))
Next i
mail.Subject = Subject
mail.Body = Message
mail.BodyEncoding = System.Text.Encoding.UTF7
If Attach.Length <> 0 Then
mail.Attachments.Add(New Attachment(Attach))
End If
mail.IsBodyHtml = True
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
mail.ReplyTo = New MailAddress(SendTo)
SmtpServer.Send(mail)
Catch ex As Exception
MessageBox.Show(ex.Message, "EMail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Using
End Sub


您的发件人"地址可能不是Gmail地址.我们无法分辨,因为您没有在评论中提供它.
Your "From" address probably isn''t a gmail address. We can''t tell though because you didn''t provide it in a comment.


这篇关于在VB.NET中发送SMTP电子邮件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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