Simple Example:
This example shows just how easy it is to send email with Dimac w3 JMail.
<%
' Create the JMail message Object
set msg = Server.CreateOBject( "JMail.Message" )
' Set logging to true to ease any potential debugging
' And set silent to true as we wish to handle our errors ourself
msg.Logging = true
msg.silent = true
' Most mailservers require a valid email address
' for the sender
msg.From = "test@mydomain.com"
msg.FromName = "My Realname"
' Next we have to add some recipients.
' The addRecipients method can be used multiple times.
' Also note how we skip the name the second time, it
' is as you see optional to provide a name.
msg.AddRecipient "recipient@hisDomain.com", "His Name"
msg.AddRecipient "recipientelle@herDomain.com"
' The subject of the message
msg.Subject = "How you doin?"
' The body property is both read and write.
' If you want to append text to the body you can
' use JMail.Body = JMail.Body & "Hello world! "
' or you can use JMail.AppendText "Hello World! "
' which in many cases is easier to use.
'
' Note the use of vbCrLf to add linebreaks to our email
msg.Body = "Hello Jim" & vbCrLf & vbCrLf & "How's it going? ..."
' There.. we have now succesfully created our message.
' Now we can either send the message or save it
' as a draft in a Database.
' To save the message you would typicly use the
' Message objects Text property
' to do something like this:
'
' SaveMessageDraft( msg.Text )
' Note that this function call is only an
' example. The function does not exist by
' default, you have to create it yourself.
' To send the message, you use the Send()
' method, which takes one parameter that
' should be your mailservers address
'
' To capture any errors which might occur,
' we wrap the call in an IF statement
if not msg.Send("mail.myDomain.net" ) then
Response.write "<pre>" & msg.log & "</pre>"
else
Response.write "Message sent succesfully!"
end if
' And we're done! the message has been sent.
%>
Sending email in HTML:
With Dimac w3 JMail you can send your emails in HTML-format. When doing that you can also have images inside your email and not just attach them.
<%
Set jmail = Server.CreateObject("JMail.Message")
jmail.AddRecipient "myRecipient@hisdomain.com", "Mr.Example"
jmail.From = "me@mydomain.com"
jmail.Subject = "Here's some graphics!"
jmail.Body = "A nice picture if you can read HTML-mail."
' The return value of AddAttachment is used as a
' reference to the image in the HTMLBody.
contentId = jmail.AddAttachment("c:\myCoolPicture.gif")
' As only HTML formatted emails can contain inline images
' we use HTMLBody and appendHTML
jmail.HTMLBody = "<html><body><font color=""red"">Hi, here is a nice picture:</font><br>"
jmail.appendHTML "<img src=""cid:" & contentId & """>"
jmail.appendHTML "<br><br>good one huh?</body></html>"
' But as not all mailreaders are capable of showing HTML emails
' we will also add a standard text body
jmail.Body = "Too bad you can't read HTML-mail."
jmail.appendText " There would have been a nice picture for you"
jmail.Send( "mailserver.mydomain.com" )
%>
Using Forms:
|
One of the most common uses for Dimac w3 JMail is when a form is filled out by the user and then emailed by ASP. This example shows you just how easy it is.
|
First of all we need a form which posts to our JMail page. |
<html>
<head>
<title>emailform</title>
</head>
<body>
<form method="post" action="SendMail.asp">
Complete this form and click the submit-button. We will answer your
questions as soon as possible.
<br><br>
Your name<br>
<input type="text" size="25" name="name"><br>
Your email<br>
<input type="text" size="25" name="email"><br> Recipient email<br>
<input type="text" size="25" name="recipient"><br> State your business<br>
<select name="subject" size="1">
<option value="help">help
<option value="tips">tips
<option value="other">other
</select>
<br> Enter your question<br>
<textarea name="body" cols="40" rows="15" wrap="PHYSICAL"></textarea>
<br>
<input type="submit" value=" Submit ">
</form>
</body>
</html>
Then we need to catch the post and read the form. Pick up all information and create the e-mail.
<%
' Get the form data
name = Request.Form("name")
senderEmail = Request.Form("email")
subject = "Regarding " & Request.Form("subject")
recipient = Request.Form("recipient")
body = Request.Form("body")
' Create the JMail message Object
set msg = Server.CreateOBject( "JMail.Message" )
' Set logging to true to ease any potential debugging
' And set silent to true as we wish to handle our errors ourself
msg.Logging = true
msg.silent = true
' Enter the sender data
msg.From = senderEmail
msg.FromName = name
' Note that as addRecipient is method and not
' a property, we do not use an equals ( = ) sign
msg.AddRecipient recipient
' The subject of the message
msg.Subject = subject
' And the body
msg.body = body
' Now send the message, using the indicated mailserver
if not msg.Send("mail.myDomain.net" ) then
Response.write "<pre>" & msg.log & "</pre>"
else
Response.write "Message sent succesfully!"
end if
' And we're done! the message has been sent.
%>
Sending Attachments:
Attaching files to your w3 JMail emails requires that the file you wish to attach resides on the webserver. However, using w3 Upload, you can let the user upload the file to the webserver and attach it on the fly. Check out the samples below to find out more...
First of all we a form where the user can enter his email message and select which file to attach.
<html>
<head>
<title>emailform</title>
</head>
<body>
<font face="verdana, arial" size="2"><b>
<form method="post" action="JmailUploadProcess.asp" ENCTYPE="multipart/form-data">
Complete this form and click the submit-button. We will answer your
questions as soon as possible.
<br><br>
Your name <br>
<input type="text" size="25" name="name"><br>
Your email <br>
<input type="text" size= "25" name= "email"><br>< /FONT>
Recipient email <br>
<input type= "text" size= "25" name= "recipient"><br>< /FONT>
State your business <br>
<select name="subject" size="1">
<option value="help">help
<option value="tips">tips
<option value="other">other
</select>
<br>
Enter your question <br>
<textarea name="body" cols="40" rows="15" wrap="PHYSICAL"></textarea>
<br>
Attachments <br>
<input type="file" name="attachment">
<br>
<br>
<input type="submit" value=" Submit ">
</form>
</b></font>
</body>
</html>
Okay, Now lets put all that info into our email. Again, notice how we use the Upload object instead of the Request object.
<%
' Create the JMail message Object
set msg = Server.CreateOBject( "JMail.Message" )
Set upload = Server.CreateObject( "w3.Upload" )
' Check for attachments and add them to the email
set attachment = upload.Form( "attachment" )
if attachment.IsFile then
msg.AddCustomAttachment attachment.filename, attachment.item, false
end if
' Set logging to true to ease any potential debugging
' And set silent to true as we wish to handle our errors ourself
msg.Logging = true
msg.silent = true
' Get the form variables, using the upload object
Name = upload.Form("name")
SenderEmail = upload.Form("email")
Subject = "Regarding " & upload.Form ("subject")
Recipient = upload.Form("recipient")
Body = upload.Form("body")
' And apply them to the message
msg.From = SenderEmail
msg.FromName = Name
msg.AddRecipient Recipient
msg.Subject = Subject
msg.Body = Body
' To capture any errors which might occur, we wrap the call in an IF statement
if not msg.Send( "mail.myDomain.net" ) then
Response.write "<pre>" & msg.log & "</pre>"
else
Response.write "Message sent succesfully!"
end if
' And we're done! the message has been sent.
%>
Simple Mailmerge:
Mailmerge is an extremely powerful feature of w3 JMail, which is used to create personalized emails from a template.This example will show you how to mailmerge manually. It will create a template and from that create two personalized emails and send them.
<%
' First we will create a message object that will serve as a
' template for the merge.
' The merge fields are marked with %% in the front and in the
' end.
' Note how we use merge fields even in the addRecipient method
set msgTemplate = Server.CreateObject( "JMail.Message" )
msgTemplate.From = "me@myDomain.com"
msgTemplate.FromName = "Mailinglist info!"
msgTemplate.AddRecipient "%%EMail%%>", "%%Name%%"
msgTemplate.Subject = "Hi %%Name%%"
msgTemplate.Body = "Hello %%Name%%, you are my favorite website visitor!"
' There, our message template is done. Next we create the mailmerge object.
set mMerge = Server.CreateObject( "JMail.MailMerge" )
' Now, tell mailMerge to use the template we created earlier
mMerge.MailTemplate = msgTemplate
' Now we have a few options on how to merge.
' We can do it manually or by specifying an ADO Recordset.
' This is the manual way to do it.
' We specify each merge variable and give it a value.
' You will remember the mergeFields below from the template previously.
mMerge.Item( "Name" ) = "Elvis Presley"
mMerge.Item( "EMail" ) = "the.king@graceland.com"
' When the merge variables are finished, we replace all mergeFields
' in our template with the merge variables, using the expand() method.
' expand() returns a Message object.
set msg = mMerge.Expand
' As usual we turn on logging and silent mode to handle possible errors.
msg.Logging = true
msg.silent = true
if not msg.Send( "mail.myDomain.net" ) then
Response.write msg.log
else
Response.write "Mailmerge successful and email sent succesfully!"
end if
' Ok, thats one email sent, however to understand the purpose
' of mailmerge, lets send another one.
' Set some new values in the merge variables
mMerge.Item( "Name" ) = "Frank Sinatra"
mMerge.Item( "EMail" ) = "old.blue.eyes@twinpalms.com"
' Now create us a new Message object with the new variables
' but use the same template. Let's reuse the msg variable as
' we no longer need the last email.
set msg = mMerge.Expand
' As usual we turn on logging and silent mode to handle possible errors.
msg.Logging = true
msg.silent = true
if not msg.Send( "mail.myDomain.net" ) then
Response.write msg.log
else
Response.write "Mailmerge successful again and email sent succesfully!"
end if
' Thats it! One template, 2 emails.
%>