Add mail server connection in application.cfc
To add a mail server connection in an Application.cfc file in ColdFusion, you can use the cfmail
tag or the mail
object. Here's an example of how you can do it:
Method 1: Using the cfmail
tag
<cfapplication name="myapp" />
<cfmail
from="[email protected]"
to="[email protected]"
subject="Test email from ColdFusion"
server="smtp.example.com"
port="587"
username="your_email_username"
password="your_email_password"
type="smtp"
charset="utf-8"
format="html"
>
<cfmailpart type="text">
This is a test email sent from ColdFusion using the cfmail tag.
</cfmailpart>
</cfmail>
In this example, we're using the cfmail
tag to send an email. We're specifying the sender's email address, recipient's email address, subject, and other settings such as the mail server, port, username, and password.
Method 2: Using the mail
object
<cfapplication name="myapp" />
<cfset mail = CreateObject("java", "java.mail.Session").getInstance()>
<cfset mailProperties = {
"mail.smtp.host" = "smtp.example.com",
"mail.smtp.port" = 587,
"mail.smtp.auth" = true,
"mail.smtp.starttls.enable" = true,
"mail.smtp.user" = "your_email_username",
"mail.smtp.password" = "your_email_password"
}>
<cfset mailProperties = mail.getMailSession().getProperties(mailProperties)>
<cfset message = CreateObject("java", "javax.mail.Message").newInstance()>
<cfset message.setFrom("[email protected]")>
<cfset message.setRecipient("To", "[email protected]")>
<cfset message.setSubject("Test email from ColdFusion")>
<cfset message.setText("This is a test email sent from ColdFusion using the mail object.")>
<cfset transport = CreateObject("java", "javax.mail.Transport").getInstance()>
<cfset transport.send(message, mailProperties)>
In this example, we're using the mail
object to send an email. We're creating a Session
object and setting its properties to specify the mail server, port, username, and password. We're then creating a Message
object and setting its properties to specify the sender's email address, recipient's email address, subject, and body. Finally, we're using the Transport
object to send the email.
Note that in both examples, you'll need to replace the placeholders ([email protected]
, [email protected]
, smtp.example.com
, etc.) with your actual email settings.