package com.sendcloud.example; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import com.sun.mail.smtp.SMTPTransport; public class SendCloudSmtp { private static final String SENDCLOUD_SMTP_HOST = "smtp.sendcloud.net"; private static final int SENDCLOUD_SMTP_PORT = 25; private static String getMessage(String reply) { String[] arr = reply.split("#"); String messageId = null; if (arr[0].equalsIgnoreCase("250 ")) { messageId = arr[1]; } return messageId; } public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { // configure javamail Properties props = System.getProperties(); props.setProperty("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", SENDCLOUD_SMTP_HOST); props.put("mail.smtp.port", SENDCLOUD_SMTP_PORT); props.setProperty("mail.smtp.auth", "true"); props.put("mail.smtp.connectiontimeout", 180); props.put("mail.smtp.timeout", 600); props.setProperty("mail.mime.encodefilename", "true"); // Verification using api_user and api_key final String apiUser = "***"; final String apiKey = "***"; String to = "***"; Session mailSession = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(apiUser, apiKey); } }); SMTPTransport transport = (SMTPTransport) mailSession.getTransport("smtp"); MimeMessage message = new MimeMessage(mailSession); // The from, use the correct email address instead message.setFrom(new InternetAddress("from@sendcloud.org", "fromname", "UTF-8")); // The recipient address,use the correct email address instead message.addRecipient(RecipientType.TO, new InternetAddress(to)); // Mail theme message.setSubject("SendCloud java smtp example", "UTF-8"); Multipart multipart = new MimeMultipart("alternative"); // Add HTML message body String html = "
" + "Welcome toSendCloud!
" + " "; BodyPart contentPart = new MimeBodyPart(); contentPart.setHeader("Content-Type", "text/html;charset=UTF-8"); contentPart.setHeader("Content-Transfer-Encoding", "base64"); contentPart.setContent(html, "text/html;charset=UTF-8"); multipart.addBodyPart(contentPart); // Add attachment ( SMTP cannot use FileStream ) File file = new File("/path/file"); BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(file); attachmentBodyPart.setDataHandler(new DataHandler(source)); attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName())); multipart.addBodyPart(attachmentBodyPart); message.setContent(multipart); // Connect to sendcloud server and send mail transport.connect(); transport.sendMessage(message, message.getRecipients(RecipientType.TO)); String messageId = getMessage(transport.getLastServerResponse()); String emailId = messageId + "0$" + to; System.out.println("messageId:" + messageId); System.out.println("emailId:" + emailId); transport.close(); } }