sending Lotus Notes mail with attachment from asp.net application

Have an ASP.NET application with vb code behind on one page have a few controls (gridview,textbox,dropdown list,radio buttons) and need to send values of those controls via Lotus Notes mail to other people. In other words I need to send data from form(sql server database) via Lotus Notes. Also in sql table I have image field (sql server 2000) with document that must be attached to this mail. How to do this, can someone provide me with code for sending mails from asp.net via Lotus Notes that includes sending attachments?

On iis server I have installed Lotus Notes 8, same on users machine, and I use sql server 2000 with asp.net 2.0

Thanks in forward.
LVL 1
kahvedzicAsked:
Who is Participating?
 
mbonaciCommented:
Here's NotesRichTextItem class:
    http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/topic/com.ibm.designer.domino.main.doc/H_NOTESRICHTEXTITEM_CLASS.html

where you'll find methods like:
    AddNewLine
    AppendText
    AppendTable
    AppendDocLink

which you can use to add content to your Body field.

And here's example for AppendText method:
    http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/topic/com.ibm.designer.domino.main.doc/H_EXAMPLES_APPENDTEXT_METHOD.html
0
 
mbonaciCommented:
0
 
Amandeep Singh BhullarCommented:
Check the article
http://www.codeproject.com/KB/vbscript/SendMailLotusNotes.aspx

Hope this will solve your issue.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
kahvedzicAuthor Commented:
As much as I can understand this question is about Lotus Notes mail with html content. I need to create form for sending mails from the begining. What do I have to do?
0
 
kahvedzicAuthor Commented:
Come on guys is there anyone with code example for sending lotus notes mail with attachments from asp.net application? I would realy appreciate some help.
0
 
mbonaciCommented:
The code from my first post was not OK?
Dim L_objEmailClient As New System.Net.Mail.MailMessage()
 
L_objEmailClient.Body = body.ToString()
L_objEmailClient.IsBodyHtml = False
 
Dim L_data As Byte() = Encoding.ASCII.GetBytes(attachment.ToString)
Dim L_MemoryStream As New System.IO.MemoryStream(L_data)
 
Dim L_StreamWriter As New System.IO.StreamWriter(L_MemoryStream)
 
Dim mail_Attachment As New System.Net.Mail.Attachment(L_MemoryStream, "registration.html", System.Net.Mime.MediaTypeNames.Text.Html)
 
L_objEmailClient.Attachments.Add(mail_Attachment)
 
smtpClient.Send(L_objEmailClient)

Open in new window

0
 
kahvedzicAuthor Commented:
it says that smtp client is not declared, also I declare body as object. How to declare smtpclient?
0
 
kahvedzicAuthor Commented:
Im not sure about smtp protocol I only have lotus/domino server and no smtp servers. For reading lotus in asp.net I use NotesSession no smtp client.
0
 
mbonaciCommented:
OK, then use normal Lotus Domino COM classes (this is written in standard VB, but Lotus objects work the same way):
Sub ComposeNotesMail()
	Dim oSess As Object
	Dim oMailDB As Object
	Dim oMailDoc As Object
	Dim oBody As Object

	Dim ws As Object
	Dim uidoc

	Dim mailsvr
	Dim mailfile

	Set oSess = CreateObject("Notes.NotesSession")

	mailsvr = oSess.GetEnvironmentString("MailServer", True)
	mailfile = LCase(oSess.GetEnvironmentString("MailFile", True))

	Set oMailDB = oSess.GETDATABASE("", "")
	Call oMailDB.open(mailsvr, mailfile)

	If oMailDB.isopen Then
		Set oMailDoc = oMailDB.CREATEDOCUMENT
	Else
		MsgBox "Could not open Notes", 32
	End If

	oMailDoc.subject = "Test mail"
	oMailDoc.Form = "Memo"

	Set oBody = oMailDoc.CREATERICHTEXTITEM("BODY")

	Call oBody.Embedobject(1454, "", "c:\Testattachment.htm")

	Set ws = CreateObject("Notes.NotesUIWorkspace")
	Set uidoc = ws.editdocument(True, oMailDoc, False)

	Set oSess = Nothing
	Set oMailDB = Nothing
	Set oMailDoc = Nothing
	Set ws = Nothing
	Set uidoc = Nothing
End Sub

Open in new window

0
 
mbonaciCommented:
0
 
kahvedzicAuthor Commented:
This is working but not the way I want to work.

I call ComposeNotesMail() on button click and everything is working fine, but at the end I have opened Lotus Notes client and new mail composed and waiting for me to enter who to send and click on send button.

What I want to do is to send mail directly from asp.net application (I have dropdown list with addresses where I can send mail,also have textboxes for subject and body) and I want to use text from textboxes for those fields, then click send and mail would be sent without user have to click anything in LN client.

Is it possible to send mails this way?
0
 
mbonaciCommented:
0
 
kahvedzicAuthor Commented:
with this code I can send mail directly from application, this is working perfect, but how to add body and attachment to this?
0
 
kahvedzicAuthor Commented:
sorry this is code behind

Sub ComposeNotesMail()

        Dim sess As New NotesSession
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        Dim mailServer As String, mailFile As String
        Dim convertMime As Boolean


        ' Init Notes session
        Call sess.Initialize("")

        ' Open user's email
        mailServer = sess.GetEnvironmentString("MailServer", True)
        mailFile = sess.GetEnvironmentString("MailFile", True)
        db = sess.GetDatabase(mailServer, mailFile)

        ' Create an email doc
        doc = db.CreateDocument
        Call doc.ReplaceItemValue("Form", "Memo")
        Call doc.ReplaceItemValue("SendTo", DropDownList1.SelectedValue.Trim)
        Call doc.ReplaceItemValue("Subject", TextBox2.Text)
        doc.SaveMessageOnSend = True
End Sub

Open in new window

0
 
mbonaciCommented:
There's a function in the last link I posted. Use it.
0
 
kahvedzicAuthor Commented:
now I have this,

    Sub SendMimeMemo(ByVal sendto As Object, ByVal subject As String, ByVal html As String, ByVal attachments() As String, ByVal imageFiles() As String, ByVal imageTypes() As String, ByVal imageIds() As String, ByVal fromFile As Boolean)

        '/**
        ' * Sends an html formatted email.
        ' * This function supports file attachments and inline images.
        ' * The html source may be passed in as a string or may be read from a file.
        ' * @param sendto A string (or array of strings) containing the email recipient(s).
        ' * @param subject A string containing the email subject.
        ' * @param html A string containing html for the email body or the path to an html file.
        ' * @param attachments An array of strings containing the full path to files to attach to this email.
        ' * @param imageFiles An array of strings containing the full path to any inline images.
        ' * @param imageTypes An array of strings containing the mime type for the images in the imageFiles parameter ("image/jpg", "image/gif").
        ' * @param imageIds An array of strings containing the content id (CID) for the images in the imageFiles parameter.
        ' * @param fromFile Set this parameter to True if the html parameter points to a file.
        ' */

        Dim sess As New NotesSession
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        Dim stream As NotesStream
        Dim mimeBody As NotesMIMEEntity, mimeHtml As NotesMIMEEntity
        Dim mimeFile As NotesMIMEEntity, mimeImage As NotesMIMEEntity
        Dim mimeHeader As NotesMIMEHeader
        Dim mailServer As String, mailFile As String
        Dim convertMime As Boolean

        ' Init Notes session
        Call sess.Initialize("")

        ' Open user's email
        mailServer = sess.GetEnvironmentString("MailServer", True)
        mailFile = sess.GetEnvironmentString("MailFile", True)
        db = sess.GetDatabase(mailServer, mailFile)

        ' Create an email doc
        doc = db.CreateDocument
        Call doc.ReplaceItemValue("Form", "Memo")
        Call doc.ReplaceItemValue("SendTo", TextBox1.Text)
        Call doc.ReplaceItemValue("Subject", TextBox2.Text)
        doc.SaveMessageOnSend = True

        ' add the body as a mime html part
        convertMime = sess.convertMime
        sess.convertMime = False
        stream = sess.CreateStream()
        If (fromFile) Then html = FileRead(html)
        stream.WriteText(html)
        mimeBody = doc.CreateMIMEEntity("Body")
        mimeHtml = mimeBody.CreateChildEntity
        Call mimeHtml.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", MIME_ENCODING.ENC_QUOTED_PRINTABLE)
        Call stream.Close()

        ' add file attachments
        Dim i As Integer
        For i = 0 To UBound(attachments)
            mimeFile = mimeBody.CreateChildEntity
            mimeHeader = mimeFile.CreateHeader("Content-Transfer-Encoding")
            Call mimeHeader.SetHeaderVal("binary")
            mimeHeader = mimeFile.CreateHeader("Content-Disposition")
            Call mimeHeader.SetHeaderVal("attachment; filename=" & attachments(i))
            Call stream.Open(attachments(i), "binary")
            Call mimeFile.SetContentFromBytes(stream, "text/plain", MIME_ENCODING.ENC_NONE)
            Call mimeFile.EncodeContent(MIME_ENCODING.ENC_IDENTITY_8BIT)
            Call stream.Close()
        Next

        ' add images referenced by cid tags
        For i = 0 To UBound(imageFiles)
            mimeImage = mimeBody.CreateChildEntity()
            mimeHeader = mimeImage.CreateHeader("Content-ID")
            Call mimeHeader.SetHeaderVal("<" & imageIds(i) & ">")
            Call stream.Open(imageFiles(i))
            Call mimeImage.SetContentFromBytes(stream, imageTypes(i) & "; name=" + imageIds(i), MIME_ENCODING.ENC_IDENTITY_BINARY)
            Call stream.Close()
        Next

        sess.convertMime = convertMime
        Call doc.CloseMIMEEntities(True, "Body")
        Call doc.Send(False)

    End Sub


    Public Function FileRead(ByVal path As String) As String

        '/**
        ' * Reads an entire file into a string.
        ' * @param path The path to the file to read.
        ' * @return A string containing the contents of the specified file.
        ' */

        Dim fileNum As Integer
        fileNum = FreeFile
    Open path For Input As fileNum
        FileRead = Input$(LOF(fileNum), fileNum)
        Close(fileNum)

    End Function

SendTo is OK it is equal to text from textbox, Subject is OK it is equal to text from textbox.
I have to add that body text is =Textbox3.Text
and attachment =FileUpload1.PostedFile.FileName  (this is path to file).
Where do I have to add this I dont know?

Also I have problem with those lines of code:
    Open path For Input As fileNum
        FileRead =  Input$(LOF(fileNum), fileNum)
        Close(fileNum)

I guess because this is VB6 code and can not be writen this way in ASP.NET.
0
 
kahvedzicAuthor Commented:
Only thing I can do is to either send text in mail or to change code and send only attachment in mail body. I did not manage to send both in one mail, and I must be able to do that. Can you please help me make this thing working in my asp.net app?
0
 
mbonaciCommented:
Read this recent post, all you need is explained there:
http://www.e-e.com/Q_25395944.html
0
 
kahvedzicAuthor Commented:
i did not know it is this hard just to compose mail and attach file in asp.net. Still did not manage to make it work. Kind busy this and next week, working on something else, but any way I will try and post again. Thanks for help.
0
 
kahvedzicAuthor Commented:
I decided for now to make form only for sending text in LN mail, no attachments, just text in subject and in body. Can you provide me with some asp.net code for this, I would really appreciate it? Thanks mbonaci.
0
 
mbonaciCommented:
I modified previously posted code.

Here are the details of the NotesDocument.Send method:
http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/topic/com.ibm.designer.domino.main.doc/H_SEND_METHOD_DOC.html
Sub ComposeNotesMail()
	Dim oSess As Object
	Dim oMailDB As Object
	Dim oMailDoc As Object
	Dim oBody As Object

	Dim ws As Object
	Dim uidoc

	Dim mailsvr
	Dim mailfile

	Set oSess = CreateObject("Notes.NotesSession")

	mailsvr = oSess.GetEnvironmentString("MailServer", True)
	mailfile = LCase(oSess.GetEnvironmentString("MailFile", True))

	Set oMailDB = oSess.GETDATABASE("", "")
	Call oMailDB.open(mailsvr, mailfile)

	If oMailDB.isopen Then
		Set oMailDoc = oMailDB.CREATEDOCUMENT
	Else
		MsgBox "Could not open Notes", 32
	End If

	oMailDoc.subject = "Test mail"
	oMailDoc.Form = "Memo"

	Set oBody = oMailDoc.CREATERICHTEXTITEM("BODY")

	Call oBody.Embedobject(1454, "", "c:\Testattachment.htm")

	Call oMailDoc.Send(False)

	Set oSess = Nothing
	Set oMailDB = Nothing
	Set oMailDoc = Nothing
	Set ws = Nothing
	Set uidoc = Nothing
End Sub

Open in new window

0
 
kahvedzicAuthor Commented:
This is my code behind, I can only send attachments from att.text, but I can not send text from body.text textbox, how to send both in one mail (some text + attachment from my computer)? I also add doc.SaveMessageOnSend = True but mails that I send from my application are not saved in sent folder of LN client, I dont know why? Thanks

Imports Domino
Imports System.Data.SqlClient

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Sub ComposeNotesMail()

        Dim sess As New NotesSession
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        Dim body As Object
        Dim mailServer As String, mailFile As String


        ' Init Notes session
        Call sess.Initialize("pass")

        ' Open user's email
        mailServer = sess.GetEnvironmentString("MailServer", True)
        mailFile = sess.GetEnvironmentString("MailFile", True)
        db = sess.GetDatabase(mailServer, mailFile)

        If db.IsOpen Then
            doc = db.CreateDocument
        Else
            MsgBox("Could not open Notes", 32)
        End If

        ' Create an email doc
        doc = db.CreateDocument
        Call doc.ReplaceItemValue("SendTo", adresa.Text.Trim)
        Call doc.ReplaceItemValue("Subject", subject.Text.Trim)
        Call doc.ReplaceItemValue("Form", "Memo")
        body = doc.CreateRichTextItem("BODY")

        Call body.Embedobject(1454, "", att.Text) '"c:\Testattachment.txt")

        Call doc.Send(False)

        doc.SaveMessageOnSend = True

        sess = Nothing
        db = Nothing
        doc = Nothing

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComposeNotesMail()
    End Sub
End Class

Open in new window

0
 
kahvedzicAuthor Commented:
Thanks man, great, works like a charm.

Cheers.

This is my code behind.

Sub ComposeNotesMail()

        Dim sess As New NotesSession
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        Dim body As Object
        Dim mailServer As String, mailFile As String


        ' Init Notes session
        Call sess.Initialize("frimejl")

        ' Open user's email
        mailServer = sess.GetEnvironmentString("MailServer", True)
        mailFile = sess.GetEnvironmentString("MailFile", True)
        db = sess.GetDatabase(mailServer, mailFile)

        If db.IsOpen Then
            doc = db.CreateDocument
        Else
            MsgBox("Could not open Notes", 32)
        End If

        ' Create an email doc
        doc = db.CreateDocument
        Call doc.ReplaceItemValue("Form", "Memo")
        Call doc.ReplaceItemValue("SendTo", adresa.Text.Trim)
        Call doc.ReplaceItemValue("CopyTo", cc.Text.Trim)
        Call doc.ReplaceItemValue("Subject", subject.Text.Trim)

        body = doc.CreateRichTextItem("BODY")
        Call body.AppendText(sadrzaj.Text)
        Call body.AddNewLine()
        Call body.AddNewLine()
        Call body.AddNewLine()

        Call body.Embedobject(1454, "", att.Text) '"c:\Testattachment.txt")

        Call doc.Send(False)

        doc.SaveMessageOnSend = True

        sess = Nothing
        db = Nothing
        doc = Nothing

    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.