before 5.0 was localized for Turkey, i have created this cu for nav 4.03 to send email.I have this codeunit and "SMTP Mail Setup" table. this codeunit uses native CDO component of Windows. therefore, you don't need any additional component. also, it has SSL support. (smtp dll introduced in NAV 5.0 does allow SSL or port setting). you can set gmail account settings in "SMTP Mail Setup" table. this works perfect for you..first, fill SMTP setup table. call CreateMessage function from anywhere you want,then call Send function.
btw..If I were you, i would configure a simple machine in your office as SMTP relay server and use it to send mail from NAV. i like to work in this way:
1. enable/setup SMTP service of IIS in one of simple computer. (Operating system is not important). you can do this adding windows component. you migth need windows CD.
2. in the delivery tab of SMTP server properties dialog, set outbound security and "Smart Host" for gmail..(just use help for those settings or press F1)
3. in NAV, use your simple SMTP server to send mail.
in this way,
1. NAV sends email to your SMTP server as quickly as possible.. your SMTP server gets email from NAV..
2. NAV and user suppose that mail is sent.
3. your SMTP server transfers the email to gmail seperately because you set "Smart Host" for this SMTP server.( just google "smart host IIS")
so, internet connection cut or huge attachments etc will not be problem. i think that this is the best way to send email from NAV in posting procedure.
best regards
OBJECT Table 60000 SMTP Mail Setup
{
OBJECT-PROPERTIES
{
Date=06.06.07;
Time=23:06:26;
Version List=6GENCore1.3.1;
}
PROPERTIES
{
CaptionML=[ENU=SMTP Mail Setup;
TRK=SMTP Posta Kurulumu];
}
FIELDS
{
{ 1 ; ;Primary Key ;Code10 ;CaptionML=[ENU=Primary Key;
TRK=Birincil Anahtar] }
{ 2 ; ;SMTP Server ;Text250 ;CaptionML=[ENU=SMTP Server;
TRK=SMTP Sunucusu] }
{ 3 ; ;Authentication ;Option ;OnValidate=BEGIN
IF Authentication <> Authentication::"2" THEN BEGIN
"User ID" := '';
Password := '';
END;
END;
CaptionML=[ENU=Authentication;
TRK=Yetkilendirme];
OptionCaptionML=[ENU=Anonymous,NTLM,Basic;
TRK=Anonim,NTLM,Basit];
OptionString=Anonymous,NTLM,Basic }
{ 4 ; ;User ID ;Text30 ;OnValidate=BEGIN
TESTFIELD(Authentication,Authentication::"2");
END;
CaptionML=[ENU=User ID;
TRK=Kullanc Kimli§i] }
{ 5 ; ;Password ;Text30 ;OnValidate=BEGIN
TESTFIELD(Authentication,Authentication::"2");
END;
CaptionML=[ENU=Password;
TRK=ifre] }
{ 6 ; ;Use SSL ;Boolean ;CaptionML=[ENU=Use SSL;
TRK=SSL Kullan] }
{ 7 ; ;Server Port ;Integer ;CaptionML=[ENU=Server Port;
TRK=Sunucu Ba§lat Noktas];
MinValue=1;
MaxValue=65000 }
{ 8 ; ;Connection Timeout ;Integer ;CaptionML=[ENU=Connection Timeout;
TRK=Ba§lant Sre AŸm] }
}
KEYS
{
{ ;Primary Key ;Clustered=Yes }
}
CODE
{
BEGIN
{
}
END.
}
}
OBJECT Codeunit 60000 SMTP Mail 4.X
{
OBJECT-PROPERTIES
{
Date=04.07.09;
Time=04:11:28;
Modified=Yes;
Version List=6GENCore1.3.1;
}
PROPERTIES
{
Permissions=TableData 50002084=r;
OnRun=BEGIN
END;
}
CODE
{
VAR
SMTPMailSetup@1003 : Record 60000;
Text001@1001 : TextConst
'ENU=The email address "%1" is invalid.';
Text002@1002 : TextConst
'ENU=Attachment %1 does not exist or can not be accessed from the program.';
Text003@1005 : TextConst
'ENU=The SMTP mail system returned the following error: %1';
Mail@1000000000 : Automation "{CD000000-8B95-11D1-82DB-00C04FB1625D} 1.0:{CD000001-8B95-11D1-82DB-00C04FB1625D}:'Microsoft CDO for Windows 2000 Library'.Message";
MailBody@1000000001 : Automation "{CD000000-8B95-11D1-82DB-00C04FB1625D} 1.0:{CD000021-8B95-11D1-82DB-00C04FB1625D}:'Microsoft CDO for Windows 2000 Library'.IBodyPart";
ADODBStream@1000000006 : Automation "{EF53050B-882E-4776-B643-EDA472E8E3F2} 2.7:{00000566-0000-0010-8000-00AA006D2EA4}:'Microsoft ActiveX Data Objects 2.7 Library'.Stream";
MailBody2@1000000002 : Automation "{CD000000-8B95-11D1-82DB-00C04FB1625D} 1.0:{CD000021-8B95-11D1-82DB-00C04FB1625D}:'Microsoft CDO for Windows 2000 Library'.IBodyPart";
PROCEDURE CreateMessage@1(SenderName@1008 : Text[100];SenderAddress@1000 : Text[50];Recipients@1001 : Text[1024];Subject@1004 : Text[200];Body@1000000001 : Text[1024];HTMLFormatted@1000000000 : Boolean);
VAR
l@1000000002 : Text[30];
BEGIN
IF Recipients <> '' THEN
CheckValidEmailAddresses(Recipients);
CheckValidEmailAddresses(SenderAddress);
SMTPMailSetup.GET;
SMTPMailSetup.TESTFIELD("SMTP Server");
IF ISCLEAR(Mail) THEN
CREATE(Mail);
IF ISCLEAR(ADODBStream) THEN
CREATE(ADODBStream);
Mail.From := SenderName + '<' + SenderAddress + '>';
Mail."To" := Recipients;
Mail.Subject := Subject;
MailBody := Mail.BodyPart;
MailBody2 := MailBody.AddBodyPart;
MailBody2.Fields.Item('urn:schemas:mailheader:content-type').Value := 'text/plain; charset="iso-8859-9"';
MailBody2.Fields.Update;
MailBody2 := MailBody.AddBodyPart;
MailBody2.Fields.Item('urn:schemas:mailheader:content-type').Value := 'text/html; charset="iso-8859-9"';
MailBody2.Fields.Update;
MailBody.Fields.Item('urn:schemas:mailheader:content-type').Value := 'multipart/alternative';
MailBody.Fields.Update;
IF HTMLFormatted THEN
MailBody2 := MailBody.BodyParts.Item(2)
ELSE
MailBody2 := MailBody.BodyParts.Item(1);
ADODBStream := MailBody2.GetDecodedContentStream;
ADODBStream.WriteText(Body);
END;
PROCEDURE Send@3();
VAR
Schema@1000000000 : Text[150];
BEGIN
WITH SMTPMailSetup DO BEGIN
ADODBStream.Flush;
Schema := 'http://schemas.microsoft.com/cdo/configuration/';
Mail.Configuration.Fields.Item(Schema + 'sendusing').Value := 2;
Mail.Configuration.Fields.Item(Schema + 'smtpserver').Value := "SMTP Server";
Mail.Configuration.Fields.Item(Schema + 'smtpserverport').Value := "Server Port";
CASE Authentication OF
SMTPMailSetup.Authentication::Anonymous: Mail.Configuration.Fields.Item(Schema + 'smtpauthenticate').Value := 0;
SMTPMailSetup.Authentication::NTLM: Mail.Configuration.Fields.Item(Schema + 'smtpauthenticate').Value := 2;
SMTPMailSetup.Authentication::Basic: Mail.Configuration.Fields.Item(Schema + 'smtpauthenticate').Value := 1;
END;
Mail.Configuration.Fields.Item(Schema + 'sendusername').Value := "User ID";
Mail.Configuration.Fields.Item(Schema + 'sendpassword').Value := Password;
IF "Use SSL" THEN
Mail.Configuration.Fields.Item(Schema + 'smtpusessl').Value := 1
ELSE
Mail.Configuration.Fields.Item(Schema + 'smtpusessl').Value := 0;
Mail.Configuration.Fields.Update;
Mail.Send
END;
CLEAR(Mail);
CLEAR(ADODBStream);
END;
PROCEDURE AddRecipients@4(Recipients@1000 : Text[1024]);
BEGIN
CheckValidEmailAddresses(Recipients);
Mail."To" := Mail."To" + ';' + Recipients;
END;
PROCEDURE AddCC@5(Recipients@1000 : Text[1024]);
BEGIN
CheckValidEmailAddresses(Recipients);
Mail.CC := Mail.CC + ';' + Recipients;
END;
PROCEDURE AddBCC@6(Recipients@1000 : Text[1024]);
BEGIN
CheckValidEmailAddresses(Recipients);
Mail.BCC := Mail.BCC + ';' + Recipients;
END;
PROCEDURE AppendBody@7(BodyPart@1000 : Text[1024];HtmlFormatted@1000000000 : Boolean);
VAR
Str@1000000001 : OutStream;
FSO@1000000002 : Automation "{420B2830-E718-11CF-893D-00A0C9054228} 1.0:{C7C3F5B5-88A3-11D0-ABCB-00A0C90FFFC0}:'Microsoft Scripting Runtime'.File";
BEGIN
ADODBStream.WriteText(BodyPart);
END;
PROCEDURE AddAttachment@2(Attachment@1000 : Text[260]);
BEGIN
IF Attachment = '' THEN
EXIT;
IF NOT EXISTS(Attachment) THEN
ERROR(Text002,Attachment);
Mail.AddAttachment(Attachment);
END;
LOCAL PROCEDURE CheckValidEmailAddresses@8(Recipients@1000 : Text[1024]);
VAR
s@1002 : Text[1024];
BEGIN
IF Recipients = '' THEN
ERROR(Text001,Recipients);
s := Recipients;
WHILE STRPOS(s,';') > 1 DO BEGIN
CheckValidEmailAddress(COPYSTR(s,1,STRPOS(s,';') - 1));
s := COPYSTR(s,STRPOS(s,';') + 1);
END;
CheckValidEmailAddress(s);
END;
LOCAL PROCEDURE CheckValidEmailAddress@12(EmailAddress@1000 : Text[250]);
VAR
i@1001 : Integer;
NoOfAtSigns@1002 : Integer;
BEGIN
IF EmailAddress = '' THEN
ERROR(Text001,EmailAddress);
IF (EmailAddress[1] = '@') OR (EmailAddress[STRLEN(EmailAddress)] = '@') THEN
ERROR(Text001,EmailAddress);
FOR i := 1 TO STRLEN(EmailAddress) DO BEGIN
IF EmailAddress[i] = '@' THEN
NoOfAtSigns := NoOfAtSigns + 1;
IF NOT (
((EmailAddress[i] >='a') AND (EmailAddress[i] <='z')) OR
((EmailAddress[i] >='A') AND (EmailAddress[i] <='A')) OR
((EmailAddress[i] >='0') AND (EmailAddress[i] <='9')) OR
(EmailAddress[i] IN ['@','.','-','_']))
THEN
ERROR(Text001,EmailAddress);
END;
IF NoOfAtSigns <> 1 THEN
ERROR(Text001,EmailAddress);
END;
BEGIN
END.
}
}
I have had difficulty sending email via Outlook Express the message I receive is "Either there is no default mail client or the current mail client can not fulfill the message request. Please run Microsoft Office Outlook and set it as the default mail client." I have both OE and the full Outlook client installed and can send email directly from either. I would prefer to use OE but when I try to use, for example, the "New Mail Message" button on the communication tab of form 5200 (other forms too) I receive the message above if OE is set as my default email client. I'm thinking that code in codeunit 397 is forcing the use of the full Outlook client but I'm not an experienced programmer. Is there some way to allow the use of OE for basic off the shelf functionality?
I'm running 5.0 SP1 SQL Server option on SQL Server 2K5. Form 5200 is customized as is codeunit 397 by Serenic Corporation. Codeunit 400 is not customized.
Comments
btw..If I were you, i would configure a simple machine in your office as SMTP relay server and use it to send mail from NAV. i like to work in this way:
1. enable/setup SMTP service of IIS in one of simple computer. (Operating system is not important). you can do this adding windows component. you migth need windows CD.
2. in the delivery tab of SMTP server properties dialog, set outbound security and "Smart Host" for gmail..(just use help for those settings or press F1)
3. in NAV, use your simple SMTP server to send mail.
in this way,
1. NAV sends email to your SMTP server as quickly as possible.. your SMTP server gets email from NAV..
2. NAV and user suppose that mail is sent.
3. your SMTP server transfers the email to gmail seperately because you set "Smart Host" for this SMTP server.( just google "smart host IIS")
so, internet connection cut or huge attachments etc will not be problem. i think that this is the best way to send email from NAV in posting procedure.
best regards
I'm running 5.0 SP1 SQL Server option on SQL Server 2K5. Form 5200 is customized as is codeunit 397 by Serenic Corporation. Codeunit 400 is not customized.