Solved

How to send emails in visual studio 2005

Posted on 2010-08-16
4
333 Views
Last Modified: 2013-12-18
Hello, can someone help me with understanding (including any imports) how to build an HTML/ASPX page that has a button to click to send an email to pre-coded email addresses with multiple attachments.  

I have muliple folders on my iis server that have a pdf file inserted into them once a month via impromptu.  I would like to have my user click one button (or something like that) and have the appropriate file in each folder retreived and sent to the appropriate email address.  I prefer not to have the user manually enter each address.  Thank you
0
Comment
Question by:ilfocorp
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
dschauhan82 earned 200 total points
ID: 33451385
http://www.codeproject.com/KB/aspnet/EmailApplication.aspx.
http://www.codeproject.com/KB/aspnet/ASPNETwebmail.aspx?msg=1455810
These links for attach the attachment from browse button, but you need to only change for directiry attachment.

Hope this help....
0
 
LVL 4

Expert Comment

by:dschauhan82
ID: 33451662
Code to send mail with attechment.,        public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)        {                        try
            {                
                // Create the mail message
                MailMessage objMailMsg = new MailMessage(strFrom, strTo);                                objMailMsg.BodyEncoding = Encoding.UTF8;                objMailMsg.Subject = strSubject;                objMailMsg.Body = strMsg;                Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));                objMailMsg.Attachments.Add(at);                objMailMsg.Priority = MailPriority.High;                objMailMsg.IsBodyHtml = true;                //prepare to send mail via SMTP transport
                SmtpClient objSMTPClient = new SmtpClient();                objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;                objSMTPClient.Send(objMailMsg);                return true;                            }            catch (Exception ex)            {                throw ex;            }        }         hope it helps./.
0
 

Assisted Solution

by:tapozsl
tapozsl earned 200 total points
ID: 33454398
Some code is there..just check this.
#Region " Send Mail Button Click                 "
    Protected Sub btnSendMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendMail.Click
        Try
            Dim attach1 As String = ""
            Dim strFileName As String = ""
            Dim objCom As New Common
            Dim sContentType As String = ""
            If FileUpload1.PostedFile.FileName <> "" Then
                Dim ulFile As HttpPostedFile = FileUpload1.PostedFile
                Dim nFileLen As Int64 = ulFile.ContentLength
                If (nFileLen > 0) Then
                    strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName)
                    strFileName = "Uploads/" + strFileName

                    'Saveing the file in proper directory
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(strFileName))
                    objCom.SendMail(tbFrom.Text, tbTo.Text, tbSubject.Text, tbMessage.Text, Server.MapPath(strFileName), cbInline.Checked)
                End If
            Else
                objCom.SendMail(tbFrom.Text, tbTo.Text, tbSubject.Text, tbMessage.Text)
            End If
        Catch ex As Exception
        End Try
    End Sub
#End Region


Public Function SendMail(ByVal sFrom As String, ByVal sTo As String, ByVal sSubject As String, ByVal sMessage As String, ByVal sFile As String, Optional ByVal sInline As Boolean = False) As Boolean
        Try
            Dim Mail As New SmtpClient(System.Configuration.ConfigurationManager.AppSettings("SMTP"))
            'set the addresses and content
            Dim mMessage As New MailMessage(sFrom, sTo, sSubject, sMessage)

            '*************************Send Attchment *************************'
            If Not sFile = "" Then
                Dim attach As New System.Net.Mail.Attachment(sFile)
                attach.ContentDisposition.Inline = True

                If sInline <> False Then
                    '*************************Send Inline Image ***********************'
                    ' create the Html part
                    'to embed images, we need to use the prefix 'cid' in the img src value
                    'the cid value will map to the Content-Id of a Linked resource.
                    'thus <img src='cid:image'> will map to a LinkedResource with a ContentId of 'image'

                    Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("", Nothing, "text/plain")
                    Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(".<img src=cid:image>", Nothing, "text/html")
                    'select the image 
                    Dim at As New Attachment(sFile)

                    'create LinkedResource (embedded image)   
                    Dim logo As New LinkedResource(sFile, "text/plain")
                    logo.ContentId = "image"
                    'add the LinkedResource to the appropriate view
                    htmlView.LinkedResources.Add(logo)
                    'add the views
                    mMessage.AlternateViews.Add(plainView)
                    mMessage.AlternateViews.Add(htmlView)
                    '--------------------------------------End----------------------------'
                Else
                    mMessage.Attachments.Add(attach)
                End If
            End If
            '--------------------------------------End----------------------------'
            'Specifing HTML mail
            mMessage.IsBodyHtml = True
            'Sending the email finally
            Mail.Send(mMessage)
            Return True
        Catch ex As Exception

        End Try
    End Function

    Public Function SendMail(ByVal sFrom As String, ByVal sTo As String, ByVal sSubject As String, ByVal sMessage As String) As Boolean
        Dim Mail As New SmtpClient(System.Configuration.ConfigurationManager.AppSettings("SMTP"))
        Mail.Send(sFrom, sTo, sSubject, sMessage)
        Return True
    End Function

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question