We help IT Professionals succeed at work.

Sending Email - Best Practice

rocky050371
rocky050371 asked
on
329 Views
Last Modified: 2012-05-05
I want to provide the ability for users to send email from my application, is it common for them to have to set up the smtp server name to do this? In the past I have used the outlook library but this does limit me.

Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
If you are using VB .Net 2005 this is the new way to sent mail in this version.

Imports System.Net.Mail

    Private Sub SendEmail(ByVal sender As String, _
        ByVal recipient As String, ByVal subject As String, _
        ByVal body As String, Optional ByVal attachmentString As String = "")

        Dim fromAddress As New MailAddress(sender)
        Dim toAddress As New MailAddress(recipient)
        Dim message As New MailMessage(fromAddress, toAddress)

        Dim mailSender As SmtpClient
        ' Change the Smtp server name on the next line
        mailSender = New SmtpClient("smtp-server.ny.rr.com", 25)

        message.Bcc.Add(fromAddress)
        message.Subject = subject
        message.IsBodyHtml = False
        message.Body = body

        If Not attachmentString = "" Then
            Dim msgAttach As New Attachment(attachmentString)
            message.Attachments.Add(msgAttach)
        End If

        Try
            mailSender.Send(message)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Mail Not Sent")
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        SendEmail("From@Domain.com", "To@Domain.com", _
            "This is a test Subject line", "This is the Body", "c:\Temp\Attachment_If_any\pp1.pdf")

    End Sub

Other resources:
http://www.freevbcode.com/ShowCode.asp?ID=6401
http://www.codeproject.com/vb/net/epsendmail.asp
http://www.example-code.com/vbdotnet/vbdotnetSendEmail.asp
www.vbhelp.gr/sendmail.zip
This sample shows how to send email over SMTP using classes in the System.Web.Mail namespace.


SOURCE: https://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22610838.html?sfQueryTermInfo=1+email

Author

Commented:
Thanks, but it is the smtp server name and the port which are my issue. Is it common to set these up at the user site, if so, in a standard app.config?
Project manager
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Ok, out of curiousiy do you you know if it is possible for the messgae to include formatted text
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
Perhaps you could consider using some kind of encryption/decryption to safely store your address and port in the config-file.
Something sort like is the connectionstring
Example: Encrypt Connection Strings in VS 2005 .config Files
http://www.developer.com/net/vb/article.php/10926_3500906_2
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
>> Ok, out of curiousiy do you you know if it is possible for the messgae to include formatted text

You can send HTML-emails, see http://www.systemwebmail.com/faq/2.2.aspx

Author

Commented:
Do you need to set outbound authentication?
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi rocky050371;

To your question, "Do you need to set outbound authentication?"

That is determined by the ISP that the user is using on. Not all ISP require authentication while others do. Your best bet is to create a resource string and add such items as SMTPServer, SMTPPort and others so that the user can configure what is needed once and use that for future mail messages.

Fernando

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.