CDOSys Mail Script Example
Here is a working example of -----------====CDOSYS====---------- without the opening ASP tags:
- Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
- Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
- Const cdoAnonymous = 0 'Do not authenticate
- Const cdoBasic = 1 'basic (clear-text) authentication
- Const cdoNTLM = 2 'NTLM
- Set objMessage = Server.CreateObject("CDO.Message")
- objMessage.Subject = "Example CDO Message"
- objMessage.Sender = "me@mydomain.com"
- objMessage.From = "me@mydomain.com"
- objMessage.To = "recipient@test.com"
- objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
- 'This section provides the configuration information for the remote SMTP server.
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
- 'Name or IP of Remote SMTP Server
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"
- 'Type of authentication, NONE, Basic (Base64 encoded), NTLM
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
- 'Your UserID on the SMTP server
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "you@your.com"
- 'Your password on the SMTP server
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password_of_you@your.com"
- 'Server port (typically 25)
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
- 'Use SSL for the connection (False or True)
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
- 'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
- objMessage.Configuration.Fields.Update
- objMessage.Send
Here is a working example of -----------====CDOSYS (with simple asp.net) or System.Net.Mail in C# ====---------- without the opening ASP tags:
- //using System.Net.Mail;
- public string SendEmail(string in_from, string in_to, string in_subject, string in_body, byte[] attachmentBytes, string attachmentName)
- {
- string errorMessage;
- try
- {
- //create the mail message
- MailMessage mail = new MailMessage();
- SmtpClient smtp = new SmtpClient("mail.DOMAINNAME.com");
- System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(in_from, "in_froms_PASSWORD");
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = SMTPUserInfo;
- //set the addresses
- mail.From = new MailAddress(in_from);
- mail.To.Add(in_to);
- //set the content
- mail.Subject = in_subject;
- mail.Body = in_body;
- mail.IsBodyHtml = true;
- //set the attachment
- if (attachmentBytes != null)
- {
- MemoryStream ms = new MemoryStream(attachmentBytes);
- mail.Attachments.Add(new Attachment(ms, attachmentName));
- }
- //send the message
- smtp.Send(mail);
- errorMessage = "";
- }
- catch (Exception err)
- {
- errorMessage = "An error has occurred in sending the email. Please contact support.<br />" + err.Message.ToString();
- }
- return errorMessage;
- }