1. Java调用示例:
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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 org.apache.commons.lang3.StringUtils;
public class MySMTP {
private static final String SMTP_SERVER = "smtp.unimarketing.org";
private static final String APIKey = "您的API Key";
private static final String APISecret = "您的API Secret";
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.put("mail.smtp.host", SMTP_SERVER);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");
properties.put("mail.transport.protocol", "smtp");
String fromAddress = "no-reply@example.com";
String recipient = "someone@customer.com";
String replyTo = "replyto@customer.com";
String subject = "邮件主题";
String fileAttachment = "附件路径";
String lang = "en";
String content = "邮件正文内容";
InternetAddress[] receiveAddresses = new InternetAddress[] { new InternetAddress(recipient) };
Session session = Session.getInstance(properties, new SmtpAuth());
session.setDebug(true);
session.setDebugOut(new PrintStream("smtp.log"));
MimeMessage message = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
multipart.setSubType(ContentTypeEnum.ALTERNATIVE.getLabel());
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(content, "text/html; charset=utf-8");
multipart.addBodyPart(bodyPart);
if (StringUtils.isNotBlank(fileAttachment)) {
DataSource dataSource = new FileDataSource(fileAttachment);
String name = dataSource.getName();
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setFileName(MimeUtility.encodeText(name));
multipart.addBodyPart(bodyPart);
}
message.setSubject(subject);
message.setContent(multipart);
//设置发件人地址
message.setFrom(new InternetAddress(fromAddress));
//设置收件人地址,最多5个地址,超过会导致本次调用全部失败
message.setRecipients(Message.RecipientType.TO, receiveAddresses);
//设置回复地址
message.setReplyTo(new InternetAddress(replyTo));
message.addHeader("X-List", MimeUtility.encodeText("联系人列表名称"));
//报表会以这个名称,分别统计数据,建议按照不同用途场景,使用不同的名称,方便识别统计数据
message.addHeader("X-Campaign", MimeUtility.encodeText("邮件名称"));
//营销邮件需要设置该参数
message.addHeader("X-Qos", MimeUtility.encodeText("bulk"));
message.addHeader("X-Lang", lang);
Transport.send(message);
/**
* 获取返回信息
*/
File file = new File("smtp.log");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
if (tempString.lastIndexOf("250 ok:") != -1) {
System.out.println("返回数据为:" + tempString);
}
}
reader.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
if (reader != null) {
reader.close();
}
}
System.out.println("发送成功");
}
static class SmtpAuth extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, PASSWORD);
}
}
}