delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


Send email in console modeGo to Arman Rad's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Send email in console mode
Product:
Delphi 6.x (or higher)
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
03/16/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet "Console" + "Email"
Times Scored:
2
Visits:
6023
Uploader: Arman Rad
Company: RadNet
Reference: ArmanHacker@delphi3000.com
 
Question/Problem/Abstract:
How can I Send email in console mode?
Answer:



You can use this unit for do it :

unit mapimail;
// --------------------------------------------------------------
// Simple wrapper for Simple MAPI.  Sends an email with an
// optional file attachment using the client's MAPI client, e.g.
// Outlook or NS Communicator.  Very simple implementation
// limited to one target and one attachment.
// --------------------------------------------------------------

interface

uses
   sysutils, dialogs, forms, Windows, MAPI;

type
   tMailFileException = class( Exception );
const
   ERROR = 'Unable to send email message.' + #10#10 + 'Reason: %s.';

function sendMail( const TargetName, TargetAddr,
                         SenderName, SenderAddr,
                         MsgSubject, MsgContent,
                         Attachment : String;
                         PreviewMsg : Boolean = TRUE ) : Integer;
function getMAPIError( intErrorCode : Integer ) : String;

implementation

function sendMail( const TargetName, TargetAddr,
                         SenderName, SenderAddr,
                         MsgSubject, MsgContent,
                         Attachment : String;
                         PreviewMsg : Boolean = TRUE ) : Integer;
var
  msg       : TMapiMessage;    // Pointer to the message itself
  mrdSender,                   // Who's sending it?
  mrdTarget : TMapiRecipDesc;  // Who's going to get it?
  mfdAttach : TMapiFileDesc;   // The attached file.
  liFlags   : Longint;         // Flags for MAPI.
  strError  : String;          // Holds MAPI error results;
begin
result := 0;
liFlags := 0;
fillChar( msg, sizeOf( msg ), 0 );
with msg do
begin

   if TargetAddr = '' then
      raise tMailFileException.createFmt( ERROR,
         [ 'Target email address not specified' ] )
   else begin
      if TargetName = '' then
         mrdTarget.lpszName := pChar( TargetAddr )
      else
         mrdTarget.lpszName := pChar( TargetName );
      mrdTarget.ulRecipClass := MAPI_TO;
      mrdTarget.lpszAddress := pChar( TargetAddr );
      mrdTarget.ulReserved := 0;
      mrdTarget.ulEIDSize := 0;
      mrdTarget.lpEntryID := NIL;
      nRecipCount := 1;
      lpRecips := @mrdTarget;

   end;

   if SenderAddr = '' then
      raise tMailFileException.createFmt( ERROR,
         [ 'Sender email address not specified' ] )
   else begin
      if SenderName = '' then
         mrdSender.lpszName := pChar( SenderAddr )
      else
         mrdSender.lpszName := pChar( SenderName );
      mrdSender.ulRecipClass := MAPI_ORIG;
      mrdSender.lpszAddress := pChar( 'SMTP:' + SenderAddr );
      mrdSender.ulReserved := 0;
      mrdSender.ulEIDSize := 0;
      mrdSender.lpEntryID := NIL;
      lpOriginator := @mrdSender;
   end;

   if MsgSubject = '' then
      lpszSubject := ''
   else
      lpszSubject := pChar( MsgSubject );

   if ( MsgContent = '' ) AND ( Attachment = '' ) then
      raise tMailFileException.createFmt( ERROR,
         [ 'Tried to send an empty message (no content or attachment)' ] )
   else begin
      if MsgContent = '' then
         lpszNoteText := 'Please see the attached file.'
      else
         lpszNoteText := pChar( MsgContent );

      if Attachment = '' then begin
         nFileCount := 0;
         lpFiles := NIL;
      end else begin
         fillChar( mfdAttach, sizeOf( mfdAttach ), 0 );
         mfdAttach.nPosition := cardinal( $FFFFFFFF );
         mfdAttach.lpszPathName := pChar( Attachment );
         nFileCount := 1;
         lpFiles := @mfdAttach;
      end;
   end; // with

   if previewMsg then liFlags := MAPI_DIALOG;

   result := mapiSendMail( 0, application.Handle, msg, liFlags, 0 );

   if result <> 0 then
      raise tMailFileException.createFmt( ERROR,
         [ 'MAPI triggered an error ('
           + getMAPIError( result ) + ')' ] );
  end;

end;

function getMAPIError( intErrorCode : Integer ) : String;
begin

   result := 'Unknown Error Code: ' + intToStr( intErrorCode );
   case intErrorCode of
     MAPI_E_USER_ABORT               : result := 'User cancelled request';
     MAPI_E_FAILURE                  : result := 'General MAPI failure';
     MAPI_E_LOGON_FAILURE            : result := 'Logon failure';
     MAPI_E_DISK_FULL                : result := 'Disk full';
     MAPI_E_INSUFFICIENT_MEMORY      : result := 'Insufficient memory';
     MAPI_E_ACCESS_DENIED            : result := 'Access denied';
     MAPI_E_TOO_MANY_SESSIONS        : result := 'Too many sessions';
     MAPI_E_TOO_MANY_FILES           : result := 'Too many files open';
     MAPI_E_TOO_MANY_RECIPIENTS      : result := 'Too many recipients';
     MAPI_E_ATTACHMENT_NOT_FOUND     : result := 'Attachment not found';
     MAPI_E_ATTACHMENT_OPEN_FAILURE  : result := 'Failed to open attachment';
     MAPI_E_ATTACHMENT_WRITE_FAILURE : result := 'Failed to write attachment';
     MAPI_E_UNKNOWN_RECIPIENT        : result := 'Unknown recipient';
     MAPI_E_BAD_RECIPTYPE            : result := 'Invalid recipient type';
     MAPI_E_NO_MESSAGES              : result := 'No messages';
     MAPI_E_INVALID_MESSAGE          : result := 'Invalid message';
     MAPI_E_TEXT_TOO_LARGE           : result := 'Text too large.';
     MAPI_E_INVALID_SESSION          : result := 'Invalid session';
     MAPI_E_TYPE_NOT_SUPPORTED       : result := 'Type not supported';
     MAPI_E_AMBIGUOUS_RECIPIENT      : result := 'Ambiguous recipient';
     MAPI_E_MESSAGE_IN_USE           : result := 'Message in use';
     MAPI_E_NETWORK_FAILURE          : result := 'Network failure';
     MAPI_E_INVALID_EDITFIELDS       : result := 'Invalid edit fields';
     MAPI_E_INVALID_RECIPS           : result := 'Invalid recipients';
     MAPI_E_NOT_SUPPORTED            : result := 'Not supported';
   end;
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Not exactly correct
    Tommy Andersen (Mar 19 2002 3:54PM)

The subject of your post is "Send email in console mode". Your example will not allways work in console mode. It will probably work in a standard Simple MAPI application.

The problem with your example is that you have not written the principles behind loging on and off MAPI.

A console app may not have a desktop to relate to (hence an NT service that isn't configured to work with a certain user/desktop).
Mapi is configured in a users profile, thus changing profile means also change of MAPI configuration.

To get Mapi to work in console mode, I guess you have to make sure that you are running on a certain user-profile. Searching delphi3000.com for an example on such things is probably a good start.

Best Regards
Tommy Andersen
EasyWare.org

Respond

RE: Not exactly correct
anonymus (Mar 21 2002 8:52PM)

A virus with name "Trojan"  written by delphi, this virus is worm type and it cans send mail to different addresses , now I want to know how it cans send email?  
and I want to know that can I use smtp component to send email in console mode?

Thank you
Arman Rad
Respond

RE: RE: Not exactly correct
Jason Goff (Apr 3 2002 11:05PM)

This code will not work in Windoze XP....MAPI is not supported.
Respond

RE: RE: RE: Not exactly correct
Tommy Andersen (Oct 13 2003 2:24PM)

Hi there,
Just reading the comment about the windows xp thing, and that isn't correct. The Windows Messaging (Mapi) part is not a part of Windows XP or Windows 2000 as they was in Windows NT 4.0, Win 95 & Win 98.
This does not mean that it's not supported or anything. By searching a bit on Microsofts web site, you can find the NTWMS.EXE installation which will install Mapi.

Outlook 2000 +++ does also install Mapi, but this is not the same as the good old one... The Mapi Spooler is removed in the latest versions of Outlook. Old transport providers might not work and so on.


Best Regards
Tommy Andersen
EasyWare.org

Respond

RE: RE: RE: RE: Not exactly correct
Gary (May 4 2005 4:14AM)

Hard to find on MS. Goto ftp://ftp.novell.com/pub/allupdates/ and download WinNTWMS.exe. Windows Messaging System(WMS) for NT4/2000/XP by MS V4.71
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Kucherov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)