Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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
Posted

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
 
Share this answer
 
Comments
Nanda_MR 28-Feb-11 1:19am    
Thanks

From your code I got solution.

Regards,
Nanda
Geeky Sharma 2-Oct-20 2:36am    
I just copied the whole function with Import statements, and i call it using...

EmailSender("RTCMAILSERVER@GMAIL.COM", "Geeky Sharma", "purshotam7062@gmail.com", "Its just a test", "Testing...", "RTCSERVER2020@", "J:\Geeky Sharma\Channel items\GeekySharmalogo.jpg")

But it shows and error after 10-20 seconds that...

{"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at"}

can you please help me one more time?
Your "From" address probably isn't a gmail address. We can't tell though because you didn't provide it in a comment.
 
Share this answer
 
Comments
Nanda_MR 24-Feb-11 5:57am    
My From and To both address is Gmail. while using Gmail address I am getting this error
Failure sending mail

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900