cancel
Showing results for 
Search instead for 
Did you mean: 

How to Send Email Using DIAPI

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Dear All,

I have a requirement from one of my client that as soon as a Sales Order is created in SAP B1. I want to send an email to the concerned person's email-id. The email should use the same functionality as it happens in SAP internally. I think it uses some messaging service or something else. But main requirement is that I don't want to hardcode the SMTP Server address. I want to pick it up everytime from the SBO Mailer whatever it contains. All I need to do this using DIAPI.

Can you please help me in this ? How can I achieve this ?

Any kind of help would be appreciated.

Thanks & Regards

Ankit Chauhan

Accepted Solutions (1)

Accepted Solutions (1)

edy_simon
Active Contributor
0 Kudos

Hi Ankit,

You can use SBO Mailer to send out the message.

No need to have your own SMTP client.

Just make sure your SBO Mailer is setup and you can send out email from SBO manually.

I use below function (in vb) to send emails with attachment from SBO Mailer.

        Private Function SendSBOEmail(ByVal sCardCode As String, ByVal ToCP As String, ByVal ToEmail As String, ByVal _subject As String, ByVal _message As String, ByVal Attachment As String) As String

            Dim objMsg As SAPbobsCOM.Messages = Nothing

            Dim lngStatus As Integer

            Dim lErrCode As Integer

            Dim sErrMsg As String

            Dim sAttachPath As String = GetAttachmentPath()

            'Check on the attachment exists in target path ?

            'If exists have to modify the attachment filename

            If sAttachPath <> "" Then

                Dim FI As New System.IO.FileInfo(Attachment)

                If IO.File.Exists(sAttachPath & FI.Name) Then

                    'Rename the original file

                    Dim NewName As String = ""

                    Dim AttachmentExOnly As String = FI.Extension

                    Dim AttachmentFNOnly As String = FI.Name.Substring(0, FI.Name.Length - FI.Extension.Length)

                    For i As Integer = 1 To 100

                        NewName = String.Format("{0}_{1}{2}", AttachmentFNOnly, i, AttachmentExOnly)

                        If Not IO.File.Exists(sAttachPath & NewName) Then

                            Exit For

                        End If

                    Next

                    'Duplicate the Attachment to the NewName

                    'If success redirect the attachment to the new name else keep the old attachment

                    Try

                        IO.File.Copy(Attachment, FI.DirectoryName & "\" & NewName)

                        Attachment = FI.DirectoryName & "\" & NewName

                    Catch ex As Exception

                    End Try

                End If

            End If

            Try

                objMsg = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oMessages)

                objMsg.Subject = _subject

                objMsg.MessageText = _message

                Dim SendTos As String()

                SendTos = ToEmail.Split(";")

                Dim iRcpt As Integer = 0

                For Each SendTo As String In SendTos

                    objMsg.Recipients.Add()

                    objMsg.Recipients.SetCurrentLine(iRcpt)

                    objMsg.Recipients.UserCode = sCardCode

                    objMsg.Recipients.NameTo = ToCP

                    objMsg.Recipients.UserType = BoMsgRcpTypes.rt_ContactPerson

                    objMsg.Recipients.SendInternal = SAPbobsCOM.BoYesNoEnum.tNO

                    objMsg.Recipients.SendEmail = SAPbobsCOM.BoYesNoEnum.tYES

                    objMsg.Recipients.EmailAddress = SendTo.ToString.ToLower

                    iRcpt += 1

                Next

                objMsg.Priority = SAPbobsCOM.BoMsgPriorities.pr_High

                objMsg.Attachments.Add()

                objMsg.Attachments.Item(0).FileName = Attachment

                lngStatus = objMsg.Add

                If lngStatus <> 0 Then

                    sErrMsg = oCompany.GetLastErrorDescription

                    oCompany.GetLastError(lErrCode, sErrMsg)

                    Return sErrMsg

                Else

                    Dim sCode As String = oCompany.GetNewObjectKey

                    Return sCode

                End If

            Catch ex As Exception

                'B1Connections.theAppl.MessageBox(ex.Message)

                Return ex.Message

            Finally

                ReleaseComOBject(objMsg)

            End Try

        End Function

Regards

Edy

Former Member
0 Kudos

Hi Edy,

thanks for the code sample.

I needed this earlier today and it seems to work fine (although with a slight delay before the items pop up in the messages overview, but that's the internals of SAP).

0 Kudos

Hi,

I was using your code which i converted to c# to send a document by email - it works perfectly, thank you.

I have an issue, if I send an email with the attached document PDF in SAP client, it creates a row in OEML and in EML1 which then "marks" the document as sent so user knows the document was already sent by email.

however if I send the message via DIAPI there's no record added on OEML table (nor EML1). I look at the message object properties in SDK but found no obvious property to make this happen by associating the document

any ideas ? is this even possible ?

Thank in advance

Answers (2)

Answers (2)

edy_simon
Active Contributor
0 Kudos

Hi,

Did you get what you need?

Please close when you are done.

Regards,

Edy

former_member201110
Active Contributor
0 Kudos

Hi Ankit,

The SMTP settings from the Mailer are unfortunately not held in the database. They are stored in an XML file on the server called b1-local-machine.xml (default path is C:\Program Files (x86)\SAP\SAP Business One ServerTools\Conf). You could get your program to read this file when it needs the SMTP details but it's probably easier just to build the ability to store the SMTP details in the SBO-COMMON or company database in a new table (or in your own config file if it's a server-side program).

When you send an email from within SBO, it stores the details in SQL tables in the company database. The Mailer will then process this message next time it runs.

In order for your program to send out emails via SMTP, you have two choices:

1) The .NET Framework includes objects for sending out emails via SMTP. Personally I prefer this method because the SBO Mailer isn't 100% reliable so it's simpler to bypass this. The downside is that the user doesn't see the message going out from their Outbox in SBO.

2) The DI API has objects for sending out messages, including via email. The main object is the MessagesService. There are samples in the SDK documentation on how to use this to send messages.

Kind Regards,

Owen