Skip to main content

Problem:

How do I specify a user and password on an SMTP email send from .Net

Resolution:

To do this you need to use the "Fields" collection property of the MailMessage object. You can set the Authentication Type userid and password parameters.

An example program that does this is:-

      $set sourceformat(variable)   

      *> Allows past col 72   

       program-id. SendMail.

       environment division.

       configuration section.

       repository.

            class cMailMessage      as "System.Web.Mail.MailMessage"

            class cMailAttachment   as "System.Web.Mail.MailAttachment"

            class cSmtpMail         as "System.Web.Mail.SmtpMail"

            class cIList            as "System.Collections.IList"

            enum  eMailFormat       as "System.Web.Mail.MailFormat"

            class cExceptionClass   as "System.Exception"

            .

       data division.

       working-storage section.

       78  mailfrom                 value "someone@microfocus.net".

       78  mailto                   value "my.name@microfocus.com".

      *78  mailto                   value "someone@somewhere.com".

       78  SmtpServer               value "ansmtpserver.microfocus.com".

       78  mailsubject              value "Auto Generated from COBOL on .Net".

       78  mailbody                 value "This is Line 1." & x"0d0a" &

                                          "This is Line 2.".

       

       01  ws-mailmessage           object reference cMailMessage.

       

       01  ws-attachment            object reference cMailAttachment.

       

       

       procedure division.

       main-section section.

       

      ***** Build the message

       

           invoke cMailMessage::"New" returning ws-mailmessage.

           set ws-mailmessage::"From"       to mailfrom

           set ws-mailmessage::"To"         to mailto

           set ws-mailmessage::"Subject"    to mailsubject

           set ws-mailmessage::"Body"       to mailbody

           set ws-mailmessage::"BodyFormat" to eMailFormat::"Text"

            

           set ws-attachment to cMailAttachment::"New"("c:\\test.txt")

           invoke ws-mailmessage::"Attachments"::"Add"(ws-attachment)

      ***** The Next Line would use NTLM Authentication (Current Logged on User)

      *    invoke ws-mailmessage::"Fields"::"Add"("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",2)

      ***** Use Basic Authentication

           invoke ws-mailmessage::"Fields"::"Add"("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1)

           invoke ws-mailmessage::"Fields"::"Add"("http://schemas.microsoft.com/cdo/configuration/sendusername","myuserid")

           invoke ws-mailmessage::"Fields"::"Add"("http://schemas.microsoft.com/cdo/configuration/sendpassword","mypassword")

           

      ***** Setup details of Mail Server

      

           set cSmtpMail::"SmtpServer" to SmtpServer

           

           invoke cSmtpMail::"Send"(ws-mailmessage)

           

           goback.

Old KB# 3788