Sending Email via SMTP

What is SMTP?

SMTP, or simple mail transfer protocol, is a quick and easy way to send email from one server to another.

Mailosaur provides virtual SMTP servers, that allow you to capture email, either to test or protect customer.

Sending a test SMTP email

Before you begin

For each of these examples you will need a Mailosaur account, which you can create one here, as well as a virtual SMTP server, which you can create immediately after signing up.

Ruby

You can send mail by using Mail:

gem install mail

You can then send an email by using this code:

require 'mail'

options = { :address              => 'mailosaur.io',
            :port                 => 25,
            :user_name            => 'SERVER_ID',
            :password             => 'SERVER_PASSWORD',
            :authentication       => 'login' }

Mail.defaults do
  delivery_method :smtp, options
end

Mail.deliver do
       to 'some.recipient@example.com'
     from 'noreply@company.com'
  subject 'Email test'
     body 'Something profound goes here.'
end

Python

Simply use the native smtplib library to send email:

import smtplib

sent_from = 'noreply@company.com'
to = ['some.recipient@example.com']
subject = 'Email test'
email_text = 'Something profound goes here.'

try:
    server = smtplib.SMTP('mailosaur.io', 25)
    server.ehlo()
    server.login('SERVER_ID', 'SERVER_PASSWORD')
    server.sendmail(sent_from, to, email_text)
    server.close()
    
    print 'Email sent!'
except:
    print 'An error occurred'

PHP

You can send mail by using pear/Mail:

composer require pear/mail

You can then send an email by using this code:

require_once('vendor/autoload.php');

$from = '<noreply@company.com>';
$to = '<some.recipient@example.com>';
$subject = 'Email test';
$body = "Something profound goes here.";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'mailosaur.io',
        'port' => '25',
        'auth' => true,
        'username' => 'SERVER_ID',
        'password' => 'SERVER_PASSWORD'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo($mail->getMessage());
} else {
    echo('Message successfully sent!');
}

Java

You can send mail by using the JavaMail API:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

You can then send an email by using this code:

Properties props = new Properties();
props.put("mail.smtp.host", "mailosaur.io");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");

Session session = Session.getDefaultInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("SERVER_ID",
          "SERVER_PASSWORD");
    }    
  });
    	
try {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress("noreply@company.com"));
    message.addRecipient(Message.RecipientType.TO,
      new InternetAddress("some.recipient@example.com"));
    message.setSubject("Email test");
    message.setText("Something profound goes here.");
    
    Transport.send(message);

    System.out.println("Message sent successfully");   
} catch (MessagingException e) {
    throw new RuntimeException(e);
}

Node.js

You can send mail by using Nodemailer:

npm install nodemailer --save

You can then send an email by using this code:

const nodemailer = require('nodemailer');

var transport = nodemailer.createTransport({
  host: 'mailosaur.io',
  port: '25',
  auth: {
    user: 'SERVER_ID',
    pass: 'SERVER_PASSWORD'
  }
});

transport.sendMail({
  from: 'noreply@company.com',
  to: 'some.recipient@example.com',
  subject: 'Email test',
  text: 'Something profound goes here.',
  html: '<p>Something <strong>profound</strong> goes here.</p>'
}).then(() => {
  // asynchronously called
});

.NET

SmtpClient smtpClient = new SmtpClient("mailosaur.io", 25);

smtpClient.Credentials = new
  System.Net.NetworkCredential("SERVER_ID", "SERVER_PASSWORD");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mail = new MailMessage();

//Setting From , To and CC
mail.From = new MailAddress("noreply@company.com");
mail.To.Add(new MailAddress("some.recipient@example.com"));
mail.Subject = "Email test";
mail.Body = "Something profound goes here.";

smtpClient.Send(mail);