SMTP API文档

1. 入口

  • 地址:smtp.unimarketing.com.cn
  • 端口:25

2. SMTP帐户密码

  • 从“系统设置”->“API Key 管理”获取SMTP帐户密码:
    • SMTP 帐户:{Consumer Key}
    • SMTP 密码:{Consumer Secret}

3. SMTP信头参数

  • 信头参数列表:
  • 信头 说明 备注
    X-Qos 邮件类型
    transactional缺省为触发邮件不用设置此参数
    bulk非触发交易邮件
    X-Campaign 邮件名称 指定本次发送所采用的邮件名称,如果邮件名称不存在,则会自动创建。
    X-List 联系人列表 指定将邮件发送者添加到的联系人列表。如果联系人列表不存在,则会创建。

4. 要求和限制

  • 1. X-List和X-Campaign,如果包含中文,需要使用BASE64编码。(B编码);
  • 2. 每次调用只能填写一个邮件接收人地址,多于一个时,除第一个外其它会被忽略;
  • 3. 确保联系人列表和发送计划不能大于250个字符;
  • 4. 附件限制64KB。

5. 调用示例

  • 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.com.cn";
    	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 subject        = "邮件主题";
    		String fileAttachment = "附件路径";
    		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));
    		message.setRecipients(Message.RecipientType.TO, receiveAddresses);
    		InternetAddress[] addresses = new InternetAddress[1];
    		addresses[0] = new InternetAddress("someone@somewhere.com");
    		message.setReplyTo(addresses);
    
    		message.addHeader("X-List", MimeUtility.encodeText("联系人列表名称"));
    		message.addHeader("X-Campaign", MimeUtility.encodeText("邮件名称"));
    		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) {
    			e.printStackTrace();
    		} finally {
    			if (reader != null) {
    				reader.close();
    			}
    		}
    
    		System.out.println("发送成功");
    	}
    	
    	static class SmtpAuth extends Authenticator {
    		
    		public PasswordAuthentication getPasswordAuthentication() {
    			return new PasswordAuthentication(USER_NAME, PASSWORD);
    		}
    	}
    }
    
  • 2. 邮件客户端调用示例:
平台介绍
关于我们 | 联系我们 | 行业应用 | 专业术语 | 资源中心 | 隐私声明
版权所有 Copyright 2006-2017 北京灵动创新信息技术有限公司 - 京ICP备09804239号