Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Sending email via SMTP only works when the From field is Hard coded but does not otherwise. How can I fix it? In ASP.net and VB.net Web Application

$
0
0
        Public Sub Send()

            Dim Message As New System.Net.Mail.MailMessage
            With Message
                .Subject = Me.Subject
                .Body = Me.Body
                .From = New MailAddress("noreply@portals.mypbiz.com", "Careers portal")

                AddAddressString(Me.MailFrom, .ReplyToList)
                .Sender = New MailAddress("noreply@portals.mypbiz.com", "Careers portal")
                AddAddressString(Me.MailTo, .To)
                AddAddressString(Me.MailCC, .CC)
                AddAddressString(Me.MailBCC, .Bcc)
                .IsBodyHtml = True
                If Not IsNothing(Attachments) Then
                    If Attachments.Length > 0 Then
                        For Each FileName As String In Split(Attachments, ";")
                            FileName = Replace(FileName, "~/", System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath & "/")
                            If FileName.StartsWith("/") Then
                                FileName = System.Web.HttpContext.Current.Server.MapPath(FileName)
                            End If

                            Try
                                Message.Attachments.Add(New System.Net.Mail.Attachment(FileName))
                            Catch
                                System.Web.HttpContext.Current.Response.Write(FileName & " could not be found")
                                ''System.Web.HttpContext.Current.Response.end()
                            End Try
                        Next
                    End If
                End If

                If DocumentIDs IsNot Nothing Then
                    For Each DocumentID As Integer In DocumentIDs
                        Dim Doc As New Document(DocumentID)
                        If System.IO.File.Exists(Doc.PathOnDisc) Then
                            Dim Attatchment As New System.Net.Mail.Attachment(Doc.PathOnDisc)
                            Attatchment.Name = Doc.OriginalFileName
                            Message.Attachments.Add(Attatchment)
                        End If
                    Next
                End If

            End With

            Try
                Dim Client As New System.Net.Mail.SmtpClient
                ''With Client

                ''    .Credentials = New NetworkCredential("admin@mypbiz.com", "$Ropic$new$t@_ge1!")
                ''    .DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
                ''    .Host = "mail.mypbiz.com" 'WebConfigurationManager.AppSettings("smtpserver")

                ''    .Send(Message)
                ''End With


                With Client
                    If WebConfigurationManager.AppSettings("smtpserver").Length > 0 Then
                        .DeliveryMethod = Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory 'Net.Mail.SmtpDeliveryMethod.Network
                        .PickupDirectoryLocation = "c:/outbox/"
                        .Host = WebConfigurationManager.AppSettings("smtpserver")
                    Else
                        .DeliveryMethod = Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis
                    End If

                    .Send(Message)
                End With

            Catch Ex As Exception
                _error = Ex.Message
                'System.Web.HttpContext.Current.Response.Write(_error.tostring)
                'System.Web.HttpContext.Current.Response.end()
            End Try


            Dim Query As String = ""

            Query &= vbCrLf & "IF @id != 0"
            Query &= vbCrLf & "BEGIN"
            Query &= vbCrLf & "    UPDATE emaillog SET error=@error, mailfrom=@mailfrom, mailto=@mailto, mailcc=@mailcc, mailbcc=@mailbcc, subject=@subject, emailcontentid=@emailcontentid WHERE id=@id;"
            Query &= vbCrLf & "    SELECT @id;"
            Query &= vbCrLf & "END"
            Query &= vbCrLf & "ELSE"

            Query &= vbCrLf & "BEGIN"
            Query &= vbCrLf & "    INSERT INTO emaillog (error, mailfrom, mailto, mailcc, mailbcc, subject, body, emailcontentid) VALUES (@error, @mailfrom, @mailto, @mailcc, @mailbcc, @subject, @body, @emailcontentid);"
            Query &= vbCrLf & "    SELECT SCOPE_IDENTITY();"
            Query &= vbCrLf & "END"

            _id = Core.DB.GetInteger(WebConfigurationManager.ConnectionStrings("logConnectionString").ConnectionString, Query, _
                Core.DB.SIP("id", _id), _
                Core.DB.SIP("error", _error), _
                Core.DB.SIP("emailcontentid", Me.ContentID), _
                Core.DB.SIP("mailfrom", IIf(IsNothing(Me.MailFrom) = True, "", Me.MailFrom)), _
                Core.DB.SIP("mailto", IIf(IsNothing(Me.MailTo) = True, "", Me.MailTo)), _
                Core.DB.SIP("mailcc", IIf(IsNothing(Me.MailCC) = True, "", Me.MailCC)), _
                Core.DB.SIP("mailbcc", IIf(IsNothing(Me.MailBCC) = True, "", Me.MailBCC)), _
                Core.DB.SIP("subject", IIf(IsNothing(Me.Subject) = True, "", Me.Subject)), _
                Core.DB.SIP("body", IIf(IsNothing(Me.Body) = True, "", Me.Body)))

        End Sub

Hello Everyone

The above code is for my email sending function. 

This works perfectly fine.

But as you can see  in the from field is Hard coded. (see below again) 

                .Subject = Me.Subject
                .Body = Me.Body
                .From = New MailAddress("noreply@portals.mypbiz.com", "Careers portal")

                AddAddressString(Me.MailFrom, .ReplyToList)
                .Sender = New MailAddress("noreply@portals.mypbiz.com", "Careers portal")
                AddAddressString(Me.MailTo, .To)
                AddAddressString(Me.MailCC, .CC)
                AddAddressString(Me.MailBCC, .Bcc)
                .IsBodyHtml = True

But I want to change it. In my front end there is text box to provide the email address for from field , To field , CC and BCC.  But as the from field is  hard coded the email always comes from noreply@portal.mypbiz.com 

how can I fix it so that the email will be sent from the email address provided by the user in front end.

I have changed hard coded line and tried the following code .but then i don't receive any email i.e the email sending doesn't work.

  With Message
                .Subject = Me.Subject
                .Body = Me.Body
                 .From = New MailAddress(Me.MailFrom, "Careers portal")

                AddAddressString(Me.MailFrom, .ReplyToList)
                .Sender = New MailAddress(Me.MailFrom, "Careers portal")

                AddAddressString(Me.MailTo, .To)
                AddAddressString(Me.MailCC, .CC)
                AddAddressString(Me.MailBCC, .Bcc)

Though I can see that  Me.Mail From contains the email address provided in the front end field when I debug the code.

But No email is received and I get the following error with the changed code. 

Server Error in '/' Application.

<div align="center">
</div>

The specified string is not in the form required for an e-mail address.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

Source Error: 

 

Line 276:                .Body = Me.Body

Line 277:                ''.From = New MailAddress("noreply@portals.mypbiz.com", "Careers portal")

Line 278:                .From = New MailAddress(Me.MailFrom, "Careers portal")

Line 279:

Line 280:                AddAddressString(Me.MailFrom, .ReplyToList)


Source File: D:\site\V4\Test\Portals\App_Code\core\Email.vb    Line: 278 

Stack Trace: 

 [FormatException: The specified string is not in the form required for an e-mail address.]

   System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index) +2604660

   System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +304

   Core.Email.Send() in D:\site\V4\Test\Portals\App_Code\core\Email.vb:278

  

We are using HMAIL server 

Can you please help me to fix this. 

I have tried to be specific . But as the problem is bit complex I understand my description may not be enough. Please let me know if you need further details.

i will be grateful 


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>